DS Unit2
DS Unit2
top
11 top
8 8 top
Representation of stack after
4 4 4 Top=null
Deletion of element
2 2 2
stack contains an order collection of elements.
All the operations regarding the stack are performed
using array. i.e pop, push.
Declare the array with a maximum size to manage a
stack operation.
#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of stack [MAX=100]:");
scanf("%d",&n);
printf("\n\t Stack operation using array");
printf("\n\t---------------------------------");
printf("\n\t 1.push \n\t 2.pop \n\t 3.display \n\t 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("\\t EXIT POINT");
break;
default: printf("\n\t please Enter a valid
choice(1/2/3/4)");
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK if overflow");
}
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 element in STACK \n");
for(i=top;i>=0;i++)
printf("\n%d",stack[i]);
printf("\n press Next choice");
}
Linked list allocates the memory dynamically.
In linked list implementation of stack, the nodes are
maintained non-contiguously in the memory.
Each node contains a pointer to its immediate successor
node in the stack.
Stack is said to be overflow if the space left in the
memory heap is not enough to create a node.
Top data null
data next
data next
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main()
{
int choice=0;
while(choice!=4)
{
printf(\n1.push\n2.pop\n3.display\n4.exit);
}
printf(“\nenter your choice:”);
scanf(“%d”,&choice):
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
V
void push()
{
int val;
struct node *ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf(“not able to push element”);
}
Else
{
printf(“enter value”);
scanf(“%d”,&val);
if(head==NULL)
{
ptr->val=val;
ptr->next=NULL;
head=ptr;
}
}
Printf(“item pushed”);
}
}
void pop()
{
int item;
struct node *ptr;
if(head==NULL)
printf(“stack is empty”);
else
{
item=head->val;
ptr=head;
head=head->next;
free(ptr);
printf(“item popped”);
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
If(ptr==NULL)
printf(“stack is empty”);
Else
{
printf(“printing stack elements”);
while(ptr=NJULL)
{
printf(“%d”,ptr->val);
ptr=ptr->next;
}
}
Notation:
Infix :The infix notation is a convenient way of writing
an expression in which each operator is placed between
the operands. Ex. A+B, (C-D) etc
34 12 53 61 9 23 8 42
front rear
Queue can also be represented using linked list.
Linked list do not have any restrictions on the number
of elements it can hold.
Space for the elements in linked list is allocated
dynamically, hence it can grow as long as there is
enough memory available for dynamic allocation.
32 12 53 20 61 N
front rear
The queue that we implement using an array suffers
from one limitation.
To overcome this limitation we can implement the
circular queue.
Circular queue is a special version of queue where the
last element of the queue is connected to the first
element of the queue forming a circle.
10 33 61 20 82
front rear
operations perform on a circular queue::
1. Front: it is used to get the front element from the
queue.
2. Rear: it is used to get the rear element from the
queue.
3. Enqueue(): this function is used to insert the new
value in the queue. The new element is always
inserted from the rear end.
4. Dequeue(): this function deletes an element from the
queue. The deletion in a queue always takes place
from the front end.
Applications of circular queue:
1. Memory management:: the circular queue provides
memory management. The memory is managed
efficiently by placing the elements in a location which is
unused.
2. CPU scheduling:: the operation system also uses the
circular queue to insert the processes and the execute
them.
3. Traffic system:: in a computer control traffic system,
traffic light is one of the best example of circular queue,
each light gets on one by one after every interval of time.
Deque is a data structure in which items can be inserted
or deleted at either the front or rear end, but no changes
can be made elsewhere in the list.
Thus it does not follow first-in-first-out (FIFO) rule.
Deque is a generalization of both stack and queue.
Insertion
Insertion
deletion
deletion
front rear
Types of double ended queue:
1. Input restricted deque: An input restricted deque
restricts the insertion of elements at one end only, but
the deletion of elements can be done at both the ends
of queue.
Insertion
deletion deletion
front rear
2.Output restricted deque: An output restricted deque,
restricts the deletion of elements at one end only, and
allows insertion to be done at both ends.
Insertion
insertion deletion
front rear
a priority queue is a collection of elements where the
elements are sorted according to their priority levels.
The order in which the elements should get inserted or
deleted is decided by the priority of the element.
Properties of priority queue:
1. Every item has a priority associated with it.
2. An element with higher priority is dequeued before
an element with low priority.
3. If two elements have the same priority, they are
served according to their order in queue.
Types of priority queue:
1. Ascending order priority queue:: in ascending order
priority queue, the elements with a lower priority
value is given a higher priority in the priority list.
2. Descending order priority queue: in descending order
priority queue, higher priority value is given as a
higher priority.