You are on page 1of 40

Chapter no.

4 Stack

Stack
Last In First Out

Input Output

Memory
Data

-1- By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Representation of Stack

Values
Stack
top
1032

965
1034

6 1036

564 1038

102 103A

3252 103C

-2- By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Representation of Stack

Values
Stack
top
1032

‘a’
1033

‘Y’ 1034

‘k’ 1035

‘+’ 1036

‘9’ 1037

-3- By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Representation of Stack

Values
Stack
top
1032

56.12
1036

6.0003 103A

12.33 103E

456.00 1042

145.09 1046

-4- By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

PUSH operation on Stack

Stack Top

12

85

56

45

After push(52) operation

Stack Top

52

12

85

56

45

-5- By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

POP operation on Stack

Stack Top

12

85

56

45

After n = pop() operation

Stack Top

85

56

45 n = 12

-6- By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

-7- By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* To pop and push items in a stack */


void push ( int );
int pop( );
int stack[10];
int pos;
void main( )
{
int n ;
pos = -1 ; /* stack is empty */
push ( 10 ) ; push ( 20 ) ; push ( 30 ) ;
push ( 40 ) ;
n = pop( ) ;
printf ( "\n\titem popped out is : %d", n ) ;
n = pop( ) ;
printf ( "\n\titem popped out is : %d", n ) ;
getch();
}
void push ( int data )
{
if ( pos == 9 )
printf ( "\n\tStack is full" ) ;
else {
pos++ ;
stack [ pos ] = data ;
}
}
int pop( )
{
int data ;
if ( pos == -1 ) {
printf ( "\n\tStack is empty" ) ;
return ( -1 ) ;
}
else {
data = stack [ pos ] ;
stack [pos] = 0;
pos-- ;
return ( data ) ;
}
}

-8- By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

struct node
{
int data ;
struct node *link ;
};
push ( struct node **s, int item )
{
struct node *q ;
q = malloc ( sizeof ( struct node ) ) ;
q -> data = item ;
q -> link = *s ;
*s = q ;
}
pop ( struct node **s )
{
int item ;
struct node *q ;
if ( *s == NULL )
printf ( " stack is empty" ) ;
else
{
q = *s ;
item = q -> data ;
*s = q -> link ;
free ( q ) ;
return ( item ) ;
}
}
main( )
{
struct node *top ;
int item ;
top = NULL ; /* empty stack */
push ( &top, 14 ) ;
//stack_display ( top ) ;
item = pop ( &top ) ;
printf ( "%d ", item ) ;
}

-9- By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

stack_display ( struct node *q )


{
printf ( "\n" ) ;
/* traverse the entire linked list */
while ( q != NULL )
{
printf ( "%2d ", q -> data ) ;
q = q -> link ;
}
printf ( "\n" ) ;
}

- 10 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Evaluation of Arithmetic Expression

(3 * 4) + (5 * 6)

Reverse Polish Notation (Post Fix):

34*56*+

4 5

3 3 12 12
3 4 * 5

5 30

12 12 42 Result

6 * +

- 11 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Conversion of Infix to Post-Fix

Operator Precedence:
1. Exponential ^
2. Mul/Div operation * /
3. Add/Sub operation + -

A/B^C+D*E–A*C

(((A / (B ^ C)) + (D * E)) – (A * C))

Solve the brackets first.

(((A / (BC^)) + (DE*))– (AC*))

(((A (BC^)/) + (DE*)) – (AC*))

(((A BC^/) + DE*) – AC*)

(((A BC^/) DE*) + AC*)-

Now, remove the brackets,


final expression will be,

ABC^/DE*+AC*-

- 12 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Example:

(A+B)*C/D+E^F/G

Go according to priority,

(AB)+*C/D+E^F/G

AB+*C/D+EF^/G

AB+C*D/+EF^G/

AB+C*D/EF^G/+

Solve:

A+B*C

A+B/C–D

A / B ^ C + (D – E)

A – B / (C * D ^ E)

(A + B ^ D) / (E – F) + G

- 13 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Infix to Postfix using stack


(A + B) * C

Convert to full parenthesis


((A + B) * C)

No. Scanned Operator


