0% found this document useful (0 votes)
21 views34 pages

DS Unit2

Uploaded by

spidyyy56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views34 pages

DS Unit2

Uploaded by

spidyyy56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Unit -2

 Stack is a linear data structure in which insertion of new


element or deletion of an existing elements always takes place
at the same end, called Top.
 When item is added to stack, the operation called push.
 When item is removed from stack, the operation called pop.
 Stack follows last in first out order.
top
top 11
8 8
top Representation of stack after
Top=null 4 4 4 Inserting element
2 2 2

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

 Prefix: The prefix notation places the operator before


the operands. Ex. +AB, -CD

 Postfix: The postfix notation places the operator after


the operands. Ex. AB+, CD-
 Convert infix expression to Postfix expression
Ex. A+B*C+D*E

Symbol stack Postfix


A A
+ + A
B + AB
* +* AB
C +* ABC
+ + ABC*+
D + ABC*+D
* +* ABC*+D
E +* ABC*+DE
ABC*+DE*+

The Postfix expression is : ABC*+DE*+


 Ex . (P + Q) – R * ( S / T )
Symbol Stack Postfix
( (
P ( P
+ (+ P
Q (+ PQ
) PQ+
- - PQ+
R - PQ+R
* -* PQ+R
( -*( PQ+R
S -*( PQ+RS
/ -*(/ PQ+RS
T -*(/ PQ+RST
) -* PQ+RST/
PQ+RST/*-

The Postfix expression is : PQ+RST/*-


 Convert Infix expression to Prefix expression
Ex. A+B*C+D*E
Reverse of expression : E*D+C*B+A
Symbol Stack Prefix
E E
* * E
D ED
+ + ED*
C + ED*C
* +* ED*C
B +* ED*CB
+ ++ ED*CB*
A ++ ED*CB*A
ED*CB*A++

The Prefix expression is: ++A*BC*DE


 Ex. (P + Q) – R * ( S / T )
Reverse of expression: ) T / S ( * R - ) Q + P (
Symbol Stack Prefix
) )
The prefix
T ) T
expression is :
/ )/ T -+PQ*R/ST
S )/ TS
( TS/
* * TS/
R * TS/R
- - TS/R*
) -) TS/R*
Q -) TS/R*Q
+ -)+ TS/R*Q
P -)+ TS/R*QP
( - TS/R*QP+
TS/R*QP+-
 Expression Evaluation
ex. Evaluate given postfix expression : 2 5 7 * + 9 –

Postfix operation Stack


expression
2 2
5 2, 5
7 2, 5, 7
* 5*7=35 2, 35
+ 2+35=37 37
9 37, 9
- 37-9 28

The expression evaluation is 28


 Queue is a linear data structure that permits insertion of
new element at one end and deletion of an element at
the other end.
 The end at which the deletion of an element takes place
is called front.
 And the end at which the insertion of an element takes
place is called rear.
 The first element that gets added into the queue is the
first one to get removed from the list, hence queue is
also referred to first-in-first-out list.
front rear
 Operations on queue:
1. Enqueue()
2. Dequeue()
3. Peek()
4. Isfull()
5. isempty()
 Queue size=5
 Insert 10
front 10
rear
 Insert 20
front 10 20
rear
 Insert 15
front 10 20 15
rear
 Insert 2
front 10 20 15 2
rear
 Queue, being a linear data structure can be represented in
various ways such as :
1. Arrays
2. Linked list

Representation of queue using array:


An array is a data structure that can store a fixed number
of elements. The size of an array should be fixed before
using it.
Queue, on other hand keeps on chaining as we remove
elements from the front end or add new element at the
rear end.
 Declaring an array with a maximum size would solve
this problem
 The maximum size should be large enough for a queue
to expand or shrink.

Arr[0] Arr[1] Arr[2] Arr[3] Arr[4] Arr[5] Arr[6] Arr[7]

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.

You might also like