Updated Ds - Unit II
Updated Ds - Unit II
( 9EC01)
UNIT - II
Topics to be covered in Unit-2:
Introduction to Data Structures
Abstract data type (ADT)
Stacks, Queues and Circular Queues and their
implementation with arrays
Stack Applications:
Infix to Post fix conversion
Postfix expression evaluation.
Applications of Queues
Introduction to Data Structures:
Data can be represented in the form of binary digits in memory.
A binary digit can be stored using the basic unit of data called bit.
A bit can represent either a zero or a one.
Type
That is an Abstract Data Type (ADT) defines data together with the
operations.
ADT depicts the basic nature or concept of the data structure rather
than the implementation details of the data.
struct stack
{
int top, size;
};
void push();
void pop();
void display();
Stack
A stack is a Last In First Out (LIFO) data structure in which all
insertions and deletions are takes place at one end, called the top.
Stack maintains a variable called top, which keeps track of the top most
element in the stack.
In the stack, the elements are removed in the reverse order of that in
which they were added to the stack that is last element inserted is to be
deleted first.
Basic Stack Operations:
push/insert : To insert an item into stack
pop/delete : To delete an item from stack
traverse/display : To display an items
isFull : To know whether the stack is full or not
isEmpty : To know whether the stack is empty or not
A stack of cups
A stack of coins
Example 1:
When the elements are inserted in the order as A,B,C,D then the size
of the stack is 4 and the top most element in the stack is D.
When deletion operation is performed on stack continuously then the
order in which the elements are deleted is D,C,B,A.
D to
C to C p
B to B p B
A to A p A
p A
C to
p B to
B
p
A A A top
Example 2: Here Stack Size is
4
3 3 3
2 2 2
1 1 TOP-> 1 20
0 TOP-> 0 10 0 10
TOP-> 3 40 TOP-> 3 40
3
TOP-> 2 2 30 2 30
30
1 20 1 20
1 20
0 10 0 10
0 10
Push 40 Push 50
Push 30
(Overflow)
3 3
TOP->2 30 2
1 20 TOP-> 1 20
0 10 0 10
pop pop
3 3
2
2
1 1
TOP-> 0 10 0
pop (TOP = -1)
pop underflow
Example:3
Example 4:
Implementing Stack using Array
Procedure:
Initialize the stack by assigning -1 to the top variable to indicate that the
array based stack is empty.
A top is an integer value, which contains the array index for the top of
the stack.
If the top value reaches to maximum size of the stack then items cannot
be inserted into the stack i.e. stack is full.
Otherwise top is incremented by one and item is inserted into the stack.
Algorithm for deleting an element from the stack
Algorithm pop()
1. if top = -1 then
print “stack is empty”
2. else
1. item = stack[top]
2. top=top-1
3.endif
4.stop
Take out the element from the location where the top is
pointing and store it in the variable then decrement top by
one.
Algorithm for displaying an elements of the stack
Algorithm:
display()
1. if top = -1
1.print “stack empty”
2. else
1. for(i = top; i >= 0;i--)
1.print “stack[i]”
3.endif
4. stop
void pop() {
if(top==-1)
printf("*** stack is empty ***\n");
else
{
item=stack[top];
top--;
printf("the deleted item from stack is %d\n",item);
}
}//pop
int isEmpty(){
void display()
if(top==-1)
{
int i; printf("Stack is
empty\n");
if(top==-1)
else
printf("**** stack underflow****");
else printf("Stack is not
empty\n");
{
return 0;
printf("*** stack display ***\n");
for(i=top;i>=0;i--) }
{
if(i==top) int isFull()
printf("%d at {
%d ->top\n",stack[i],i); if(top==SIZE-1)
else
printf("Stack is full\
printf("%d at n");
%d\n",stack[i],i);
} else
} printf("Stack is not
full\n");
}
return 0;
}
Applications of Stack
2+3 +2 3 2 3 +
2+3*5 + 2 *3 5 2 3 5 * +
(2 + 3) * 5 * + 2 3 5 2 3 + 5 *
– * 2 3 + 5 2 3 * 5 9 +
2 * 3 – (5 + 9)
9 –
Infix to Postfix (RPN) Conversion
Algorithm to convert infix expression to RPN:
1. Initialize an empty stack.
2. Repeat the following until the end of the infix expression is reached.
1. Get next input token (constant, variable, arithmetic operator, left
parenthesis, right parenthesis) in the infix expression.
2. If the token is
1. A left parenthesis: Push it onto the stack.
2. A right parenthesis:
1. Pop and display stack elements until a left parenthesis is on the
top of the stack.
2. Pop the left parenthesis also, but do not display it.
3. An operator:
1. While the stack is nonempty and token has lower or equal
priority than stack top element, pop and display.
2. Push token onto the stack.
4. An operand: Display it.
3. When the end of the infix expression is reached, pop and display stack
items until the stack is empty.
An Example: 7-(2*3+5)*(8-4/2) 723*5+842/-*-
Remaining Infix String Stack Postfix String
7-(2*3+5)*(8-4/2) empty null
-(2*3+5)*(8-4/2) empty 7
(2*3+5)*(8-4/2) - 7
2*3+5)*(8-4/2) -( 7
*3+5)*(8-4/2) -( 72
3+5)*(8-4/2) -(* 72
+5)*(8-4/2) -(* 723
5)*(8-4/2) -(+ 723*
)*(8-4/2) -(+ 723*5
*(8-4/2) - 723*5+
(8-4/2) -* 723*5+
8-4/2) -*( 723*5+
-4/2) -*( 723*5+8
4/2) -*(- 723*5+8
/2) -*(- 723*5+84
2) -*(-/ 723*5+84
) -*(-/ 723*5+842
null empty 723*5+842/-*-
/*3i).Write a C program that uses stack operations to perform convertion of infix expression
into postfix expression.*/
#include<stdio.h> #include<ctype.h> #include<string.h> static char str[30]; int
top=-1;
int push(char); int pop(); int priority(char);
void main() {
char in[30],post[30],ch; int i,j,l; printf("\n Enter the string(Infix Expression): ");
scanf(“%s”,in); l=strlen(in); printf("\n The length of the given string is: %d\
n",l);
for(i=0,j=0;i<l;i++) {
if(isalpha(in[i]) || isdigit(in[i]))
post[j++]=in[i];
else {
if(in[i]=='(')
push(in[i]);
else
if(in[i]==')') {
while((ch=pop())!='(')
post[j++]=ch;
}
else {
while(priority(in[i])<=priority(str[top]))
post[j++]=pop();
push(in[i]);
}
}
while(top!=-1)
post[j++]=pop();
post[j]='\0';
printf("\nEquivalent infix to postfix is:%s ",post);
}//main
int push(char c) {
str[++top]=c;
return 0;
int priority (char c) { }//push
switch(c) {
case '+':
case '-': return int pop(){
1; return (str[top--]);
case '*': }//pop
case '/': return
2;
case '^': return
3;
}//switch
return 0;
}//priority
Evaluation of Postfix (RPN) Expression
Algorithm to evaluate Postfix Expression:
1. Initialize an empty stack.
2. Do
1. Get next token (constant, arithmetic operator) in RPN
expression.
2. If token is an operand, push it on the stack.
3. If token is an operator do the following:
1. Pop top two values from the stack. (If the stack does not
contain two items, report error.)
2. Apply operator token to these two values.
3. Push the resulting value onto the stack.
3. Until the end of the expression is encountered.
4. The value of the expression is on the top of the stack (and stack
should contain only this value).
Evaluate the postfix expression: 5,6,2,+,*,12,4,/,-
Remaining postfix String Symbol Scanned Stack
5 6 2 + * 12 4 / - no symbol empty
6 2 + * 12 4 / - 5 5
2 + * 12 4 / - 6 5
6+ * 12 4 / - 2 5 6 2
* 12 4 / - + 5 8
12 4 / - * 40
4 / - 12 40 12
/ - 4 40 12 4
- / 40 3
null - 37
/*3ii).Write a C program that uses Stack operations to perform the evaluation of
the postfix expression*/
#include<stdio.h>
#include<math.h>
#include<string.h>
void main() {
char postfix[30],t[30];
int stack[10],top=-1,len,i,j,op2,op1,n;
printf("\n\n\nEnter the postfix:");
scanf(“[^\n]s”,postfix);
len=strlen(postfix);
postfix[len]=‘#’; //Assume # as the last value of postfix expression
for(i=0;postfix[i]!=‘#’;i++)
{
j=0;
if(postfix[i]==‘ ‘)
continue;
if(postfix[i]=='+'||postfix[i]=='-'||postfix[i]=='*'||postfix[i]=='/'||postfix[i]=='^')
{
op2=stack[top--];
op1=stack[top--];
switch(postfix[i]) {
case '+': stack[++top]=op1+op2;
break;
case '-': stack[++top]=op1-op2;
break;
case '*': stack[++top]=op1*op2;
break;
case '/': stack[++top]=op1/op2;
break;
case '^': stack[++top]=pow(op1,op2);
break;
}//end of switch
}//end of if
else {
while(postfix[i]!=’ ‘) {
t[j++]=postfix[i++];
}
t[j]='\0';
n=atoi(t);
stack[++top]=n;
}
}//end of for
printf("\n\n\nResult=%d\n",stack[top]);
}//end of main
Queues
A queue is a First In First Out (FIFO) data structure in which
elements are accessed from two different ends called Front and
Rear. The elements are inserted into a queue through the Rear end
and are removed from the Front end.
In the Queue, the elements are removed in the order of that in
which they were added to the queue, that is first element inserted is
to be deleted first.
Basic Queue Operations:
Bus
Stop
Bus
Stop
Bus
Stop
front rear
rear
Bus Stop Queue
Bus
Stop
front rear
rear
front rear
New people must enter the queue at the
rear.
front
rear
People should leave the queue
from the front end.
rear
front
Types of Queues
Linear Queue
Circular Queue
Double Ended Queue
Priority Queue
Linear Queue: It is the one in which the insertions are takes place at
rear end and deletions are takes place at front end.
Priority Queue: In priority queues, the items added to the queue have a
priority associated with them which determines the order in which
they exit the queue. Items with highest priority are removed first.
Working of a linear queue:
i) Initially front=rear= -1. It indicates queue is empty.
0 1 2 3 4
front=rear= -1
0 1 2 3 4 0 1 2 3 4
10 20 30 40 50 10 20 30 40 50
0 1 2 3 4 0 1 2 3 4
40 50 50
front=rear= -1 front=rear= -1
Implementing Queue using Array
Procedure:
Explanation:
This procedure deletes an element from
Algorithm delete() the queue.
1. if front = -1 then
1. print “queue is The first step of this algorithm checks
empty” for underflow condition.
2. exit
If the front value is -1 then queue is
2. else empty.
1. item =
queue[front] Take out the element from the location
2. front = front + where, the front is pointing and store it
1 in the variable, then increment front by
3. endif one.
4. stop
Algorithm for displaying an elements from the queue
Algorithm display()
1. if front = -1 then
1. print “queue is
empty”
2. exit
2. else
1. for(i = front; i <=
rear; i++)
1. print
“queue[i]”
3. endif
4. stop
If the front value is -1 then the queue is empty.
A circular queue also have a Front and Rear to keep the track of
elements to be deleted and inserted and therefore to maintain the
unique characteristic of the queue.
Examples:
Print spooler of operating system
Revolvers bullet cylinder
Traffic light sequence
Bottle caping system in cold drink factory
Circular Queue with size 5 After adding 5, 10, 20
1. (A-B)*(D/E)
2. (A+B^D)/(E-F)+G
3. A*(B+D)/E-F*(G+H/K)
4. ((A+B)*D)^(E-F)
5. (A-B)/((D+E)*F)
6. ((A+B)/D)^((E-F)*G)
7. 12/(7-3)+2*(1+5)
8. 5+3^2-8/4*3+6
9. 6+2^3+9/3-4*5
10. 6+2^3^2-4*5
Evaluate the postfix expression
1. 5,3,+,2,*,6,9,7,-,/,-
2. 3,5,+,6,4,-,*,4,1,-,2,^,+
3. 3,1,+,2,^,7,4,1,-,2,*,+,5.-,-