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

Linked List

Uploaded by

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

Linked List

Uploaded by

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

Stack (Part-I)

(Abstract Data Type)

Ashish Patel

Department of CSE & Information Technology


Shri S’ad Vidya Mandal Institute of Technology

Ashish Patel (SVMIT) Data Structures (3130702) 1 / 14


Contents

1 What is Stack?

2 Applications of Stack

3 Operations on Stack

4 C Program: Stack using array

5 C Program: Stack using structure

6 C Program: Reverse string using stack

Ashish Patel (SVMIT) Data Structures (3130702) 2 / 14


What is Stack?

Stack is an abstract data type.


It is one type of container in which the data are stored. The container is
open from one side only so the data can be inserted and deleted using only
one end.
It is an ordered list of items.
The insert operation is known as PUSH and the delete operation is known
as POP.
Stack is using LIFO (last in - first out) mechanism for push and pop. If the
first element is pushed into the stack, it will be the last element for pop.
For example consider a packet of biscuits torn from one side only. If anybody
wants to eat biscuit, he or she has to eat the first biscuit available on top of
the packet. We cannot remove second biscuit without removing the first one.
Also, if we want to put the biscuit back in the packet we have to place it on
top of the remaining biscuits. There are many real world examples

Ashish Patel (SVMIT) Data Structures (3130702) 3 / 14


Stack Data Structure

In computer science, stack is used in many applications we are using in


everyday life. For example, the browser uses this mechanism to retrieve
your last visited page of any website. When you press back button it
display the page you have used most recently.

Ashish Patel (SVMIT) Data Structures (3130702) 4 / 14


Applications of Stack

Reversing String

Recursion or nested function calls

Evaluating expressions

Backtracking

Parenthesis matching

Ashish Patel (SVMIT) Data Structures (3130702) 5 / 14


Operations on stack

create() → Create an empty stack.

destroy() → Destroy a stack.

empty() → Return true if the stack is empty, otherwise return false.

push(new item) → Add a new item to the top of the stack.

item pop() → Remove the top element from the stack and return it to the
caller.

item top() → Return the top element of the stack without removing it.

Ashish Patel (SVMIT) Data Structures (3130702) 6 / 14


