SlideShare une entreprise Scribd logo
1  sur  62
Stacks and Queues
Introduction to the Stack
• A stack is a data structure that stores and retrieves items
in a last-in-first-out (LIFO) manner.
Applications of Stacks
• Computer systems use stacks during a program’s
execution to store function return addresses, local
variables, etc.
• Some calculators use stacks for performing
mathematical operations.
• Maze Problem
• Infix and Postfix Evaluation
• Breadth First Search
• Undo sequence in a text editor
• Browser History
A Mazing Problem
entrance

0
1
0
1
1
0
0
0
1
0
0

1
0
1
1
1
0
1
0
1
0
1

0
0
1
0
0
1
1
1
0
1
0

0
0
0
1
1
1
1
1
0
1
0

0
1
0
1
0
0
1
0
0
1
1

1
1
0
1
0
1
0
1
1
1
1

1
0
0
1
1
1
0
1
1
1
1

0
1
1
0
0
1
1
0
0
0
1

0
1
1
1
1
0
1
1
1
0
1

0
1
1
1
1
1
1
1
1
0
0

1
0
1
0
1
0
1
1
0
1
1

1
0
0
1
1
0
1
1
0
1
1

1
1
0
1
1
1
1
1
0
1
1

1
1
1
0
1
0
1
0
0
1
1

1
1
1
0
1
1
1
1
0
0
0

exit

1: blocked path 0: through path
4
Static and Dynamic Stacks
• Static Stacks
– Fixed size
– Can be implemented with an array
• Dynamic Stacks
– Grow in size as needed
– Can be implemented with a linked list
Limitations of arrays
• Once an array is created, its size cannot be altered.
• Array provides inadequate support for inserting,
deleting, sorting, and searching operations.

6
Design of ArrayList and LinkedList
For convenience, let’s name these two classes: MyArrayList
and MyLinkedList. These two classes have common
operations, but different data fields. The common operations
can be generalized in an interface or an abstract class. A good
strategy is to combine the virtues of interfaces and abstract
classes by providing both interface and abstract class in the
design so the user can use either the interface or the abstract
class whichever is convenient. Such an abstract class is known
as a convenience class.
MyArrayList
MyList

MyAbstractList
MyLinkedList

7
Stack Operations
• Push
– causes a value to be stored in (pushed onto) top of the
stack
• Pop
– retrieves and removes a value from the top of the
stack
Thus, we need to keep the index of or have a pointer to
the top element
The Push Operation
• Suppose we have an empty integer stack that is capable of
holding a maximum of three values. With that stack we
execute the following push operations.
push(5);
push(10);
push(12);
The Push Operation
The state of the stack after each of the push operations:
push(5);

Push(10);

push(12);

top
top
top

5

12

10

10

5

5
The Pop Operation
• Now, suppose we execute three consecutive
pop operations on the same stack:
pop();

pop();

pop();

12
10
top

10
5

5
top

5
top
Stacks
• Problem:
– What happens if we try to pop an item off the
stack when the stack is empty?
• This is called a stack underflow. The pop method needs
some way of telling us that this has happened.

12
Reversing a Word
• We can use a stack to reverse the letters in a word.

•How?
• Read each letter in the word and push it onto the stack
• When you reach the end of the word, pop the letters off the
stack and print them out.

13
Other Useful Stack Functions
• isFull: A Boolean function needed for static stacks.
Returns true if the stack is full. Otherwise, returns false.
• isEmpty: A Boolean operation needed for all stacks.
Returns true if the stack is empty. Otherwise, returns false.
Performance and Limitations

(array-based implementation of stack ADT)
• Performance
– Let n be the number of elements in the stack
– The space used is O(n)
– Each operation runs in time O(1)

• Limitations
– The maximum size of the stack must be defined a priori
, and cannot be changed
– Trying to push a new element into a full stack causes an
implementation-specific exception
Stack Summary
• Stack Operation Complexity for Different
Array
Singly
Implementations Array
Fixed-Size Expandable (doubling
strategy)

Linked
List

Pop()

O(1)

O(1)

O(1)

Push(o)

O(1)

O(n) Worst Case
O(1) Best Case
O(1) Average Case

O(1)

Top()

O(1)

O(1)

O(1)

Size(), isEmpty()

O(1)

O(1)

O(1)
A Static Integer Stack Implementation
• Using classes (see IntStack.h and
IntStack.cpp)
Member Variable

Description

stackArray

A pointer to int. When the constructor is executed, it uses
stackArray to dynamically allocate an array for storage.

stackSize

An integer that holds the size of the stack.

top

An integer that is used to mark the top of the stack.
The IntStack Class
• Member Functions
Member Function

Description

constructor

The class constructor accepts an integer argument, which specifies the
size of the stack. An integer array of this size is dynamically
allocated, and assigned to stackArray. Also, the variable top is
initialized to -1.

push

The push function accepts an integer argument, which is pushed onto
the top of the stack.

pop

