0% found this document useful (0 votes)
37 views

The Stack

The document describes stacks, which are linear data structures that follow the LIFO (last-in, first-out) principle. Stacks only allow two operations - push, which adds an element to the top, and pop, which removes an element from the top. Common applications of stacks include balancing symbols in expressions, postfix calculators, and converting infix notation to postfix notation.

Uploaded by

Nouman Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

The Stack

The document describes stacks, which are linear data structures that follow the LIFO (last-in, first-out) principle. Stacks only allow two operations - push, which adds an element to the top, and pop, which removes an element from the top. Common applications of stacks include balancing symbols in expressions, postfix calculators, and converting infix notation to postfix notation.

Uploaded by

Nouman Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

The Stack

CS221(A) – Data Structure &


Algorithms
Stack
• An Ordered list in which only two operations
are permissible:
– Insertion
– Deletion
• A List (ADT) with restriction that insertions
and deletions can be performed at only one
position i.e. the end of the list called top.
• Items are stored and retrieved in a last-in, first
–out (LIFO) manner.
Stack
• Fundamental operations on a stack are Push,
which is equivalent to insert & Pop which is
equivalent to deletion.
Push (S, A)

top
Stack
• Fundamental operations on a stack are Push,
which is equivalent to insert & Pop which is
equivalent to deletion.
Push (S, A)
Push (S, B)

top
Stack
• Fundamental operations on a stack are Push,
which is equivalent to insert & Pop which is
equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)

top
Stack
• Fundamental operations on a stack are Push,
which is equivalent to insert & Pop which is
equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D) top
Stack
• Fundamental operations on a stack are Push,
which is equivalent to insert & Pop which is
equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D)
Pop ()
top
Stack
• Fundamental operations on a stack are Push,
which is equivalent to insert & Pop which is
equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D)
Pop ()
Pop ()
top
Stack
• Fundamental operations on a stack are Push,
which is equivalent to insert & Pop which is
equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D)
Pop ()
Pop ()
Pop ()
top
Stack
• Fundamental operations on a stack are Push,
which is equivalent to insert & Pop which is
equivalent to deletion.
Push (S, A)
Push (S, B)
Push (S, C)
Push (S, D) top
Pop ()
Pop ()
Pop ()
Pop ()
Stack
• Push by inserting at the front of the list.
• Pop by deleting the element at the front of
the list.
• Top examines the element at the front of the
list and returns its value.
Stack Operations
• struct Node
• {
– ElementType Element;
– Struct Node *Next;
• }
Stack Operations
• struct Node;
• typedef struct Node *PtrToNode;
• typedef PtrToNode Stack; // S
Push Operation

S
Push Operation

TmpCell

PtrToNode TmpCell = malloc (sizeof(struct Node));


TmpCell  Element = “A”;
Push Operation

TmpCell

PtrToNode TmpCell = malloc (sizeof(struct Node));


TmpCell  Element = “A”;
TmpCell  Next = S  Next;
S  Next = TmpCell;
Push Operation

TmpCell
PtrToNode TmpCell = malloc (sizeof(struct Node));
TmpCell  Element = “B”;
Push Operation

PtrToNode TmpCell = malloc (sizeof(struct Node));


TmpCell
TmpCell  Element = “B”;
TmpCell  Next = S  Next;
Push Operation

TmpCell
PtrToNode TmpCell = malloc (sizeof(struct Node));
TmpCell  Element = “B”;
TmpCell  Next = S  Next;
S  Next = TmpCell;
Push Operation
• void Push(ElementType X, Stack S)
• {
– PtrToNode TmpCell = malloc(sizeof(struct Node));
– if (TmpCell == NULL)
– {
• FatalError (“Out of Space!”);
– }
– else
– {
• TmpCellElement = X;
• TmpCellNext = SNext;
• SNext = TmpCell;
– }
• }
Stack Operations
• int IsEmpty(Stack S)
• {
– return SNext == NULL;
• }
Stack Operations
• ElementType Top(Stack S)
• {
– if (!IsEmpty(S))
–{
• return SNextElement;
–}
– return 0;
• }
Stack Operations
• void Pop(Stack S)
• {
– PtrToNode FirstCell;
– if (!IsEmpty(S))
– {
• Error(“Empty Stack”);
– }
– else
– {
– FirstCell = SNext;
– SNext = SNextNext;
– Free(FirstCell);
– }
• }
Stack Operations
• void MakeEmpty(Stack S)
• {
– while ( !IsEmpty(S))
–{
• Pop(S);
–}
• }
Array Implementation of Stack
• Associated with each stack is TopOfStack, which
is -1 for an empty stack.
• To push some element X onto the stack. We
increment TopOfStack and then set
Stack[TopOfStack] = X.

• To pop we set the return value to


