Stacks data structures refer to the lists stored and accessed in a special way, where LIFO (Last In First Out)
technique is followed. In stacks ,insertion and deletions (PUSH and POP) take place only at one end , called top.
Stack is similar to a stack of plates. The plates are inserted or removed only from the top of the stack.
Program:
#include<iostream>
#include<windows.h>
using namespace std;
#define MAX 100
using namespace std;
struct Stack{
int arr[MAX];
int top;
public:
Stack()
{
top=-1;
}
void push(int n)
{
if(top==MAX-1)
cout<<"Stack full!!!!!!";
else
arr[++top]=n;
}
int pop()
{
if(top==-1)
return -1;
else
return arr[top--];
}
void display()
{
for(int i=0;i<top;++i)
cout<<arr[i]<<" ";
}
}a[5];
int main()
{
system("color e0");
for(int i=0;i<=10;++i)
{
a[i].push(i);
}
cout<<"\n\n\t\tPushing array elements...\n\n";
Sleep(3500);
cout<<"\n\n\t\tPoping array elements...\n\n";
Sleep(3000);
for(int i=0;i<=10;++i)
{
cout<<"\t\t\t"<<a[i].pop()<<endl;
}
return 0;
}
Output:
technique is followed. In stacks ,insertion and deletions (PUSH and POP) take place only at one end , called top.
Stack is similar to a stack of plates. The plates are inserted or removed only from the top of the stack.
Program:
#include<iostream>
#include<windows.h>
using namespace std;
#define MAX 100
using namespace std;
struct Stack{
int arr[MAX];
int top;
public:
Stack()
{
top=-1;
}
void push(int n)
{
if(top==MAX-1)
cout<<"Stack full!!!!!!";
else
arr[++top]=n;
}
int pop()
{
if(top==-1)
return -1;
else
return arr[top--];
}
void display()
{
for(int i=0;i<top;++i)
cout<<arr[i]<<" ";
}
}a[5];
int main()
{
system("color e0");
for(int i=0;i<=10;++i)
{
a[i].push(i);
}
cout<<"\n\n\t\tPushing array elements...\n\n";
Sleep(3500);
cout<<"\n\n\t\tPoping array elements...\n\n";
Sleep(3000);
for(int i=0;i<=10;++i)
{
cout<<"\t\t\t"<<a[i].pop()<<endl;
}
return 0;
}
Output:
0 comments:
Post a Comment