The pop function uses an integer reference parameter. The value at
the top of the stack is removed, and copied into the reference
parameter.
The IntStack Class
• Member Functions (continued)
Member Function

Description

isFull

Returns true if the stack is full and false otherwise. The stack is
full when top is equal to stackSize - 1.

isEmpty

Returns true if the stack is empty, and false otherwise. The stack
is empty when top is set to -1.
MyStack and MyQueue
MyStack

MyStack
-list: MyArrayList
+isEmpty(): boolean

Returns true if this stack is empty.

+getSize(): int

Returns the number of elements in this stack.

+peek(): Object

Returns the top element in this stack.

+pop(): Object

Returns and removes the top element in this stack.

+push(o: Object): Object

Adds a new element to the top of this stack.

+search(o: Object): int

Returns the position of the specified element in this stack.

MyQueue

MyQueue<E>
-list: MyLinkedList<E>
+enqueue(e: E): void

Adds an element to this queue.

+dequeue(): E

Removes an element from this queue.

+getSize(): int

Returns the number of elements from this queue.
20
Contents of IntStack.h
#ifndef INTSTACK_H
#define INTSTACK_H
class IntStack
{
private:
int *stackArray;
int stackSize;
int top;
public:
IntStack(int);
void push(int);
void pop(int &);
bool isFull(void);
bool isEmpty(void);
};
#endif
Contents of IntStack.cpp
//*******************
// Constructor
*
//*******************
IntStack::IntStack(int size)
{
stackArray = new int[size];
stackSize = size;
top = -1;
}
Contents of IntStack.cpp
//*************************************************
// Member function push pushes the argument onto *
// the stack.
*
//*************************************************
void IntStack::push(int num)
{
if (isFull())
{
cout << "The stack is full.n";
}
else
{
top++;
stackArray[top] = num;
}
}
Contents of IntStack.cpp
//****************************************************
// Member function pop pops the value at the top
*
// of the stack off, and copies it into the variable *
// passed as an argument.
*
//****************************************************
void IntStack::pop(int &num)
{
if (isEmpty())
{
cout << "The stack is empty.n";
}
else
{
num = stackArray[top];
top--;
}
}
Contents of IntStack.cpp
//***************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise.
*
//***************************************************
bool IntStack::isFull(void)
{
bool status;

if (top == stackSize - 1)
status = true;
else
status = false;
return status;

}
Contents of IntStack.cpp
//****************************************************
// Member funciton isEmpty returns true if the stack *
// is empty, or false otherwise.
*
//****************************************************
bool IntStack::isEmpty(void)
{
bool status;

if (top == -1)
status = true;
else
status = false;
return status;

}
Demo Program (IntStackDemo.cpp)
#include "intstack.h"
#include <iostream>
using namespace std;
int main()
{
IntStack stack(5);
int num;
cout << "Created an empty stack with capacity 5, trying to pop. n";
stack.pop(num);
Created an empty stack with
int values[] = {2, 7, 10, 5, 3, 8, 11};
capacity 5, trying to pop.
cout << "nPushing...n";
The stack is empty.
for (int k = 0; k < 7; k++)
Pushing...
{
2
cout << values[k] << " ";
7
stack.push(values[k]);
10
cout << endl;
5
}
3
cout << "nPopping...n";
8 The stack is full.
while (!stack.isEmpty())
11 The stack is full.
{
stack.pop(num);
cout << num << endl;
Popping...
}
3
cout << endl;
5
return 0;
10
}
7
2
Dynamic Stacks
• A dynamic stack is built on a linked list instead of an
array.
• A linked list-based stack offers two advantages over
an array-based stack.
– No need to specify the starting size of the stack.
• A dynamic stack simply starts as an empty linked list, and then
expands by one node each time a value is pushed.

– A dynamic stack will never be full, as long as the system
has enough free memory.

• Next couple of slides give an implementation of a
dynamic integer stack using classes (see
DynIntStack.h and DynIntStack.cpp)
Contents of DynIntStack.h
struct StackNode
{
int value;
StackNode *next;
};
class DynIntStack
{
private:
StackNode *top;
public:
DynIntStack(void);
void push(int);
void pop(int &);
bool isEmpty(void);
};
Contents of DynIntStack.cpp
//************************************************
// Constructor to generate an empty stack.
*
//************************************************

