You are on page 1of 21

Stack, Queue & Doubly Linked List

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

 A stack can be easily implemented either through an array or a linked


list.

 The array implementation aims to create an array where the zero-offset


position is the bottom. The program keeps track of the size, or the length
of the stack.

 A stack linked-list is much simpler than most linked-list


implementations.It requires that we implement a linked-list where only
the head node or element can be removed, or popped, and a node can
only be inserted by becoming the new head node.
Stack Implementation
struct LinkedList
{
int info;
struct LinkedList *link;
};
typedef struct LinkedList Node;
Stack Implementation
class Stack {
Node *top;
Node *temp;
public:
Stack() {
top=NULL;
}
void push();
void pop();
void traverse();
~Stack() {
while(top!=NULL){
temp=top;
top=top->link;
delete temp;
}
}
};
Pushing an Item to Stack
void Stack::push()
{

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 provide services in computer science, transport and operations research


where various entities such as data, objects, persons, or events are stored and held to
be processed later.

 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.

1. Using single linked list


2. Using double linked list
3. Using arrays
Doubly Linked List
Doubly Link List
 Doubly-linked list is a linked data structure that consists of a set of sequentially
linked records called nodes. Each node contains two fields, called links, that are
references to the previous and to the next node in the sequence of nodes.
 The beginning and ending nodes' previous and next links, respectively, point to
some kind of terminator, typically a sentinel node or NULL, to facilitate traversal of
the list.
 If there is only one sentinel node, then the list is circularly linked via the sentinel
node. It can be conceptualized as two singly linked lists formed from the same data
items, but in opposite sequential orders.
 A linked list whose nodes contain three fields: a data value, the link to the next
node, and the link to the previous node. The two node links allow traversal of the
list in either direction.
 While adding or removing a node in a doubly-linked list requires changing more
links than the same operations on a singly linked list, the operations are simpler and
potentially more efficient (for nodes other than first nodes) because there is no need
to keep track of the previous node during traversal or no need to traverse the list to
find the previous node, so that its link can be modified.

You might also like