DS Stack
DS Stack
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:
• Student Work
• Write a C program to implement decimal to binary conversion using stack.
String Reverse
• String1 = “abcde” string2 Reverse of String1 “edcba”
• Procedure
• 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