0% found this document useful (0 votes)
27 views42 pages

Dsa 5

Uploaded by

Taimour Afridi
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)
27 views42 pages

Dsa 5

Uploaded by

Taimour Afridi
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/ 42

DATA STRUCTURES

AND ALGORITHMS

1
Outline

■ Stack
■ Stack Implementation
– Array
– Linked List
■ Applications of Stack

2
INTRODUCTION ABOUT
STACKS
The stack is a very common data structure used in
programs which has lot of potential.

Stacks hold objects, usually all of the same type.


It follows the concept of LIFO – last in first out.
Stack is a linear list of items in which all
additions and deletion are restricted to one end.
STACK
A stack is a first-in-last-out data structure with access only to the top

of the data.

Since many languages does not provide facility for stack, it is backed

by either arrays or linked list.

In Stack, insertion and deletion is possible on only one side of the


stack. The other side is sealed.

Eg: a[10] –array a[10] - stack


A LIFO Stack
Push Pop

Stack Pointer Top

Bottom
Stack
Example
Components of Stack
• Top is a variable which refers to
last position in stack.

• Element is component which has


data.
• MaxStack is variable that describes
maximum number of elements in a stack.
STACK
Stack data structure is not inherently provided by many programming
IMPLEMENTATION
languages.

Stack is implemented using arrays or linked lists.


Let S be a stack, n be the capacity, x be the element to be pushed, then
push and pop will be given as

Push(S,x) and Pop(S)


Here we use “top” which keeps track of the top element in the stack.
When top = = 0 , and pop() operation gives stack underflow as result.
When top = = n, and push() operation gives stack overflow as result.
The pop() operation just gives an illusion of deletion, but the elements
are retained. Only the top is decremented.
S[1:6] Pop(S) a b c d e
Top=
5
Push(S,a) a Pop(S) a
Pop(S) Top=
Top= Pop(S)
1 1
Pop(S)
Push(S,a) a b c d e f Pop(S)
Push(S,b)
Push(S,c) Top= Pop(S)
Push(S,d) 6
Top=
Push(S,e) Push(S,f) 0
Push(S,g) a b c d e f Pop(S)
Top=6 = n Top=0 stack
Stack overflow underflow
Main Operation

STAC
K

PUSH POP
Add data to Take data
element in from element
stack. in stack
Kinds of Operation

• Stack Operation in array form

• Stack Operation in Linked list form


❶❷

STACK OPERATION IN ARRAY


FORM
Initialization

Operation that give an initial value for top


pointer in stack with the following rules:

• Give 0 if the first element starts from 1


• Give -1 if the first element starts from 0
Empty Operation

■Operation that returns true if the top

pointer have 0 or -1 as its value (depend on

initialization) or returns false to the

contrary.
Full Operation

■Operation that returns true if top have reached

maximum array or maximum array -1 (depend on

initialization) or returns false if top is not

equal to maximum array.


Push

Steps in push operation:


• Stack can be added when it’s not full
• Add the top pointer with 1
• Stack element, which was refered
by top pointer, is filled with new
data.
Push

Push(Stack,8)
Push(Stack,3)
1 4 Push(Stack,5)
Push(Stack,1)
5 3
Push(Stack,7)
3 2 “Stack Overflow”
8 1
Top
Stack 0
Algorithm for Push Operation in Array

■ Push(s,data)
■ { If(top>=(max-1))
■ { cout<<“Stack Overflow”; }
■ Else
■ { top=top+1;
■ S[top]=data; }

18
Algorithm for Pop Operation in Array

■ Pop(s)
■ { If(top<0)
■ { cout<<“Stack underflow”; }
■ Else
■ { int x= s[top];
■ top=top-1;
■ Return x;}

19
Pop
■Steps in pop operation:
• Stack can be pop when its elements is not
empty.

• Element that have taken out from stack is saved


in a variable.

• Substract the top pointer with 1.


Pop
Pop(Stack)
1 4
Pop(Stack)
5 3 Pop(Stack)
Pop(Stack)
3 2
Pop(Stack)
8 1 “Stack Underflow”
Top
Stack 0 8
1
3
5
Element
❶❷
STACK OPERATION IN LINKED
LIST FORM
Push
❖ Push(*Top,8)
Top

head 8

❖ Push(Top,3) Top

head 3 8
Push

❖ Push(*Top,5)

Top

head 5 3 8
Pop

Steps of pop operation in linked list form

is similar with front deletion.


Pop
❖ Pop(*Top)

head Top

5 3 8
Pop

❖ Pop(*Top)

head Top

3 8
Pop

❖ Pop(*Top)

head Top

8
Algorithm for Push Operation in Linked list
■ Push(struct node *top, int info)
■ { Node *ptr=new node();
■ If(ptr==NULL)
■ { cout<<“Stack Overflow”; }
■ Elseif(top==NULL)
■ { Ptr->data= info;
■ Ptr->next=NULL;
■ Top=ptr; }
■ Else
■ { ptr->data= info;
■ Ptr->next=top;
■ Top=ptr;}
■ Return top;}

29
Algorithm for Pop Operation in Linked list
■ Pop(struct node *top)
■ { struct node *temp;
■ If(top==NULL)
■ { cout<<“Stack Underflow”; }
■ Else
■ {temp=top;
■ Top=top->next;
■ Delete(temp);}
■ Return top;}

30
STACK APPLICATIONS
Reverse a string
Evaluating Mathematical Expression
Compilers
Balancing Symbols
Stack Application

Reversing a string: To reverse a string we can


use following algorithm.
1. Given the string and a stack
2. While there is not end of string, do the following.
3. Read a character form the string
4. Push it on the stack
5. End while
6. Re-initialize string position
7. While stack is not Empty, do the following.
8. Pop a character from the stack
9.Insert the character popped into next position in string.
10.End While
Reverse
String...

String is a b c d e f
PUSH to STACK
Reverse String...

Reversed String: f e d c b
a POP from STACK
Balancing Symbols

■ To check that every right brace, bracket, and parentheses must correspond to its left counterpart
– e.g. [( )] is legal, but [( ] ) is illegal
■ Algorithm
– Make an empty stack.
– Read characters until end of file
■ If the character is an opening symbol, push it onto the stack
■ If it is a closing symbol, then if the stack is empty, report an error
■ Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, then
report an error
■ At end of file, if the stack is not empty, report an error

DSA- 06 35
Detecting Unbalanced Parenthesis

■ (((x + y) + ((a+b) * c)) / d )

■ (
■ ((
■ (((
■ ((
■ (((
■ ((((
■ (((
■ ((
■ ( balanced

36
Balancing Symbols

Traces of the algorithm that


checks for balanced braces

DSA- 06
Solve

■ ([)]
■ ((()]))

38
39
40
41
42

You might also like