0% found this document useful (0 votes)
11 views31 pages

DS Stack

Uploaded by

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

DS Stack

Uploaded by

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

Stack Data Structure

Fundamentals of Stacks
• Data Structures is nothing but arrangement of data in a particular order.
• Different ways in which we can arrange the data, such as Stack, Queue,
Trees, Graph, etc.
• Data structures are independent of Language i.e. they can be
implemented in any language such as C, C++, Java, etc.
• Stack is a one-way of representation of data, in which data is inserted and
removed from the same end.
• Stack is referred as Last In First Out (LIFO)/ First In Last Out (FILO)
• Stack is a one ended array.
• Examples of stack are stack of coins on table, Stack of books on table i.e.
one on top of another, Stack of chairs, etc.
• The topmost book or chair is removed first and bottom most is removed at
last.
Fundamentals of Stacks
• Primitive operations of stack are
• PUSH :
Insertion of an element at Top position
• POP :
Deletion of an element from Top Position
• StackEmpty :
When Top == -1 then Stack is Empty
• StackFull :
When Top == Max-1 then Stack is Full
• Display :
Contents of stack should be displayed till Top position of the
stack
Applications of Stacks
• Applications of Stacks are all those areas where LIFO/FILO concept is
applied. These applications are:

• Decimal to Binary conversion


• String Reverse
• Tower of Hanoi problem
• Parenthesis validity
• Functions and recursive functions
• Expression Conversions
• Expression Evaluations
Practice Session

practice
•.
practice
•.
practice
•.
Decimal to Binary conversion
• (10) in Decimal  ( ? ) in Binary
• Procedure
• Divide Decimal no. by 2 to get new quotient
• Push Remainder in stack
• Repeat above two steps till quotient becomes smaller than two.
• Then go on popping the stack and collect all the elements in an array
• Display this array as Binary equivalent of given Decimal number.

• Student Work
• Write a C program to implement decimal to binary conversion using stack.
String Reverse
• String1 = “abcde” string2  Reverse of String1  “edcba”
• Procedure

• Accept a string1 from user


• Go on pushing all the characters till ‘\0’ character
• Once ‘\0’ character reached then
• Go on popping the stack and place the popped character in String2 until
stack becomes empty.
• Once stack becomes empty then place ‘\0’ character at the end of String2.

• Student Work
• Write a C program to implement user defined string reverse function
using stack.
Parenthesis Validity
• String 1: (({])}  Invalid String2 : {()}  Valid

• Procedure
• Accept the string of Parenthesis from user
• While (string[i] !=‘\0’)
{ if (string[i]== opening bracket)
Push (string[i])
else // Closing bracket
{ ch=pop();
if(popped char == matching pair of string[i])
continue
else
print Invalid string of parenthesis
}
} print valid string of parenthesis

• Student Work
• Write a C program to check the parenthesis validity using stack.
Expression conversion
• Stacks can be used to form different expression conversions.
•There are three types of Expression representations
• Infix  A+B
• Postfix  AB+
• Prefix  +AB

• We can convert
Infix to equivalent Postfix or Prefix form using stack
Postfix or Prefix to its equivalent Infix form using stack
Postfix to Prefix or Prefix to Postfix without Infix form using stack

• We can perform Postfix and Prefix evaluation using stack