Post-Fix
Charcter Stack
1 ( (

2 ( ((

3 A (( A

4 + ((+ A

5 B ((+ AB

6 ) ( AB+

7 * (* AB+

8 C (* AB+C

9 ) ( AB+C*

10 AB+C*

- 14 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Evaluation of Post-fix expression

42^3*3–84/11+/+

Scanned Operand
No. Operation
Character Stack
1 4 4

2 2 42

3 ^ 16 4^2

4 3 16 3

5 * 48 16*3

6 3 48 3

7 - 45 48-3

8 8 45 8

9 4 45 8 4

10 / 45 2 8/4

11 1 45 2 1

12 1 45 2 1 1

13 + 45 2 2 1+1

14 / 45 1 2/2

15 + 46 45+1

- 15 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Evaluation of Post-fix expression

623+-382/+*2^3+

Scanned Operand
No. Operation
Character Stack
1 6 6

2 2 6 2

3 3 6 2 3

4 + 6 5 2+3

5 - 1 6-5

6 3 1 3

7 8 1 3 8

8 2 1 3 8 2

9 / 1 3 4 8/2

10 + 1 7 3+4

11 * 7 1*7

12 2 72

13 ^ 49 7^2

14 3 49 3

15 + 52 49+3

- 16 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Conversion of Infix to Pre-Fix

(A – C) * B

Solve the brackets first

Step 1 (-AC) * B

Step 2 *(-AC) B

Step 3 *-ACB

(A + B) * (C – D)

Step 1 (+ A B) * (- C D)

Step 2 * (+ A B) (- C D)

Step 3 *+AB-CD

- 17 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Infix to Pre-fix using Stack


4 ^ 2 * 3 – 3 + 8 / 4 / (1 + 1)
) 1 + 1 (/ 4 / 8 + 3 – 3 * 2 ^ 4

Scanned Operand
No. Expression
Character Stack
1 ) )
2 1 ) 1
3 + )+ 1
4 1 )+ 11
5 ( Empty +11
6 / / +11
7 4 / 4+11
8 / // 4+11
9 8 // 84+11
10 + + //84+11
11 3 + 3//84+11
12 - +- 3//84+11
13 3 +- 33//84+11
14 * +-* 33//84+11
15 2 +-* 233//84+11
16 ^ +-*^ 233//84+11
17 4 +-*^ 4233//84+11
18 Empty +-*^4233//84+11

- 18 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Post-fix to Infix Expression

Example:

AB+C*D/EF^G/+

1. (A + B) C * D / E F ^ G / +

2. ((A + B) * C) D / E F ^ G / +

3. (((A + B) * C) / D) E F ^ G / +

4. (((A + B) * C) / D) (E ^ F ) G / +

5. (((A + B) * C) / D) ((E ^ F ) / G ) +

6. (((A + B) * C) / D) + ((E ^ F ) / G )

- 19 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Post-fix to Infix Expression

Example:

ABC^/DE*+AC*-

1. A (B ^ C) / D E * + A C * -

2. A / (B ^ C) D E * + A C * -

3. (A / (B ^ C)) (D * E) + A C * -

4. (A / (B ^ C)) + (D * E) A C * -

5. (A / (B ^ C)) + (D * E) (A * C) -

6. (A / (B ^ C)) + ((D * E) - (A * C))

- 20 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Recursion
1. Direct Recursion

function( )

2. Mutual Recursion

function1( ) function2( )

3. General Indirect Recursion

function2( )

function1( ) function2( )

- 21 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Factorial of a number using recursion

6! = 6 * 5!
120 * 6 = 720

5! = 5 * 4!
24 * 5 = 120

4! = 4 * 3!
6 * 4 = 24

3! = 3 * 2! 3*2=6

2! = 2 * 1!
2*1=2

Base: 1! = 1

- 22 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Factorial in C using recursion


#include<stdio.h>
#include<conio.h>
int fact(int x)
Return Point
{
if(x<1)
return(1);
else
return( x * fact(x-1));
}
void main()
{
int a;
clrscr();
printf("%d",fact(4));
getch();
}

- 23 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Finding XY in C using recursion


#include<stdio.h>
#include<conio.h>
int power(int x, int y)
{ Return Point
if(y<1)
return(1);
else
return( x * power(x,--y));
}
void main()
{
printf("%d",power(4,3));
getch();
}

- 24 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Application of Stack in Recursion

2 * fact (1)

3 * fact (2)

4 * fact (3)

5 * fact (4)

6 * fact (5)

7 * fact (6)

Result

- 25 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Tower of Hanoi Problem

- 26 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Solution to Tower of Hanoi in C - Recursion


#include<stdio.h>
#include<conio.h>
void Tower(int n,char from,char to,char aux)
{
if(n==1)
{
printf("\nMove disk1 from %c -> %c",from,to);
return;
}
Tower(n-1,from,aux,to);
printf("\nMove disk%d from %c ->
%c",n,from,to);
Tower(n-1,aux,to,from);
}
void main()
{
clrscr();
Tower(3,'A','C','B');
getch();
}

Move disk1 from A -> C


Move disk2 from A -> B
Move disk1 from C -> B
Move disk3 from A -> C
Move disk1 from B -> A
Move disk2 from B -> C
Move disk1 from A -> C

- 27 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Solution to Fibonacci Series in C – Recursion

#include<stdio.h>
#include<conio.h>
int fib(int n)
{
if(n<1)
return(0);
if(n<3)
return(1);
return(fib(n-1)+fib(n-2));
}
void main( )
{
int i, n;
clrscr( );
printf("\nHow many numbers ? ");
scanf("%d", &n);
for(i=0;i<=n;i++)
printf("\n%d",fib(i));
getch( );
}

- 28 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Solution: a6 + a4 + a2 + 1

#include<stdio.h>
#include<conio.h>
int n = 8, x;
int power(int x, int y)
{
if(y<1)
return(1);
else
return( x * power(x,--y));
}
int funct(int a)
{
if(n<1)
return(x);
else
{
x += power(a, n-=2);
return(funct(a));
}
}
void main()
{
int a;
clrscr();
printf("\nEnter value of 'a' : ");
scanf("%d",&a);
printf("\nAnswer : %d", funct(a));
getch();
}

- 29 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

Application of Stack in matching parenthesis

1 #include<stdio.h>
2 void main( )
3 {
4 int a, b=7, c=2, k = 10, x[5];
5 a = ( c + b ) / ( 5 * k ) ) – c;
6 printf(“Result : %d”, a);
7 getch( );
8 }

Line No. Stack


2 (
2 ()
3 {
4 {[] Match found
5 {(
5 {()
5 {
5 {(
5 {()
5 {) Match found
6 {)(
6 {)()
6 {)
7 {)()
8 {)}
8 ) Stack is not empty

- 30 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Addition using recursion */


#include<stdio.h>
#include<conio.h>
int add(int a, int b)
{
if(b<0)
return 0;
else
return(a+add(1, b-1));
}
void main()
{
int x = add(14, 25);
clrscr();
printf("%d",x);
getch();
}

- 31 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Multiplication using recursion */


#include<stdio.h>
#include<conio.h>
int mul(int a,int b)
{
if(b<1)
return 0;
else
return(a+mul(a, b-1));
}
void main()
{
int x = mul(15,4);
clrscr();
printf("%d",x);
getch();
}

- 32 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Find square using recursion */


#include<stdio.h>
#include<conio.h>
int square(int a)
{
static int b;
b++;
if(a<b)
return 0;
else
return(square(a)+a);
}
void main()
{
int x = square(25);
clrscr();
printf("%d",x);
getch();
}

- 33 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Sum of digits of a number using


recursion */

#include<stdio.h>
#include<conio.h>
long sumdig(long n)
{
long sum = 0,rem;
if(n!=0)
{
rem = n % 10;
sum = rem + sumdig(n/10);
}
return(sum);
}
void main()
{
long n;
clrscr();
printf("Enter the number:");
scanf("%ld",&n);
printf("Sum : %ld",sumdig(n));
getch();
}

- 34 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Binary equivalent using recursion */


#include<stdio.h>
#include<conio.h>
void binary(long n)
{
int r;
r = n % 2;
n = n / 2;
if(n!=0)
binary(n);
printf("%d", r);
}
void main()
{
long n;
printf("Enter the number:");
scanf("%ld", &n);
printf("Binary equivalent is : ");
binary(n);
}

- 35 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Finding prime number using


recursion*/

#include<stdio.h>
#include<conio.h>

int isprime(int num)


{
static int a = 2;
if(num%a++==0)
return(a);
else
return(isprime(num));
}
void main()
{
int x, y;
printf("\nEnter any value ");
scanf("%d",&x);
y = isprime(x);
if(--y == x)
printf("The no. is prime...!");
else
{
printf("The no. is not prime...!");
printf("\nDivisible by : %d",y);
}
}

- 36 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Reversing string using recursion */


#include<stdio.h>
#include<conio.h>
void reverse(char *a)
{
if(!*a)
return;
else
{
reverse(a+1);
printf("%c",*a);
}
}
void main()
{
char a[] = "HELLO";
reverse(a);
getch();
}

- 37 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Length of String using recursion */


#include<stdio.h>
#include<conio.h>
length(char *a)
{
static int x;
if(*a!='\0')
{
x++;
length(a+1);
}
else
return x;
}
void main()
{
char a[] = "HELLO";
int len = length(a);
printf("Length: %d", len);
}

- 38 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Palindrome String using recursion */


#include<stdio.h>
#include<conio.h>
int palin(char *a)
{
int len = strlen(a);
static int count=1;
if(*a!='\0')
{
if(*a==a[len-count])
{
count++;
palin(a+1);
}
else
return 0;
}
else
return 1;
}
void main()
{
char a[] = "MalayalaM";
if(palin(a))
printf("\nPalindrome !!!");
else
printf("\nNot Palindrome !!!");
getch();
}

- 39 - By, Kute T. B. for DST (SYIF) 2007-08


Chapter no. 4 Stack

/* Add array elements using


recursion */

#include<stdio.h>
#include<conio.h>
int add(int *num, int n)
{
if(n)
return(*num+add(num+1,n-1));
else
return 0;
}
void main()
{
int x[]={15, 44, 8, 10, 23};
int a = add(x, 5);
printf("\nAddition is : %d", a);
getch();
}

- 40 - By, Kute T. B. for DST (SYIF) 2007-08

You might also like