Push Operation (Insertion

An item is always pushed on top of the stack,


increasing the stack size by one. As stack size is
push ( )
{ usually limited, this may lead to stack overflow if the
if ( t o p > MAX) { maximum size is reached. See the code written in C
p r i n t f ( ‘ ‘ Stack language.
O v e r f l o w ’’ ) ;
return ; In this example, M AX indicates the maximum size
} of the stack. If the stack is full, return the control
top = top + 1 ; back to the calling function otherwise continue with
s t a c k [ top ] = item ;
the remaining statements.
}

If stack is not full, increment the top index by one


and insert item at that location in the stack. Initially
top is −1 allowing us to access the 0 index of array
for the first insertion. Recall that array range is 0 to
M AX − 1 and here top is used as an index to an
array.

Ashish Patel (SVMIT) Data Structures (3130702) 7 / 14


Pop Operation (Deletion)

Only top item can be popped from the stack decreasing stack size by one. If
no top item is available in the stack, it is empty and called stack underflow.
pop ( )
{
if ( t o p < 0 )
{
p r i n t f ( ‘ ‘ S t a c k U n d e r f l o w ’’ ) ;
return ;
}
item = s t a c k [ top ] ;
top = top − 1 ;
}

top indicates top most element on the stack.


After removing top most element, decrement top index by 1.

Ashish Patel (SVMIT) Data Structures (3130702) 8 / 14


Figure: Operations on Stack

The above figure demonstrates push and pop operations on the stack.
1 Initially stack is empty and top = −1, when user inserts first value, say 10,
the function first increments top, setting it’s value to 0.
2 Using the same method 20 and 30 are inserted and top = 2.
3 Next operations is pop. The only value user can pop from the stack is 30.
After popping 30, the code decrements top by 1, setting top = 1.
4 Then 40 is pushed onto the stack, setting top = 2.
5 The last operation is pop, which removes 40 from the stack and set the
value top = 1.
Ashish Patel (SVMIT) Data Structures (3130702) 9 / 14
C Program: Stack using array

int t o p =−1; p r i n t f ( " \ nEnter value for push : " ) ;


int s t a c k [ 5 ] ; s c a n f ( " % d " ,& x ) ;
top = top + 1 ;
int main ( ) s t a c k [ top ] = x ;
{ }
int c h o i c e ;
while ( 1 ) { pop ( )
p r i n t f ( " \n - - - - - Menu - - - - -\ n " ) ; {
p r i n t f ( " 1. push \ n2 . pop \ n3 . display \ n4 . exit " ) ; int x ;
p r i n t f ( " \ nEnter your choice : " ) ; if ( t o p < 0 ) {
s c a n f ( " % d " ,& c h o i c e ) ; p r i n t f ( " \ nUnderflow " ) ;
switch ( c h o i c e ) return ;
{ }
case 1 : push ( ) ; p r i n t f ( " Popoed element is % d " , s t a c k [ t o p ] ) ;
break ; top = top − 1 ;
case 2 : pop ( ) ; }
break ;
case 3 : d i s p l a y ( ) ; display ()
break ; {
case 4 : e x i t ( 1 ) ; int i ;
} if ( t o p < 0 ) {
} p r i n t f ( " \ nUnderflow " ) ;
} return ;
}
push ( ) for ( i =0; i<=t o p ; i ++) {
{ p r i n t f ( "%d | " , s t a c k [ i ] ) ;
int x ; }
if ( t o p >= 4 ) { }
p r i n t f ( " \ nOverflow " ) ;
return ;
}
/* Stack using pointer to structure */ void push ( s t a c k ∗ s , int no )
# include<s t d i o . h> {
# define N 10 if ( ! f u l l ( s ) ) {
s−>t o p = s−>t o p + 1 ;
typedef struct s−>a r r [ s−>t o p ] = no ;
{ p r i n t f ( " Element is Pushed \ n " ) ;
int a r r [ N ] , t o p ; }
} stack ; else {
p r i n t f ( " STACK OVERFLOW \ n " ) ;
stack s ; }
void pu sh ( s t a c k ∗ , int ) ; }
void pop ( s t a c k ∗) ; void pop ( s t a c k ∗ s )
void d i s p l a y ( s t a c k ∗) ; {
int f u l l ( s t a c k ∗) ; int no ;
int empty ( s t a c k ∗) ; if ( ! empty ( s ) ) {
no = s−>a r r [ s−>t o p ] ;
main ( ) { s−>t o p = s−>t o p − 1 ;
int c h o i c e , no ; p r i n t f ( " Popped Element is % d \ n " ,
s . t o p = −1; no ) ;
do { }
p r i n t f ( " 1 Push 2 Pop 3 Display 4 Exit " ) ; else {
p r i n t f ( " \ nEnter your choice : " ) ; p r i n t f ( " STACK UNDERFLOW \ n " ) ;
s c a n f ( " % d " , &c h o i c e ) ; }
switch ( c h o i c e ) }
{ void d i s p l a y ( s t a c k ∗ s )
case 1 : p r i n t f ( " Enter element for Push \ n " ) ; {
s c a n f ( " % d " , &no ) ; int i ;
push (&s , no ) ; if ( ! empty ( s ) ) {
break ; p r i n t f ( " Stack Contents \ n " ) ;
case 2 : pop (& s ) ; break ; for ( i = 0 ; i <= s−>t o p ; i ++)
case 3 : d i s p l a y (& s ) ; break ; p r i n t f ( " % d \ n " , s−>a r r [ i ] ) ;
case 4 : e x i t ( 1 ) ; }
default : p r i n t f ( " Invalid choice \ n " ) ; else {
} p r i n t f ( " STACK UNDERFLOW \ n " ) ;
} while ( c h o i c e != 3 ) ; }
} }
int f u l l ( s t a c k ∗ s ) int empty ( s t a c k ∗ s )
{ {
if ( s−>t o p > N − 1 ) if ( s−>t o p < 0 )
return 1 ; return 1 ;
else else
return 0 ; return 0 ;
} }
Reverses String using Stack

# include<s t d i o . h> while ( i n d e x > 0 )


# define N 10 /* size of the character array {
*/ ch = pop (& s t k ) ;
typedef struct p r i n t f ( " % c " , ch ) ;
{ i n d e x −−;
char s t r i n g [ N ] ; }
int t o p ; }/* End of MAIN */
} stack ;
stack stk ; void push ( s t a c k ∗ s t k , char ch )
{
void pu sh ( s t a c k ∗ , char ) ; s t k−>s t r i n g [++( s t k−>t o p ) ] = ch ;
char pop ( s t a c k ∗ ) ; } /* End of PUSH */

main ( ) char pop ( s t a c k ∗ s t k )


{ {
int i n d e x = 0 ; char ch ;
char ch , m y S t r i n g [ N ] ; ch = s t k−>s t r i n g [ ( s t k−>t o p ) −−];
s t k . t o p = −1; return ch ;
p r i n t f ( " Enter Input String :\ n " ) ; } /* End of POP */
s c a n f ( " % s " , m y S t r i n g ) ; // can be replaced
by gets ()

while ( m y S t r i n g [ i n d e x ] != ’ \0 ’ )
{
pu sh (& s t k , m y S t r i n g [ i n d e x ] ) ;
i n d e x ++;
}

Ashish Patel (SVMIT) Data Structures (3130702) 13 / 14


For Infix, Prefix and Postfix, refer another file...

END

Ashish Patel (SVMIT) Data Structures (3130702) 14 / 14

You might also like