Practice session
Infix to Postfix conversion
• Procedure
• Declare a stack which can hold the operators.
• Scan the infix expression.
• All the operands are directly added to the postfix string.
• The operators are pushed on to the stack by comparing their priorities.
• The priorities of the operator being read from the infix expression (in
coming priority, icp) is compared with the priority of the operator on the
stacktop ( in stack priority, isp).
• If (isp>=icp) operators are popped from the stack and written to the
postfix string, else they are pushed on the stack.
• The ‘(‘ symbol is always pushed on the stack without checking priority and
when ‘)’ is read, all the operators are popped and written to the postfix
expression till the matching ‘(‘ is popped out and discarded.
• The above process continues till the infix expression is completely read.
• At the end, pop all the operators from stack and append it to the postfix
expression.
Infix to Postfix conversion
• Pseudo code
While (Infix[i] !=’\0’)
{ if(Operand(infix[i])) Then Postfix[j++] = Infix[i]
else // operator
{ if(stackEmpty()) Then Push (Infix[i])
else
{ while(Priority(StackTop() >= Priority(Infix[i])
Postfix[j++] = Pop();
Push(Infix[i])
}
} Example
} // end of while A+B*C  ABC*+
While(!StackEmpty())
Postfix[j++]=pop();
Postfix[j] = ‘\0’
Evaluation of Postfix
• Procedure
• Accept Postfix expression from user
• Scan the postfix expression.
• Accept the numerical values of all the operands from the user.
• When ever we encounter operand, accept its numerical value from user
and push it on a stack
• If operator is encountered, then pop two operands from the stack and
perform the operation with the operator read from postfix expression.
• Push the calculated value back to the stack.
• Continue to perform above steps till we reach the end of the postfix
expression.
• The value on the stack that remains at the end is the result of the
evaluation of the postfix expression.
Evaluation of Postfix
• Pseudo code
While (Postfix[i] !=’\0’)
{ if(Operand(postfix[i]))
Accept its value and Push it on stack
else // operator
{ // Pop two terms from stack as
term2=Pop()
term1=Pop()
Ans = Eval(term1,term2, Postfix[i])
Push(Ans)
}
} // end of while Example:
Final Ans= Pop() ABC*+  1 2 3 * +  7
Postfix to Infix Conversion
• Pseudo code
While (Postfix[i] !=’\0’)
{ if(Operand(postfix[i]))
Convert Postfix[i] into a string and Push it on stack
else // operator
{ // Pop two terms from stack as
strcpy(term2,Pop())
strcpy(term1,Pop())
Ans = Eval(term1,term2, Postfix[i]) // to form Infix term
Push(Ans)
} Example
} // end of while ABC*+  A+B*C
Strcpy (Final_Ans, Pop())
Practice session
Infix to Prefix Conversion
• Pseudo code
Reverse Infix string as Rev_In
While (Rev_in[i] !=’\0’)
{ if(Operand(Rev_In[i])) Then Prefix[j++] = Rev_In [i]
else // operator
{ if(stackEmpty()) Then Push(Rev_In[i])
else
{ while(Priority(StackTop() > Priority(Rev_In[i])
Prefix[j++] = Pop();
Push(Rev_In[i]) }
} Example
} // end of while A+B*C  +A*BC
While(!StackEmpty())
{ Prefix[j++]=pop(); } Prefix[j] = ‘\0’
• Display reverse of Prefix array to get correct Prefix expression
Evaluation of Prefix
• Pseudo code
• Reverse Prefix string and then
While (Prefix[i] !=’\0’)
{ if(Operand(prefix[i]))
Accept its value and Push it on stack
else // operator
{ // Pop two terms from stack as
term1=Pop()
term2=Pop()
Ans = Eval(term1,term2, Prefix[i])
Push(Ans)
}
} // end of while Example:
Final Ans= Pop() +*ABC+*123 5
Prefix to Infix Conversion
• Pseudo code
Reverse Prefix string and then
While (Prefix[i] !=’\0’)
{ if(Operand(Prefix[i]))
Convert Prefix[i] into a string and Push it on stack
else // operator
{ // Pop two terms from stack as
Strcpy(term1,Pop())
Strcpy(term2,Pop())
Ans = Eval(term1,term2, Prefix[i]) // to form Infix term
Push(Ans)
} Example
} // end of while +*ABC  A*B+C
Strcpy (Final_Ans, Pop())
Student Work
• Write a C program to convert Postfix to Prefix using stack, without
creation of Infix.

• Write a C program to convert Prefix to Postfix using stack, without


creation of Infix.

Convert following Expressions in other two forms:


1.(A+B)*(C-(D+E)/F)
2.AB*C/DE-F/+
3.+*A-BC/D-EF
4.ABC/*DE-F$+
5.A/(B*C)+(D-E)/F
6.ABC$/DE*+AC*-
Quiz
1. Stack is based on basic concept of ____________.
a. FIFO b. FILO c. LILO d. None of these
2. The postfix form of the expression (A+ B)*(C*D- E)*F / G is?
a) AB+ CD*E - FG /** b) AB + CD* E - F **G /
c) AB + CD* E - *F *G / d) AB + CDE * - * F *G /
3. The postfix form of A*B+C/D is?
a) *AB/CD+ b) AB*CD/+ c) A*BC+/D d) ABCD+/*
4. Which data structure is used for implementing recursion?
a) Queue b) Stack c) Array d) List
5. Stack is a data structure in which all insertion and deletions are made at:
a) any position b) Both the ends c) One end d) in the middle
6. The operation to add element in stack is called as _______
a) Add b) Append c) Push d) Insert
7. Stack data structure can not be used for______
a)Recursive Functions b) Palindrome of string
c) comparison of string d) None of the above
Revision Questions
1. Explain the use of data structure stack in recursive procedure using a
suitable example.
2. Clearly indicate the content of stack during the evaluation of the
following prefix expression: * * a / b c d
3. Convert the following infix expression into postfix expression :
a. ((a+b)-(c*d))/(ce/(f^2)) b. (a+b)*d+e/(f+a*d)+c
4. Write a C program to implement following operations using stack.
A. Factorial of a given number B. Generation of Fibonacci series
5. Write a C program to implement following string operations using
stack.
A. Palindrome of a String B. String Comparison
6. Write a C program to implement following string operations using
stack.
A. String length B. String Concate
7. Write a C program to implement binary search using stack.
Practice session
Practice session
Practice session
Practice session
Practice session
Thank You

You might also like