Stack and Queue
APURBO DATTA
Overview
 what is stack ?
 Stack in Programming.
 Basic operations of stack(Pushing, popping etc.)
 Implementation of Stacks
 Queue Definition
 Basic operations of queue
Enqueuing, dequeuing etc.
 Implementation of queue
Stack Overview
 Stack Definition
 what is stack ?
 Condition of stack.
 Stack in Programming.
 Basic operations of stack(Pushing, popping etc.)
 Implementation of Stacks
Stack Definition
We know that the Stack is LIFO Structure i,e Last in First Out. It
is very useful data structure in C Programming. Stack can be
implemented using the Linked List or Array.
1.Stack is LIFO Structure [ Last in First
Out ]
2.Stack is Ordered List of Elements of
Same Type.
3.Stack is Linear List
4.In Stack all Operations such as
Insertion and Deletion are permitted at
only one end called Top
Condition of stack
Stack in Programming
Similar is the idea of stack in Programming
1. 1. You can add an element onto the
stack.
● 2. You can remove the top most element.
● 3. You can see the top most element.
Push: Equivalent to an insert
Pop: Deletes the most recently inserted element
Top: Examines the most recently inserted element
Fundamental operations
Push and Pop
Push
Add an element to the top of the stack
Pop
Remove the element at the top of the stack
empty stack
top
top
push an element
A A A
B
push another pop
top
top
Implementation of Stacks
 Any list implementation could be used to implement a
stack
 Arrays (static: the size of stack is given initially)
 Linked lists (dynamic: never become full)
 We will explore implementations based on array and
linked list
 Let’s see how to use an array to implement a stack first
ArrayImplementation of Stack
Just like the array implementation of the List, we also
need the array of items where we are going to store
the elements of the Stack
Aside from this, we also need an object that keeps
track of where the last element is located
 From this point on, we are going to call it the top
 top is simply an integer (very much like the head in the
cursor implementation of the List)
Our Stack class should look very much like this:
const MAX = 100;
class Stack{
private:
int top, items[MAX];
public:
Stack();
bool push(int);
bool pop();
int peek(); //int top();
bool isEmpty();
bool isFull();
void display();
};
Array Implementation of Stack
The constructor
Stack::Stack(){
top = -1;
}
The full check
bool Stack::isFull(){
if(top+1==MAX)
return true;
return false;
}
The empty check
bool Stack::isEmpty(){
if(top==-1)
return true;
return false;
}
Array Implementation of Stack
The push
bool Stack::push(int x){
if(isFull())
return false;
items[++top] = x;
return true;
}
The pop
bool Stack::pop(){
if(isEmpty())
return false;
top--;
return true;
}
10 13 16 19 22
top = -1
top = 0 top =1 top =2 top =3 top =4
10 13 16 19 22
top = 0 top =1 top =2 top =3 top =4
Array Implementation of Stack
Linked-list Implementation of Stack
This implementation is the linked-list implementation of the list
except for the following operations
General insert and append
General delete
44 97 23 17
head: tail:
9
Linked-list Implementation of Stack
44 97 23 17
9
PUSH
top:
top:
44 97 23 17
top:
tmp tmp tmp tmp
del
9
Linked-list Implementation of Stack
pop
Queue Overview
 Queue Definition
 Basic operations of queue
Enqueuing, dequeuing etc.
 Implementation of queue
 Array
 Linked list
What is Queue
 The Queue is like the List but with “limited”
insertion and deletion.
 Insertion can be done only at the end or rear
 Deletion can only be done in the front
 FIFO – first-in-first-out data structure
 Operations
enqueue
dequeue
Basic operations of queue
Primary queue operations: Enqueue and Dequeue
Like check-out lines in a store, a queue has a front and a rear.
Enqueue
◦ Insert an element at the rear of the queue
Dequeue
◦ Remove an element from the front of the queue
Insert
(Enqueue)Remove
(Dequeue)
rearfront
C Program
Algorithm
Enqueue Operation
Algorithm
C Program
Dequeue Operation
Storing a queue in a static data structure
 This implementation stores the queue in an array.
 The array indices at which the head and tail of the queue are currently stored must be maintained.
 The head of the queue is not necessarily at index 0. The array can be a “circular array” – the queue
“wraps round” if the last index of the array is reached.
Example – storing a queue in an array of length 5
Storing a queue in a dynamic data structure
 Each node in a dynamic data structure contains data AND a reference to the next node.
 A queue also needs a reference to the head node AND a reference to the tail node.
 The following diagram describes the storage of a queue called Queue. Each node consists of data
(DataItem) and a reference (NextNode).
 The first node is accessed using the name Queue.Head.
 Its data is accessed using Queue.Head.DataItem
 The second node is accessed using Queue.Head.NextNode
 The last node is accessed using Queue.Tail
Adding a node (Add) in a dynamic data structure
 The new node is to be added at the tail of the queue. The reference Queue.Tail should point to the
new node, and the NextNode reference of the node previously at the tail of the queue should point
to the DataItem of the new node.
Removing a node (Remove) in a dynamic data structure
 The value of Queue.Head.DataItem is returned. A temporary reference Temp is declared and set to point
