Breaking

Search

Friday, June 26, 2020

STACK USING LINKED LIST A LGORITHM FOR PUSH AND POP Operation

STACK USING POINTER PUSH AND POP ALGORITHM

STACK USING LINKED LIST A LGORITHM FOR PUSH AND POP Operation

The write a Program for Stack using pointer. The Following Algorithm Steps.

 [ A  . Push Algorithm ]

  • Step1 : Start the Program
  • Step2 : Check if top >=Size-1 “Stack Overflow and Exit”.
  • Step3 :Increment insertion s [TOP]=value.
  • Step4 : Exit.


[B. Pop Algorithm]

  • Step1 : Check if top=0 “Stack underflow ” and Exit.
  • Step2 : Increment the pointer value.
  • Step3 : Perform value =s [ TOP ]
  • Step4 : Return value.
  • Step5 : Stop the execution.

Stack Using Pointer program:

#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#define maxsize 10
class pstack
{
private:
int top,*a;
public:
pstack()
{
a=new int[maxsize];
top=1;
}
void push(int);
int pop(void);
void show(void);
};
void pstack: :push (int m)
{
if (top + 1==maxsize)
{
count<<"\n stack is full";
getch();
}
else
{
top++;
* (a+top)=m;
count<<"\n push sucess";
getch();
}
}
int pstack: :pop(void)
{
if (top==-1)
{
count<<"\n stack is empty";
getch();
return -1;
}
else
{
Return (*(a+top--));
}
}
void pstack: :show(void)
{
if (top==-1)
{
count<<"\n stack is empty";
getch();
}
else
{
for (int i=top;i>=0;1--)
{
count<<"\n"<< *(a+i);
}
getch();
}
}
void main()
{
int result,item,choice;
pstack obj;
clrscr();
while(1)
{
count<<"\n 1.push";
count<<"\n 2.pop";
count<<"\n 3.show";
count<<"\n 4.exit";
count<<"\n Enter your choice";
cin>> choice;
switch (choice)
{
case 1:

count<<"\nEnter the number to pushed";
cin>>item;
obj.push(item);
break;
case 2:
count<<"\n poped number";
result=obj.pop();
count<<result;
break;
case 3:
obj.show();
break;
case 4:
exit(0);
default:
count<<"\n wrong chioce";
getch();
break;
}
}

OUTPUT:

1.Push
2.pop
3.show
4.exit


Enter your choice:1
Enter number to be pushed:10



1.Push
2.pop
3.show
4.exit


Enter your choice:1
Enter number to be pushed:20


1.Push
2.pop
3.show
4.exit


Enter your choice:1
Enter number to be pushed:30



1.Push
2.pop
3.show
4.exit


Enter your choice:3
30
20
10


1.Push
2.pop
3.show
4.exit



Enter your choice:2
Enter number to be pushed:30



Enter your choice:3
20
10


1.Push
2.pop
3.show
4.exit

Enter your choice:4

No comments:

Post a Comment