The document discusses stacks and queues. It defines stacks as LIFO data structures and queues as FIFO data structures. It describes basic stack operations like push and pop and basic queue operations like enqueue and dequeue. It then discusses implementing stacks and queues using arrays and linked lists, outlining the key operations and memory requirements for each implementation.
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
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
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