Professional Documents
Culture Documents
Objective
• Stacks
– Representation Of Stacks
– Static Representation
– Dynamic Representation
– Uses Of Stack
• Queues
– Implementation Of Queues
– DeQueue
– Uses Of Queue
• Doubly Linked List
– Implementation and uses
Stack
A stack is a last in, first out (LIFO) abstract data type and data structure.
It is characterized by two fundamental operations: push and pop.
The push operation adds an item to the top of the list, hiding any items already on
the stack, or initializing the stack if it is empty.
The pop operation removes an item from the top of the list, and returns this value to
the caller. A pop either reveals previously concealed items, or results in an empty
list.
Stack Implementation
temp=new Node;
cout<<"enter data for insertion into stack:\n";
cin>>temp->info;
temp->link=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->link=top;
top=temp;
}
cout<<“data item pushed to stack”<<endl;
}
Traversing a Stack
void Stack::traverse()
{
if(top==NULL)
cout<<"Stack is empty:"<<endl;
else
{
temp=top;
cout<<"Stack Elements are:"<<endl;
while(temp!=NULL)
{
cout<<temp->info<<endl;
temp=temp->link;
}
cout<<endl;
}
}
Popping an Item from Stack
void Stack::pop()
{
int data;
if(top==NULL)
{
cout<<"Stack is already empty:\n";
}
else
{
data=top->info;
temp=top;
top=top->link;
delete temp;
cout<<data<<"\t popped successfully!!!!"<<endl;
}
}
Uses Of Stack
• Expression evaluation
• Backtracking (game playing, finding paths)
• Memory management.
Queues
It is a particular kind of collection in which the entities in the collection are kept in
order.
The principal operations on the collection are the addition of entities to the rear
terminal position and removal of entities from the front terminal position (First In
First Out - FIFO).
Queues
Queues are common in computer programs, where they are implemented as data
structures coupled with access routines, as an abstract data structure or in object-
oriented languages as classes
Queue Implementation
struct LinkedList
{
int info;
struct LinkedList *link;
};
typedef struct LinkedList Node;
Queue Implementation
class Queue {
Node *front;
Node *rear;
Node *temp;
public :
Queue() {
front=NULL;
rear=NULL;
}
void enqueue();
void dequeue();
void traverse();
~Queue() {
while(front!=NULL) {
temp=front;
front=front->link;
delete temp;
}
}
};
Enqueue a Node
void Queue::enqueue()
{
temp=new Node;
cout<<"Enter data to insert\n"<<endl;
cin>>temp->info;
temp->link=NULL;
if(front==NULL)
{
front=temp;
rear =temp;
}
else
{
rear->link=temp;
rear=temp;
}
}
Traversing a Queue
void Queue::traverse()
{
temp=front;
if(temp==NULL)
{
cout<<"\nQueue is Empty."<<endl;
exit(1);
}
cout<<"Queue Items are:\n"<<endl;
while(temp!=NULL)
{
cout<<temp->info<<" ";
temp=temp->link ;
}
cout<<endl;
}
Dequeue a Node
void Queue::dequeue()
{
temp=front;
if(front==NULL)
cout<<"\nQueue is Empty"<<endl;
else
{
if(front==rear)
{
cout<<front->info<<" dequeued from the queue.\n"<<endl;
front=NULL;
rear=NULL;
}
else
{
cout<<front->info<<" dequeued from the queue.\n"<<endl;
front=front->link;
}
delete temp;
}
}
Circular Queue
Circular Queue
Standard queue data structure re-buffering problem occurs for each dequeue
operation. To solve this problem by joining the front and rear ends of a queue to
make the queue as a circular queue.
In circular queue the last node is connected back to the first node to make a circle.
Circular linked list follow the First In First Out principle
Elements are added at the rear end and the elements are deleted at front end of the
queue
Both the front and the rear pointers points to the beginning of the array.It is also
called as “Ring buffer”.
Circular Queue can be created in three ways they are.