Data Structures
Data Structures
STACKS:
A stack is a linear data structure in which the elements are processed in LIFO manner (Last In
First Out)which means the element which is inserted last will be the first one to remove.
The insertions and deletions in a stack can be performed at only one end. That end is called
“TOP of thestack”.
The following are the list of operations that can be performed on a stack.
a. Push : The insertion operation is called as push operation.
b. Pop : The deletion operation is called as pop operation.
c. Traverse : visiting each and every data item of a stack is called traverse operation.
A stack is a linear list of data items which are processed as last in first out mechanism. Which
means the element which was pushed last will be the first one to pop out.
The following picture illustrates a stack.
OVERFLOW: Inserting an element in an already filled stack is called overflow situation. Before
insertingan element into the stack, we need to take care about the overflow situation.
UNDERFLOW: The Pop operation with an empty stack is called as underflow situation. So,
before poppingan element from the stack, we need to take care about the underflow situation.
STACK OPERATIONS
Stacks can be implemented by using following two ways:
1. Stacks using arrays.
2. Stacks using linked list.
1. STACK USING ARRAYS: The stacks are very easily implemented with the help of arrays. Here
we have two operations, push() and pop().
Step-2 : Exit
2. Pop( )
Since 3 ! = -1, so top = top – 1; display
element to beout is 40. Poppedelement is : 40
3. Pop( )
Since 2 ! = -1, so top = top – 1; display
element to beout is 30. Poppedelement is : 30
4. Pop( )
Since 1 ! = -1, so top = top – 1; display
element to beout is 20. Poppedelement is : 20
5. Pop( )
Since 0 ! = -1, so top = top – 1; display
element to beout is 10. Poppedelement is : 10
6. Pop( ) Since 0 > = 5, so top = top – 1 Stack is
underflow.
IMPLEMENTATION
1. Read an element into item since top = = null
2. Item=10, create a node temp.
3. Set temp.data=item, temp.link=null
IMPLEMENTATION:
Consider the following stack as Example.
item=30
2. Since top != null , item=top.data = 20 and top=top.link
item=20
3. Since top != null , item=top.data = 10 and top = top.link.
4. Since top=null “stack is under flow”.
A Queue is a linear list of data items, which are processed as FIFO mechanism, which means the
elementsENQued will be first to be DNDued. The following picture illustrate a Queue.
OVERFLOW: Inserting an extra element in an already filled Queue is called Overflow. Before
inserting an elementinto a Queue, we need to take care about the overflow.
UNDERFLOW: The deletion operation with an empty Queue is called Underflow. So, before deleting
an element fromthe Queue, we need to take care about the underflow situation.
5. Q[rear]=item
6. Since, (rear+1)=1>=5 is false
7. Item=20
8. Since rear != -1 rear = rear + 1, Q[rear]=20
Types of Queues:
There are several types of Queues, each one is used in different type of application. The following
are thedifferent types of Queues.
1. Circular Queues
2. Priority Queues
3. Double Ended Queues (DEQueues)
1. CIRCULAR QUEUES:
Let us consider a situation in ordinary Queue that the max-size of the Queue is 5 and 5
insertions arealready performed and 2 deletions are performed. After performing deletions the
Queue is as follows:
One more insertion operation is not permitted in the above Queue because the rear
reaches the max-size of theQueue even though there is a space left for 2 elements before front.
In order to overcome such a kind of problem, we implement Queue as a circular one. In a
circular Queuethere is no beginning and there is no ending. If the rear reaches end of Queue, it
move towards front and the space utilized properly.
The circular Queues can also be implemented in two ways.
1. Using Arrays
1. Insertion( ) 2. Insertion( )
Item=10 Item=20
3. Insertion( ) 4. Insertion( )
Item=30 Item=40
5. Insertion( )
6. Deletion( )
Item=50
7. Deletion( ) 8. Deletion( )
A dequeue is a double-ended queue. You can insert items at either end and delete them
from either end. The methods might be called insertLeft() and insertRight(), and removeLeft() and
removeRight(). If you restrict yourself to insertLeft() and removeLeft() (or their equivalents on the
right), then the deque acts like a stack. If you restrict yourself to insertLeft() and removeRight()
(or the opposite pair), then it acts like a queue.
A dequeue provides a more versatile data structure than either a stack or a queue, and is
sometimes used in container class libraries to serve both purposes. However, it's not used as
often as stacks and queues, so we won't explore it further here.
PRIORITY QUEUES
A priority queue is a more specialized data structure than a stack or a queue. However, it's
a useful tool in a surprising number of situations. Like an ordinary queue, a priority queue has a
front and a rear, and items are removed from the front. However, in a priority queue, items are
ordered by key value, so that the item with the lowest key (or in some implementations the
highest key) is always at the front. Items are inserted in the proper position to maintain the order.
Here's how the mail sorting analogy applies to a priority queue. Every time the postman
hands you a letter, you insert it into your pile of pending letters according to its priority. If it must
be answered immediately (the phone company is about to disconnect your modem line), it goes
on top, while if it can wait for a leisurely answer (a letter from your Aunt Mabel), it goes on the
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t ");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter the Choice:1
Enter a value to be pushed:12
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*top;
if (top == NULL)
{
top = temp;
top->next = NULL;
}
else
{
temp->next = top;
top = temp;
}
}
/* Pop Operation: Removes Top Element of the Stack */
void pop()
{
struct node *temp;
if (isEmpty(top))
{
printf("\nStack is Empty\n");
return;
}
else
{
temp = top;
top = top->next;
printf("Removed Element : %d\n", temp->data);
free(temp);
}
}
/* Prints the linked list representation of a stack */
void printStack(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
printf("\n");
}
Output :
Stack Size : 4
Top Element : 4
Stack as linked List
4-->3-->2-->1
Removed Element : 4
Removed Element : 3
Removed Element : 2
Removed Element : 1
Stack is Empty
3. Write a Program on Queue using Arrays
#include<stdio.h>
#define n= 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
OUTPUT:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:10
Enter the Choice:1
Enter no 2:54
Enter the Choice:1
Enter no 3:98
Enter the Choice:1
Enter no 4:234
Enter the Choice:3
Queue Elements are:
10
54
98
234
Enter the Choice:2
Deleted Element is 10
Enter the Choice:3
Queue Elements are:
54
98
234
Enter the Choice:4
4. Write a program on Queue using linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *front, *back;
if (back == NULL)
{
front = back = temp;
}
else
{
back->next = temp;
back = temp;
}
}
/* Removes an element from front of the queue */
void dequeue()
{
struct node *temp;
if (front == NULL)
{
printf("\nQueue is Empty \n");
return;
}
else
{
temp = front;
front = front->next;
if(front == NULL){
back = NULL;
}
printf("Removed Element : %d\n", temp->data);
free(temp);
}
}
/* Print's Queue */
void printQueue()
{
struct node *temp = front;
if ((front == NULL) && (back == NULL))
printf("Queue is Empty\n");
return;
}
while (temp != NULL)
{
printf("%d", temp->data);
temp = temp->next;
if(temp != NULL)
printf("-->");
}
}
int main()
{
/* Initializing Queue */
initialize();
/* Adding elements in Queue */
enqueue(1);
enqueue(3);
enqueue(7);
enqueue(5);
enqueue(10);
/* Printing Queue */
printQueue();
/* Printing size of Queue */
printf("\nSize of Queue : %d\n", getQueueSize());
/* Printing front and rear element of Queue */
printf("Front Element : %d\n", getFrontElement());
printf("Rear Element : %d\n", getBackElement());
/* Removing Elementd from Queue */
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
return 0;
}
Output :
1-->3-->7-->5-->10
Size of Queue : 5
Front Element : 1
Rear Element : 10
Removed Element : 1
Removed Element : 3
Removed Element : 7
Removed Element : 5
Removed Element : 10
Queue is Empty
5. Write a program on circular queues
#include <stdio.h>
# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}
// function to delete the element from the queue
int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x; // variables declaration
switch(choice)
{
case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
}
6. Write a program on circular queuelist using linked list
#include <stdio.h>
// Declaration of struct type node
struct node
{
int data;
struct node *next;
};
struct node *front=-1;
struct node *rear=-1;
// function to insert the element in the Queue
void enqueue(int x)
{
struct node *newnode; // declaration of pointer of struct node type.
newnode=(struct node *)malloc(sizeof(struct node));
// allocating the memory to the newnode
newnode->data=x;
newnode->next=0;
if(rear==-1) // checking whether the Queue is empty or not.
{
front=rear=newnode;
rear->next=front;
}
else
{
rear->next=newnode;
rear=newnode;
rear->next=front;
}
}
// function to delete the element from the queue
void dequeue()
{
struct node *temp; // declaration of pointer of node type
temp=front;
if((front==-1)&&(rear==-1)) // checking whether the queue is empty or not
{
printf("\nQueue is empty");
}
else if(front==rear) // checking whether the single element is left in the queue
{
front=rear=-1;
free(temp);
}
else
{
front=front->next;
rear->next=front;
free(temp);
}
}
// function to get the front of the queue
int peek()
{
if((front==-1) &&(rear==-1))
{
printf("\nQueue is empty");
}
else
{
printf("\nThe front element is %d", front->data);
}
}
// function to display all the elements of the queue
void display()
{
struct node *temp;
temp=front;
printf("\n The elements in a Queue are : ");
if((front==-1) && (rear==-1))
{
printf("Queue is empty");
}
else
{
while(temp->next!=front)
{
printf("%d,", temp->data);
temp=temp->next;
}
printf("%d", temp->data);
}
}
void main()
{
enqueue(34);
enqueue(10);
enqueue(23);
display();
dequeue();
peek();
}
Output :