to head node in the queue (Temp = Queue.Head). Queue.Head is then set to point to the second node
instead of the top node.
 The only reference to the original head node is now Temp and the memory used by this node can then
be freed.
Implementation of Queue
 Just as stacks can be implemented as
arrays or linked lists, so with queues.
 Dynamic queues have the same
advantages over static queues as
dynamic stacks have over static
stacks
Array Implementation of Queue
10 13 16 19 22
size=5size=1 size=2 size=3 size=4
10 13 16 29 22
size=1 size=2 size=3 size=4 size=5
/*Queue - Linked List implementation*/
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
// Two l variables to store address of front and rear nodes.
struct Node* front = NULL;
struct Node* rear = NULL;
// To Enqueue an integer
void Enqueue(int x)
{
struct Node* temp =
(struct Node*)malloc(sizeof(struct Node));
temp->data =x;
temp->next = NULL;
if(front == NULL && rear == NULL){
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
queue using linked list in c // To Dequeue an integer.
void Dequeue() {
struct Node* temp = front;
if(front == NULL) {
printf("Queue is Emptyn");
return;
}
if(front == rear) {
front = rear = NULL;
}
else {
front = front->next;
}
free(temp);
}
int Front() {
if(front == NULL) {
printf("Queue is emptyn");
return;
}
return front->data;
}
void Print() {
struct Node* temp = front;
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("n");
}
int main(){
/* Drive code to test the
implementation. */
// Printing elements in Queue
after each Enqueue or Dequeue
Enqueue(2); Print();
Enqueue(4); Print();
Enqueue(6); Print();
Dequeue(); Print();
Enqueue(8); Print();
Implement queue using linked list in c
Comparing queue implementations
•Memory requirements
– Array-based implementation
• Assume a queue (size: 100) of strings (80 bytes
each)
• Assume indices take 2 bytes
• Total memory: (80 bytes x 101 slots) + (2 bytes x 2
indexes) = 8084 bytes
– Linked-list-based implementation
• Assume pointers take 4 bytes
• Total memory per node: 80 bytes + 4 bytes = 84
bytes
Comparing queue implementations
(cont.)
Comparing queue implementations
Big-O Comparison of Queue Operations
Operation Array
Implementation
Linked
Implementation
Class constructor O(1) O(1)
MakeEmpty O(1) O(N)
IsFull O(1) O(1)
IsEmpty O(1) O(1)
Enqueue O(1) O(1)
Dequeue O(1) O(1)
Destructor O(1) O(N)
Stack and Queue

Stack and Queue

  • 1.
  • 2.
    Overview  what isstack ?  Stack in Programming.  Basic operations of stack(Pushing, popping etc.)  Implementation of Stacks  Queue Definition  Basic operations of queue Enqueuing, dequeuing etc.  Implementation of queue
  • 3.
    Stack Overview  StackDefinition  what is stack ?  Condition of stack.  Stack in Programming.  Basic operations of stack(Pushing, popping etc.)  Implementation of Stacks
  • 4.
    Stack Definition We knowthat the Stack is LIFO Structure i,e Last in First Out. It is very useful data structure in C Programming. Stack can be implemented using the Linked List or Array.
  • 5.
    1.Stack is LIFOStructure [ Last in First Out ] 2.Stack is Ordered List of Elements of Same Type. 3.Stack is Linear List 4.In Stack all Operations such as Insertion and Deletion are permitted at only one end called Top Condition of stack
  • 6.
    Stack in Programming Similaris the idea of stack in Programming 1. 1. You can add an element onto the stack. ● 2. You can remove the top most element. ● 3. You can see the top most element.
  • 7.
    Push: Equivalent toan insert Pop: Deletes the most recently inserted element Top: Examines the most recently inserted element Fundamental operations
  • 8.
    Push and Pop Push Addan element to the top of the stack Pop Remove the element at the top of the stack empty stack top top push an element A A A B push another pop top top
  • 9.
    Implementation of Stacks Any list implementation could be used to implement a stack  Arrays (static: the size of stack is given initially)  Linked lists (dynamic: never become full)  We will explore implementations based on array and linked list  Let’s see how to use an array to implement a stack first
  • 10.
    ArrayImplementation of Stack Justlike the array implementation of the List, we also need the array of items where we are going to store the elements of the Stack Aside from this, we also need an object that keeps track of where the last element is located  From this point on, we are going to call it the top  top is simply an integer (very much like the head in the cursor implementation of the List)
  • 11.
    Our Stack classshould look very much like this: const MAX = 100; class Stack{ private: int top, items[MAX]; public: Stack(); bool push(int); bool pop(); int peek(); //int top(); bool isEmpty(); bool isFull(); void display(); }; Array Implementation of Stack
  • 12.
    The constructor Stack::Stack(){ top =-1; } The full check bool Stack::isFull(){ if(top+1==MAX) return true; return false; } The empty check bool Stack::isEmpty(){ if(top==-1) return true; return false; } Array Implementation of Stack The push bool Stack::push(int x){ if(isFull()) return false; items[++top] = x; return true; } The pop bool Stack::pop(){ if(isEmpty()) return false; top--; return true; }
  • 13.
    10 13 1619 22 top = -1 top = 0 top =1 top =2 top =3 top =4 10 13 16 19 22 top = 0 top =1 top =2 top =3 top =4 Array Implementation of Stack
  • 14.
    Linked-list Implementation ofStack This implementation is the linked-list implementation of the list except for the following operations General insert and append General delete 44 97 23 17 head: tail: 9
  • 15.
    Linked-list Implementation ofStack 44 97 23 17 9 PUSH top: top:
  • 16.
    44 97 2317 top: tmp tmp tmp tmp del 9 Linked-list Implementation of Stack pop
  • 17.
    Queue Overview  QueueDefinition  Basic operations of queue Enqueuing, dequeuing etc.  Implementation of queue  Array  Linked list
  • 18.
    What is Queue The Queue is like the List but with “limited” insertion and deletion.  Insertion can be done only at the end or rear  Deletion can only be done in the front  FIFO – first-in-first-out data structure  Operations enqueue dequeue
  • 19.
    Basic operations ofqueue Primary queue operations: Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear. Enqueue ◦ Insert an element at the rear of the queue Dequeue ◦ Remove an element from the front of the queue Insert (Enqueue)Remove (Dequeue) rearfront
  • 20.
  • 21.
  • 22.
    Storing a queuein a static data structure  This implementation stores the queue in an array.  The array indices at which the head and tail of the queue are currently stored must be maintained.  The head of the queue is not necessarily at index 0. The array can be a “circular array” – the queue “wraps round” if the last index of the array is reached. Example – storing a queue in an array of length 5
  • 23.
    Storing a queuein a dynamic data structure  Each node in a dynamic data structure contains data AND a reference to the next node.  A queue also needs a reference to the head node AND a reference to the tail node.  The following diagram describes the storage of a queue called Queue. Each node consists of data (DataItem) and a reference (NextNode).  The first node is accessed using the name Queue.Head.  Its data is accessed using Queue.Head.DataItem  The second node is accessed using Queue.Head.NextNode  The last node is accessed using Queue.Tail
  • 24.
    Adding a node(Add) in a dynamic data structure  The new node is to be added at the tail of the queue. The reference Queue.Tail should point to the new node, and the NextNode reference of the node previously at the tail of the queue should point to the DataItem of the new node.
  • 25.
    Removing a node(Remove) in a dynamic data structure  The value of Queue.Head.DataItem is returned. A temporary reference Temp is declared and set to point to head node in the queue (Temp = Queue.Head). Queue.Head is then set to point to the second node instead of the top node.  The only reference to the original head node is now Temp and the memory used by this node can then be freed.
  • 26.
    Implementation of Queue Just as stacks can be implemented as arrays or linked lists, so with queues.  Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks
  • 27.
    Array Implementation ofQueue 10 13 16 19 22 size=5size=1 size=2 size=3 size=4 10 13 16 29 22 size=1 size=2 size=3 size=4 size=5
  • 28.
    /*Queue - LinkedList implementation*/ #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node* next; }; // Two l variables to store address of front and rear nodes. struct Node* front = NULL; struct Node* rear = NULL; // To Enqueue an integer void Enqueue(int x) { struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); temp->data =x; temp->next = NULL; if(front == NULL && rear == NULL){ front = rear = temp; return; } rear->next = temp; rear = temp; } queue using linked list in c // To Dequeue an integer. void Dequeue() { struct Node* temp = front; if(front == NULL) { printf("Queue is Emptyn"); return; } if(front == rear) { front = rear = NULL; } else { front = front->next; } free(temp); } int Front() { if(front == NULL) { printf("Queue is emptyn"); return; } return front->data; } void Print() { struct Node* temp = front; while(temp != NULL) { printf("%d ",temp->data); temp = temp->next; } printf("n"); } int main(){ /* Drive code to test the implementation. */ // Printing elements in Queue after each Enqueue or Dequeue Enqueue(2); Print(); Enqueue(4); Print(); Enqueue(6); Print(); Dequeue(); Print(); Enqueue(8); Print(); Implement queue using linked list in c
  • 29.
    Comparing queue implementations •Memoryrequirements – Array-based implementation • Assume a queue (size: 100) of strings (80 bytes each) • Assume indices take 2 bytes • Total memory: (80 bytes x 101 slots) + (2 bytes x 2 indexes) = 8084 bytes – Linked-list-based implementation • Assume pointers take 4 bytes • Total memory per node: 80 bytes + 4 bytes = 84 bytes
  • 30.
  • 31.
    Comparing queue implementations Big-OComparison of Queue Operations Operation Array Implementation Linked Implementation Class constructor O(1) O(1) MakeEmpty O(1) O(N) IsFull O(1) O(1) IsEmpty O(1) O(1) Enqueue O(1) O(1) Dequeue O(1) O(1) Destructor O(1) O(N)