Implementation of stack using link list

 Stack: (Linear Data Structure)

Properties of Stack: LIFO(LAST IN FIRST OUT)

FILO(FIRST IN LAST OUT)

Operations in Stack:

 PUSH(INSERTION)

 POP(DELETION)

 PEEK(not remove the element but return the

top most element of stack)

 IsEmpty()(underflow)

 IsFull()(Overflow)

Logical Representation of Stack

 Dynamic

 Static

e.g. size=5(stack size)


Initially this stack is

2 Empty stack


0, 1


Initially assume top =-1

I want to insert value 2 (index 0)

PUSH(2)

top++(top=0)

PUSH(3)

Top++(top=1)

Call POP

POP()

Top--;

Pop()

Top- -

PUSH(1)


Implementation of stack

Push()

Pop()

Peek()

Display()


0 1 2 3 4 5

Int a[6]

100 104 108……………………………………


Int stack [5]

Void push()

{


int X;

Printf(“enter data”);

Scanf(“%d, &X);

If (top == N+1)

{

Prinf(“overflow”)

}

Else

{

top++;

stack [top] = X;

}}

Void pop()

Int item;

if

{


Top == -1;

{

printf(“underflow”)

else

item = stack[top]

top--;

printf (“%d”,item);

}

Void peek()

{

If (top == -1)

{

Printf(“Stack is empty”)

}

Else

{


Printf( (“%d”,stack[top])

}

Void display()

Int i;

For (i=top,i>=0,i--)

{

printf (“%d”,……)

}

Implementation using SWITCH statement

#define n,5; //Macro definition

Int stack[n]

Int top = -1;

Void main()

{

Int ch;

Clrscr();


do

{

Printf(“enter choice :1; Push

2;POP1,PEEK4:DISPLAY)………………)

Scanf(“%d”,&ch);

Switch(ch)

{

Case1 push();

break;

case2 pop();

break;

case 3 peek()

break;

case 4 display()

break()

default : printf(“invalid choice”)


}

While(ch!==0)

getch();

}}

Comments

Popular posts from this blog

Bihar Board 12th Result 2022 - Check BSEB Inter Result at BiharBoardonline.Bihar.gov.in

Chennai Super Kings against Mumbai Indians in Mumbai

BSEB 12th Result 2022 – biharboardonline.bihar.gov.in, Download Link