Stack
•A stack is an example of a data structure•A stack is a Last In, First Out (LIFO) data
structure•Anything added to the stack goes on the “top”
of the stack•Anything removed from the stack is taken from
the “top” of the stack•Things are rem oved in the reverse order from
that in which they were inserted.
Basic
Stack Operations
•Push (Insertion the
item)
• Pop (Remove
the item)
•Push
–the
operation to place a new item at the top of the stack
•Pop
–the
operation to remove the next item from the top of the stack
Example
•Stack size = 6
•Max value of stack top = 5
•Min value of stack top = -1
•First value of stack top = 0
Operation of Stack
•Delete operation
•Add operation
Example code:
#include<iostream>
using namespace std;
class stack
{ private:
int A[10],top;
public:
public:
stack()
{
top=-1;
}
void push(int x)//Addition
{
if(top==10-1)
{cout<<"Stack is full:";
return;}
else{
A[top++]=x;
}
}
void pop()//deletion
{
if(top==-1)
{
cout<<"Stack is Empty:"<<endl;
return;
}
else
{
A[top--];
cout<<"Stack is deleted:";
}
}
};
int main()
{
int a;
stack s;
start:
cout<<"\n 1 to push: \n 2 to pop:";
cin>>a;
switch(a)
{
case 1:
s.push(a);
goto start;
break;
case 2:
s.pop();
goto start;
break;
default:
cout<<"Thanks:";
}
return 0;
}




No comments:
Post a Comment