DynIntStack::DynIntStack()
{
top = NULL;
}
Contents of DynIntStack.cpp
//************************************************
// Member function push pushes the argument onto *
// the stack.
*
//************************************************
void DynIntStack::push(int num)
{
StackNode *newNode;
// Allocate a new node & store Num
newNode = new StackNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else
// Otherwise, insert NewNode before top
{
newNode->next = top;
top = newNode;
}
}
Contents of DynIntStack.cpp
/****************************************************
// Member function pop pops the value at the top
*
// of the stack off, and copies it into the variable *
// passed as an argument.
*
//****************************************************
void DynIntStack::pop(int &num)
{
StackNode *temp;
if (isEmpty())
{
cout << "The stack is empty.n";
}
else
// pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
Contents of DynIntStack.cpp
//****************************************************
// Member funciton isEmpty returns true if the stack *
// is empty, or false otherwise.
*
//****************************************************
bool DynIntStack::isEmpty(void)
{
bool status;
if (top == NULL)
status = true;
else
status = false;
return status;
}
Demo Program (DynIntStackDemo.cpp)
// This program demonstrates the dynamic stack class DynIntStack.
#include <iostream>
#include "DynIntStack.h"
using namespace std;
int main()
{
DynIntStack stack;
int catchVar;
// Push values 5, 10, and 15 on the stack
for (int value = 5; value <=15; value = value + 5)
{
cout << "Push: " << value << "n";
Push: 5
stack.push(value);
Push: 10
}
Push: 15
cout << "n";
//Pop three values, then attempt a fourth pop
for (int k = 1; k <= 3; k++)
{
cout << "Pop: ";
stack.pop(catchVar);
cout << catchVar << endl;
}
cout << "nAttempting to pop again... ";
stack.pop(catchVar);
return 0;
}

Pop: 15
Pop: 10
Pop: 5
Attempting to pop again...
The stack is empty.
DynIntStack stack;
char token;
int rhs, lhs, result;
cout << "Please enter postfix expression: ";
while (cin >> token) //as long as there is input
{
if (token >= '0' && token <= '9')
//if digit
{
stack.push(token - '0'); //push it to stack
}
else
//if operator
{
stack.pop(rhs);
stack.pop(lhs); // pop two operands
//and apply the operations. Result is pushed to the stack
if (token == '+')
stack.push(lhs + rhs);
else if (token == '-')
stack.push(lhs - rhs);
else if (token == '*')
stack.push(lhs * rhs);
else if (token == '/')
stack.push(lhs / rhs);
}
}
//after while, the dtack contains only the final result, pop it and display
stack.pop(result);
cout << "result is: " << result << endl;
Queue
Introduction to the Queue
• Like a stack, a queue is a data structure that holds a sequence of
elements.
• A queue, however, provides access to its elements in first-in, first-out
(FIFO) order.
• The elements in a queue are processed like customers standing in a
grocery check-out line: the first customer in line is the first one
served.
Example Applications of Queues
• In a multi-user system, a queue is used to hold print jobs
submitted by users, while the printer services those jobs one at
a time.
• Communications software also uses queues to hold information
received over networks. Sometimes information is transmitted
to a system faster than it can be processed, so it is placed in a
queue when it is received.
• Round Robin Scheduling
• Buffers on MP3 players and portable CD players, iPod playlist
• Call center phone systems will use a queue to hold people in
line until a service representative is free.
Application: Round Robin
Schedulers
•

We can implement a round robin scheduler using a queue,
Q, by repeatedly performing the following steps:
1.
2.
3.

e = Q.dequeue()
Service element e
Q.enqueue(e)

The Queue

1. Deque the
next element

2 . Service the
next element

3. Enqueue the
serviced element

Shared
Service

39
Static and Dynamic Queues
• Just as stacks, queues are implemented as
arrays or linked lists.
• Dynamic queues offer the same advantages
over static queues that dynamic stacks offer
over static stacks.
Queue Operations
• Think of queues as having a front and a
rear.
– rear: position where elements are added
– front: position from which elements are
removed
Queue Operations
• The two primary queue operations are
enqueuing and dequeuing.
• To enqueue means to insert an element at
the rear of a queue.
• To dequeue means to remove an element
from the front of a queue.
Efficiency Problem of Dequeue & Solution
• Shifting after each dequeue operation
causes ineffiency.
• Solution
– Let front index move as elements are
removed
– let rear index "wrap around" to the beginning
of array, treating array as circular
• Similarly, the front index as well
– Yields more complex enqueue, dequeue code,
but more efficient
– Let's see the trace of this method on the board
for the enqueue and dequeue operations given
on the right (queue size is 3)

Enqueue(3);
Enqueue(6);
Enqueue(9);
Dequeue();
Dequeue();
Enqueue(12);
Dequeue();
INITIALIZE THE QUEUE
•The queue is initialized by having the rear set to -1, and
front set to 0. Let us assume that maximum number of the
element we have in a queue is MAXQUEUE elements as
shown below.

items[MAXQUEUE-1]

.
.

.
.

.
items[1]

items[0]

front=0
rear=-1
insert(&Queue, ‘A’)
• an item (A) is inserted at the Rear of the queue

items[MAXQUEUE
-1]

.
.
items[3]
items[2]
items[1]
items[0]

.
.

A

Front=0,
Rear=0
insert(&Queue, ‘B’)
• A new item (B) is inserted at the Rear of the queue

items[MAXQUEUE
-1]

.
.
items[3]
items[2]
items[1]
items[0]

.
.

B
A

Rear=1
Front=0
insert(&Queue, ‘C’)
• A new item (C) is inserted at the Rear of the queue

items[MAXQUEUE
-1]

.
.
items[3]
items[2]
items[1]
items[0]

.
.
C
B
A

Rear=2

Front=0
insert(&Queue, ‘D’)
• A new item (D) is inserted at the Rear of the queue

items[MAXQUEUE-1]
.
.
items[3]
items[2]
items[1]
items[0]

.
.
D
C
B
A

Rear=3

Front=0
char remove(&Queue)
• an item (A) is removed (deleted) from the Front of
the queue
items[MAXQUEUE-1]
.
.

.
.

items[3]

D

items[2]

C

items[1]

B

items[0]

A

Rear=3
Front=1
char remove(&Queue)
• Remove two items from the front of the queue.
items[MAXQUEUE-1]
.
.

.
.

items[3]

D

Rear=3

items[2]
items[1]
items[0]

C
B
A

Front=2
char remove(&Queue)
• Remove two items from the front of the queue.
items[MAXQUEUE-1]
.
.

.
.

items[3]

D

items[2]
items[1]
items[0]

C
B
A

Front=Rear=3
char remove(&Queue)
• Remove one more item from the front of the queue.
items[MAXQUEUE-1]
.
items[4]

.

items[3]

D

items[2]
items[1]
items[0]

C
B
A

Front=4
Rear=3
Performance and Limitations
- array-based implementation of queue ADT

• Performance
– Let n be the number of elements in the queue
– The space used is O(n)
– Each operation runs in time O(1)

• Limitations
– The maximum size of the queue must be defined a
priori , and cannot be changed
– Trying to enqueue a new element into a full queue
causes an implementation-specific exception
Queue Summary
• Queue Operation Complexity for Different
Implementations
Array
Array
Fixed-Size Expandable (doubling
strategy)

List
SinglyLinked

dequeue()

O(1)

O(1)

O(1)

enqueue(o)

O(1)

O(n) Worst Case
O(1) Best Case
O(1) Average Case

O(1)

front()

O(1)

O(1)

O(1)

Size(), isEmpty()

O(1)

O(1)

O(1)
Implementation of a Static Queue
• The previous discussion was about static arrays
– Container is an array

• Class Implementation for a static integer queue
– Member functions
• enqueue
• dequeue
• isEmpty
• isFull
• clear
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

void insert_element()
{
int num;
printf("n Enter the number to be inserted: ");
scanf("%d",&num);
if(front==0 && rear==MAX-1)
printf("n Queue OverFlow Occured");
else if(front==-1&&rear==-1)
{
front=rear=0;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

void delete_element()
{
int element;
if(front== -1)
{
printf("n Underflow");
}
element=queue[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
printf("n The deleted element is: %d",element);
}
}
Dynamic Queues
• Like a stack, a queue can be implemented
using a linked list
• Allows dynamic sizing, avoids issue of
shifting elements or wrapping indices
NULL

front

rear
Dynamic Queues
• A dynamic queue starts as an empty linked list.
• With the first enqueue operation, a node is added,
which is pointed to by front and rear pointers.
• As each new item is added to the queue, a new
node is added to the rear of the list, and the rear
pointer is updated to point to the new node.
• As each item is dequeued, the node pointed to by
the front pointer is deleted, and front is made
to point to the next node in the list.
• See the next slides for the implementation of an
integer dynamic queue.
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert()
{
printf("nnEnter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
}
else
{
rear->link = (struct node *)malloc(sizeof(struct node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
}
void delete()
{
struct node *ptr;
if(front == NULL)
printf("nnQueue is empty.n");
else
{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
printf("nItem deleted: %dn", item);
if(front == NULL)
rear = NULL;
}
}

Contenu connexe

Tendances

03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
Amit Vats
 

Tendances (19)

Stacks
StacksStacks
Stacks
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
stack & queue
stack & queuestack & queue
stack & queue
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
 
Stack project
Stack projectStack project
Stack project
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
Stack & queue
Stack & queueStack & queue
Stack & queue
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 

En vedette

Java Stack Traces
Java Stack TracesJava Stack Traces
Java Stack Traces
dbanttari
 
Ml seminar ppt 2014
Ml seminar ppt 2014Ml seminar ppt 2014
Ml seminar ppt 2014
nurulhuda41
 
100.10.05 公園一科 科務報告
100.10.05 公園一科 科務報告100.10.05 公園一科 科務報告
100.10.05 公園一科 科務報告
gongwujugongwuju
 
Internet decency legislation
Internet decency legislationInternet decency legislation
Internet decency legislation
Tgarmon34
 

En vedette (20)

Java
JavaJava
Java
 
Linked list (java platform se 8 )
Linked list (java platform se 8 )Linked list (java platform se 8 )
Linked list (java platform se 8 )
 
Java Stack Traces
Java Stack TracesJava Stack Traces
Java Stack Traces
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Heap and stack space in java
Heap and stack space in javaHeap and stack space in java
Heap and stack space in java
 
Sorting Technique
Sorting TechniqueSorting Technique
Sorting Technique
 
04 sorting
04 sorting04 sorting
04 sorting
 
CHC Finance: Using the New IRS 990 Form
CHC Finance: Using the New IRS 990 FormCHC Finance: Using the New IRS 990 Form
CHC Finance: Using the New IRS 990 Form
 
100.11.23養護科科務報告
100.11.23養護科科務報告100.11.23養護科科務報告
100.11.23養護科科務報告
 
2012南瀛綠都心迎春花卉展
2012南瀛綠都心迎春花卉展2012南瀛綠都心迎春花卉展
2012南瀛綠都心迎春花卉展
 
第21局局務會議紀錄
第21局局務會議紀錄第21局局務會議紀錄
第21局局務會議紀錄
 
100.11.2公園二科業務簡報
100.11.2公園二科業務簡報100.11.2公園二科業務簡報
100.11.2公園二科業務簡報
 
NTU & NTUST Tangible Interaction Design Project Proposal 1
NTU & NTUST Tangible Interaction Design Project Proposal 1NTU & NTUST Tangible Interaction Design Project Proposal 1
NTU & NTUST Tangible Interaction Design Project Proposal 1
 
使用管理科業務報告 1102
使用管理科業務報告 1102使用管理科業務報告 1102
使用管理科業務報告 1102
 
工務局第三十次局務會議紀錄
工務局第三十次局務會議紀錄工務局第三十次局務會議紀錄
工務局第三十次局務會議紀錄
 
Ml seminar ppt 2014
Ml seminar ppt 2014Ml seminar ppt 2014
Ml seminar ppt 2014
 
100.10.05 公園一科 科務報告
100.10.05 公園一科 科務報告100.10.05 公園一科 科務報告
100.10.05 公園一科 科務報告
 
0802局務會議報告--使管
0802局務會議報告--使管0802局務會議報告--使管
0802局務會議報告--使管
 
科務報告(企劃科) 2011.08.25(定稿版)
科務報告(企劃科) 2011.08.25(定稿版)科務報告(企劃科) 2011.08.25(定稿版)
科務報告(企劃科) 2011.08.25(定稿版)
 
Internet decency legislation
Internet decency legislationInternet decency legislation
Internet decency legislation
 

Similaire à My lecture stack_queue_operation

stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
HusnainNaqvi2
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 

Similaire à My lecture stack_queue_operation (20)

STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
02 stackqueue
02 stackqueue02 stackqueue
02 stackqueue
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Stacks
StacksStacks
Stacks
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
02 Stack
02 Stack02 Stack
02 Stack
 

Dernier

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

My lecture stack_queue_operation

  • 2. Introduction to the Stack • A stack is a data structure that stores and retrieves items in a last-in-first-out (LIFO) manner.
  • 3. Applications of Stacks • Computer systems use stacks during a program’s execution to store function return addresses, local variables, etc. • Some calculators use stacks for performing mathematical operations. • Maze Problem • Infix and Postfix Evaluation • Breadth First Search • Undo sequence in a text editor • Browser History
  • 5. Static and Dynamic Stacks • Static Stacks – Fixed size – Can be implemented with an array • Dynamic Stacks – Grow in size as needed – Can be implemented with a linked list
  • 6. Limitations of arrays • Once an array is created, its size cannot be altered. • Array provides inadequate support for inserting, deleting, sorting, and searching operations. 6
  • 7. Design of ArrayList and LinkedList For convenience, let’s name these two classes: MyArrayList and MyLinkedList. These two classes have common operations, but different data fields. The common operations can be generalized in an interface or an abstract class. A good strategy is to combine the virtues of interfaces and abstract classes by providing both interface and abstract class in the design so the user can use either the interface or the abstract class whichever is convenient. Such an abstract class is known as a convenience class. MyArrayList MyList MyAbstractList MyLinkedList 7
  • 8. Stack Operations • Push – causes a value to be stored in (pushed onto) top of the stack • Pop – retrieves and removes a value from the top of the stack Thus, we need to keep the index of or have a pointer to the top element
  • 9. The Push Operation • Suppose we have an empty integer stack that is capable of holding a maximum of three values. With that stack we execute the following push operations. push(5); push(10); push(12);
  • 10. The Push Operation The state of the stack after each of the push operations: push(5); Push(10); push(12); top top top 5 12 10 10 5 5
  • 11. The Pop Operation • Now, suppose we execute three consecutive pop operations on the same stack: pop(); pop(); pop(); 12 10 top 10 5 5 top 5 top
  • 12. Stacks • Problem: – What happens if we try to pop an item off the stack when the stack is empty? • This is called a stack underflow. The pop method needs some way of telling us that this has happened. 12
  • 13. Reversing a Word • We can use a stack to reverse the letters in a word. •How? • Read each letter in the word and push it onto the stack • When you reach the end of the word, pop the letters off the stack and print them out. 13
  • 14. Other Useful Stack Functions • isFull: A Boolean function needed for static stacks. Returns true if the stack is full. Otherwise, returns false. • isEmpty: A Boolean operation needed for all stacks. Returns true if the stack is empty. Otherwise, returns false.
  • 15. Performance and Limitations (array-based implementation of stack ADT) • Performance – Let n be the number of elements in the stack – The space used is O(n) – Each operation runs in time O(1) • Limitations – The maximum size of the stack must be defined a priori , and cannot be changed – Trying to push a new element into a full stack causes an implementation-specific exception
  • 16. Stack Summary • Stack Operation Complexity for Different Array Singly Implementations Array Fixed-Size Expandable (doubling strategy) Linked List Pop() O(1) O(1) O(1) Push(o) O(1) O(n) Worst Case O(1) Best Case O(1) Average Case O(1) Top() O(1) O(1) O(1) Size(), isEmpty() O(1) O(1) O(1)
  • 17. A Static Integer Stack Implementation • Using classes (see IntStack.h and IntStack.cpp) Member Variable Description stackArray A pointer to int. When the constructor is executed, it uses stackArray to dynamically allocate an array for storage. stackSize An integer that holds the size of the stack. top An integer that is used to mark the top of the stack.
  • 18. The IntStack Class • Member Functions Member Function Description constructor The class constructor accepts an integer argument, which specifies the size of the stack. An integer array of this size is dynamically allocated, and assigned to stackArray. Also, the variable top is initialized to -1. push The push function accepts an integer argument, which is pushed onto the top of the stack. pop The pop function uses an integer reference parameter. The value at the top of the stack is removed, and copied into the reference parameter.
  • 19. The IntStack Class • Member Functions (continued) Member Function Description isFull Returns true if the stack is full and false otherwise. The stack is full when top is equal to stackSize - 1. isEmpty Returns true if the stack is empty, and false otherwise. The stack is empty when top is set to -1.
  • 20. MyStack and MyQueue MyStack MyStack -list: MyArrayList +isEmpty(): boolean Returns true if this stack is empty. +getSize(): int Returns the number of elements in this stack. +peek(): Object Returns the top element in this stack. +pop(): Object Returns and removes the top element in this stack. +push(o: Object): Object Adds a new element to the top of this stack. +search(o: Object): int Returns the position of the specified element in this stack. MyQueue MyQueue<E> -list: MyLinkedList<E> +enqueue(e: E): void Adds an element to this queue. +dequeue(): E Removes an element from this queue. +getSize(): int Returns the number of elements from this queue. 20
  • 21. Contents of IntStack.h #ifndef INTSTACK_H #define INTSTACK_H class IntStack { private: int *stackArray; int stackSize; int top; public: IntStack(int); void push(int); void pop(int &); bool isFull(void); bool isEmpty(void); }; #endif
  • 22. Contents of IntStack.cpp //******************* // Constructor * //******************* IntStack::IntStack(int size) { stackArray = new int[size]; stackSize = size; top = -1; }
  • 23. Contents of IntStack.cpp //************************************************* // Member function push pushes the argument onto * // the stack. * //************************************************* void IntStack::push(int num) { if (isFull()) { cout << "The stack is full.n"; } else { top++; stackArray[top] = num; } }
  • 24. Contents of IntStack.cpp //**************************************************** // Member function pop pops the value at the top * // of the stack off, and copies it into the variable * // passed as an argument. * //**************************************************** void IntStack::pop(int &num) { if (isEmpty()) { cout << "The stack is empty.n"; } else { num = stackArray[top]; top--; } }
  • 25. Contents of IntStack.cpp //*************************************************** // Member function isFull returns true if the stack * // is full, or false otherwise. * //*************************************************** bool IntStack::isFull(void) { bool status; if (top == stackSize - 1) status = true; else status = false; return status; }
  • 26. Contents of IntStack.cpp //**************************************************** // Member funciton isEmpty returns true if the stack * // is empty, or false otherwise. * //**************************************************** bool IntStack::isEmpty(void) { bool status; if (top == -1) status = true; else status = false; return status; }
  • 27. Demo Program (IntStackDemo.cpp) #include "intstack.h" #include <iostream> using namespace std; int main() { IntStack stack(5); int num; cout << "Created an empty stack with capacity 5, trying to pop. n"; stack.pop(num); Created an empty stack with int values[] = {2, 7, 10, 5, 3, 8, 11}; capacity 5, trying to pop. cout << "nPushing...n"; The stack is empty. for (int k = 0; k < 7; k++) Pushing... { 2 cout << values[k] << " "; 7 stack.push(values[k]); 10 cout << endl; 5 } 3 cout << "nPopping...n"; 8 The stack is full. while (!stack.isEmpty()) 11 The stack is full. { stack.pop(num); cout << num << endl; Popping... } 3 cout << endl; 5 return 0; 10 } 7 2
  • 28. Dynamic Stacks • A dynamic stack is built on a linked list instead of an array. • A linked list-based stack offers two advantages over an array-based stack. – No need to specify the starting size of the stack. • A dynamic stack simply starts as an empty linked list, and then expands by one node each time a value is pushed. – A dynamic stack will never be full, as long as the system has enough free memory. • Next couple of slides give an implementation of a dynamic integer stack using classes (see DynIntStack.h and DynIntStack.cpp)
  • 29. Contents of DynIntStack.h struct StackNode { int value; StackNode *next; }; class DynIntStack { private: StackNode *top; public: DynIntStack(void); void push(int); void pop(int &); bool isEmpty(void); };
  • 30. Contents of DynIntStack.cpp //************************************************ // Constructor to generate an empty stack. * //************************************************ DynIntStack::DynIntStack() { top = NULL; }
  • 31. Contents of DynIntStack.cpp //************************************************ // Member function push pushes the argument onto * // the stack. * //************************************************ void DynIntStack::push(int num) { StackNode *newNode; // Allocate a new node & store Num newNode = new StackNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node if (isEmpty()) { top = newNode; newNode->next = NULL; } else // Otherwise, insert NewNode before top { newNode->next = top; top = newNode; } }
  • 32. Contents of DynIntStack.cpp /**************************************************** // Member function pop pops the value at the top * // of the stack off, and copies it into the variable * // passed as an argument. * //**************************************************** void DynIntStack::pop(int &num) { StackNode *temp; if (isEmpty()) { cout << "The stack is empty.n"; } else // pop value off top of stack { num = top->value; temp = top->next; delete top; top = temp; } }
  • 33. Contents of DynIntStack.cpp //**************************************************** // Member funciton isEmpty returns true if the stack * // is empty, or false otherwise. * //**************************************************** bool DynIntStack::isEmpty(void) { bool status; if (top == NULL) status = true; else status = false; return status; }
  • 34. Demo Program (DynIntStackDemo.cpp) // This program demonstrates the dynamic stack class DynIntStack. #include <iostream> #include "DynIntStack.h" using namespace std; int main() { DynIntStack stack; int catchVar; // Push values 5, 10, and 15 on the stack for (int value = 5; value <=15; value = value + 5) { cout << "Push: " << value << "n"; Push: 5 stack.push(value); Push: 10 } Push: 15 cout << "n"; //Pop three values, then attempt a fourth pop for (int k = 1; k <= 3; k++) { cout << "Pop: "; stack.pop(catchVar); cout << catchVar << endl; } cout << "nAttempting to pop again... "; stack.pop(catchVar); return 0; } Pop: 15 Pop: 10 Pop: 5 Attempting to pop again... The stack is empty.
  • 35. DynIntStack stack; char token; int rhs, lhs, result; cout << "Please enter postfix expression: "; while (cin >> token) //as long as there is input { if (token >= '0' && token <= '9') //if digit { stack.push(token - '0'); //push it to stack } else //if operator { stack.pop(rhs); stack.pop(lhs); // pop two operands //and apply the operations. Result is pushed to the stack if (token == '+') stack.push(lhs + rhs); else if (token == '-') stack.push(lhs - rhs); else if (token == '*') stack.push(lhs * rhs); else if (token == '/') stack.push(lhs / rhs); } } //after while, the dtack contains only the final result, pop it and display stack.pop(result); cout << "result is: " << result << endl;
  • 36. Queue
  • 37. Introduction to the Queue • Like a stack, a queue is a data structure that holds a sequence of elements. • A queue, however, provides access to its elements in first-in, first-out (FIFO) order. • The elements in a queue are processed like customers standing in a grocery check-out line: the first customer in line is the first one served.
  • 38. Example Applications of Queues • In a multi-user system, a queue is used to hold print jobs submitted by users, while the printer services those jobs one at a time. • Communications software also uses queues to hold information received over networks. Sometimes information is transmitted to a system faster than it can be processed, so it is placed in a queue when it is received. • Round Robin Scheduling • Buffers on MP3 players and portable CD players, iPod playlist • Call center phone systems will use a queue to hold people in line until a service representative is free.
  • 39. Application: Round Robin Schedulers • We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: 1. 2. 3. e = Q.dequeue() Service element e Q.enqueue(e) The Queue 1. Deque the next element 2 . Service the next element 3. Enqueue the serviced element Shared Service 39
  • 40. Static and Dynamic Queues • Just as stacks, queues are implemented as arrays or linked lists. • Dynamic queues offer the same advantages over static queues that dynamic stacks offer over static stacks.
  • 41. Queue Operations • Think of queues as having a front and a rear. – rear: position where elements are added – front: position from which elements are removed
  • 42. Queue Operations • The two primary queue operations are enqueuing and dequeuing. • To enqueue means to insert an element at the rear of a queue. • To dequeue means to remove an element from the front of a queue.
  • 43. Efficiency Problem of Dequeue & Solution • Shifting after each dequeue operation causes ineffiency. • Solution – Let front index move as elements are removed – let rear index "wrap around" to the beginning of array, treating array as circular • Similarly, the front index as well – Yields more complex enqueue, dequeue code, but more efficient – Let's see the trace of this method on the board for the enqueue and dequeue operations given on the right (queue size is 3) Enqueue(3); Enqueue(6); Enqueue(9); Dequeue(); Dequeue(); Enqueue(12); Dequeue();
  • 44. INITIALIZE THE QUEUE •The queue is initialized by having the rear set to -1, and front set to 0. Let us assume that maximum number of the element we have in a queue is MAXQUEUE elements as shown below. items[MAXQUEUE-1] . . . . . items[1] items[0] front=0 rear=-1
  • 45. insert(&Queue, ‘A’) • an item (A) is inserted at the Rear of the queue items[MAXQUEUE -1] . . items[3] items[2] items[1] items[0] . . A Front=0, Rear=0
  • 46. insert(&Queue, ‘B’) • A new item (B) is inserted at the Rear of the queue items[MAXQUEUE -1] . . items[3] items[2] items[1] items[0] . . B A Rear=1 Front=0
  • 47. insert(&Queue, ‘C’) • A new item (C) is inserted at the Rear of the queue items[MAXQUEUE -1] . . items[3] items[2] items[1] items[0] . . C B A Rear=2 Front=0
  • 48. insert(&Queue, ‘D’) • A new item (D) is inserted at the Rear of the queue items[MAXQUEUE-1] . . items[3] items[2] items[1] items[0] . . D C B A Rear=3 Front=0
  • 49. char remove(&Queue) • an item (A) is removed (deleted) from the Front of the queue items[MAXQUEUE-1] . . . . items[3] D items[2] C items[1] B items[0] A Rear=3 Front=1
  • 50. char remove(&Queue) • Remove two items from the front of the queue. items[MAXQUEUE-1] . . . . items[3] D Rear=3 items[2] items[1] items[0] C B A Front=2
  • 51. char remove(&Queue) • Remove two items from the front of the queue. items[MAXQUEUE-1] . . . . items[3] D items[2] items[1] items[0] C B A Front=Rear=3
  • 52. char remove(&Queue) • Remove one more item from the front of the queue. items[MAXQUEUE-1] . items[4] . items[3] D items[2] items[1] items[0] C B A Front=4 Rear=3
  • 53. Performance and Limitations - array-based implementation of queue ADT • Performance – Let n be the number of elements in the queue – The space used is O(n) – Each operation runs in time O(1) • Limitations – The maximum size of the queue must be defined a priori , and cannot be changed – Trying to enqueue a new element into a full queue causes an implementation-specific exception
  • 54. Queue Summary • Queue Operation Complexity for Different Implementations Array Array Fixed-Size Expandable (doubling strategy) List SinglyLinked dequeue() O(1) O(1) O(1) enqueue(o) O(1) O(n) Worst Case O(1) Best Case O(1) Average Case O(1) front() O(1) O(1) O(1) Size(), isEmpty() O(1) O(1) O(1)
  • 55. Implementation of a Static Queue • The previous discussion was about static arrays – Container is an array • Class Implementation for a static integer queue – Member functions • enqueue • dequeue • isEmpty • isFull • clear
  • 56. • • • • • • • • • • • • • • • • • • void insert_element() { int num; printf("n Enter the number to be inserted: "); scanf("%d",&num); if(front==0 && rear==MAX-1) printf("n Queue OverFlow Occured"); else if(front==-1&&rear==-1) { front=rear=0; queue[rear]=num; } else { rear++; queue[rear]=num; } }
  • 57. • • • • • • • • • • • • • • • • • • • • void delete_element() { int element; if(front== -1) { printf("n Underflow"); } element=queue[front]; if(front==rear) front=rear=-1; else { if(front==MAX-1) front=0; else front++; printf("n The deleted element is: %d",element); } }
  • 58. Dynamic Queues • Like a stack, a queue can be implemented using a linked list • Allows dynamic sizing, avoids issue of shifting elements or wrapping indices NULL front rear
  • 59. Dynamic Queues • A dynamic queue starts as an empty linked list. • With the first enqueue operation, a node is added, which is pointed to by front and rear pointers. • As each new item is added to the queue, a new node is added to the rear of the list, and the rear pointer is updated to point to the new node. • As each item is dequeued, the node pointed to by the front pointer is deleted, and front is made to point to the next node in the list. • See the next slides for the implementation of an integer dynamic queue.
  • 60. struct node { int info; struct node *link; }*front = NULL, *rear = NULL;
  • 61. void insert() { printf("nnEnter ITEM: "); scanf("%d", &item); if(rear == NULL) { rear = (struct node *)malloc(sizeof(struct node)); rear->info = item; rear->link = NULL; front = rear; } else { rear->link = (struct node *)malloc(sizeof(struct node)); rear = rear->link; rear->info = item; rear->link = NULL; }
  • 62. void delete() { struct node *ptr; if(front == NULL) printf("nnQueue is empty.n"); else { ptr = front; item = front->info; front = front->link; free(ptr); printf("nItem deleted: %dn", item); if(front == NULL) rear = NULL; } }