Stack[TopOfStack] and then decrement.
Array Implementation of Stack
• Struct StackRecord
• {
– int Capacity;
– int TopOfStack = -1;
– ElementType *Array;
• }
Array Implementation of Stack
• void Push(ElementType X, Stack S)
• {
– if (IsFull(S))
–{
• Error (“Full Stack”);
–}
– else
–{
• SArray[++STopOfStack] = X;
–}
• }
Array Implementation of Stack
• ElementType TopAndPop(Stack S)
• {
– if (!IsEmpty(S))
–{
• return SArray[STopOfStack--];
–}
– Error(“Empty Stack”);
– return 0;
• }
Array Implementation of Stack
• Stack CreateStack(int MaxElements)
• {
– if (MaxElements < MinStackSize)
–{
• Error (“Stack size is too small”);
–}
– Stack S = malloc(sizeof(StackRecord));
– SArray =
malloc(sizeof(ElementType)*MaxElements);
– SCapacity = MaxElements;
– return S;
• }
Stack Applications
• Balancing Symbols
– Read Characters till EOF
– If Character == opening symbol then Push
(Character)
– If Character == closing symbol && !IsEmpty(Stack)
then Pop( )
– If Poped Symbol isn't the corresponding opening
symbol then Error( )
– At EOF IsEmpty(Stack) == False then Error( )
Stack Applications
• Postfix Calculators
– Postfix notation is : Operand Operand Operator
• 54+,6534*44+8*+3*
• Easiest way to calculate is by using a stack.
• When input == Operand than Push (Number)
• When input == Operator than Pop ( Two Pushed
Numbers) and apply the operator and Push (Result).
• Execute couple of examples:
– 6 5 2 3 + 8 * + 3 + * = 288
– 5 1 2 + 4 * + 3 − = 14
Stack Applications
• Postfix Calculations
–512+4*+3−
Stack Applications
• Infix to Postfix Conversion
• Operand Operator Operand  Operand
Operand Operator
• 5+454+
Stack Applications
• Infix to Postfix Conversion
– If input = operand : add it to the output.
– Else if input = operator, o1 :
• 1) while there is an operator, o2, at the top of the stack, and either o1 is associative or
left-associative, and its precedence is less than or equal to that of o2, or o1 is right-
associative and its precedence is less than that of o2, pop o2 off the stack, onto the
output
• 2) push o1 onto the operator stack. Else if the input is a left parenthesis, then push it onto
the stack.
– Else if the input is a right parenthesis, then pop operators off the stack, onto
the output until the token at the top of the stack is a left parenthesis, at which
point it is popped off the stack but not added to the output queue. If the stack
runs out without finding a left parenthesis, then there are mismatched
parentheses.
– When there are no more inputs, pop all the operators, if any, off the stack, add
each to the output as it is popped out and exit. (These must only be operators;
if a left parenthesis is popped, then there are mismatched parentheses.)
Stack Applications
• Infix to Postfix Conversion
– 3+4*2/(1-5)^2  3 4 2 * 1 5 - 2 ^ / +
– a+b*c+(d*e+f)*g  abc*+de*f+g*+
Stack Applications
• Infix to Postfix Conversion
• a+b*c+(d*e+f)*g  abc*+de*f+g*+

Operand “a” is read passed to the output.


Operator “+” is read pushed into the stack.
Operand “b” is read passed to the output.
Stack Applications
• Infix to Postfix Conversion
• a+b*c+(d*e+f)*g  abc*+de*f+g*+

Operator “ * “ is read, top of the stack has lower


Precedence than * , so * is pushed.
Operand “c” is read passed to the output.
Stack Applications
• Infix to Postfix Conversion
• a+b*c+(d*e+f)*g  abc*+de*f+g*+

Operator “+” is read. top of the stack has higher


Precedence than + so * is poped. Other + is also poped
As it has equal precedence as “+”. + is pushed.
Stack Applications
• Infix to Postfix Conversion
• a+b*c+(d*e+f)*g  abc*+de*f+g*+

Operator “(” is read.


Highest Precedence “(“ is pushed.
Operand “d” is read passed to the output..
Stack Applications
• Infix to Postfix Conversion
• a+b*c+(d*e+f)*g  abc*+de*f+g*+

Operator “ * ” is read & pushed.


Stack Applications
• Infix to Postfix Conversion
• a+b*c+(d*e+f)*g  abc*+de*f+g*+

Operator “+” is read.


Operator “*” is pop passed to the output then “+” is
Pushed.
Stack Applications
• Infix to Postfix Conversion
• a+b*c+(d*e+f)*g  abc*+de*f+g*+

Operator “)” is read.


Stack is emptied back to “)”. + is passed to the output
Stack Applications
• Infix to Postfix Conversion
• a+b*c+(d*e+f)*g  abc*+de*f+g*+

Operator “ * ” is read & pushed.


Operand “g” is read and passed to the output.
Stack Applications
• Infix to Postfix Conversion
• a+b*c+(d*e+f)*g  abc*+de*f+g*+

Input is empty so pop the stack and pass the operators


To output.

You might also like