You are on page 1of 28

Stack using arrays

Definition
 A Stack is a data structure containing
finite elements of the same type and with
which you can perform the following
operations.
 Add data, one item at a time, Push
 Access the data item on the top (other
data items are hidden from view), Top
 Remove and discard the data item on the
top, thus exposing the next item which
now becomes the top item, Pop.
Definition…
 In a stack bottom is fixed and all stack
operations are done at top end.
 The order in which data in added,
accessed and removed is LIFO (last in,
first out).
 Stacks can be implemented using “arrays”
and “linked lists”.
 Linked lists is already studied.
Functions performed by a stack
 Push
 Insert an item x into top of the stack and returns new
stack S with top pointing to the new item x.

 Pop
 Delete the item x pointed by the top of the stack and
returns new stack S with top is pointing to the item next
down to the deleted item.
 isEmpty
 Checks whether the stack is empty or not.
Stack Insertion
Top=-1
•Stack is empty
•Top=NULL or -1
Push(1)

1 Top=0

•First item inserted


•Top=0
Push(5)

5 Top=1

•Second item inserted


•Top=1
Top=2 Push(10)
10

5
1

•Third item inserted


•Top=2
20 Top=3

Push(20)
10

5
1

•Fourth item inserted


•Top=4
40 Top=4

20
Push(40)
10
5

•Fifth item inserted


•Top=4
!!! Error (Overflow)
40 Top=4

20
Push(80)
10
5

•Ifwe try to insert a sixth item


inserted the stack will
overflow. As MaxStk is 5
Stack Deletion
40 Top=4 pop()
Top=3
20

10
5

•Topitem is deleted
Top=3
pop()

10 Top=2

•SecondTop item deleted


•Top=2
pop()

5 Top=1

•Third Top item is deleted


•Top=3
pop()

1 Top=0

•Fourth Top item deleted


•Top=2
pop()

Top=NULL or -1

•Fifth Top item is deleted


•Top=-1 or NULL
Trying to delete Sixth Top
item ?

Top=NULL or -1

•Top=-1 or NULL
pop()

!!! Error Underflow

Top=NULL or -1

•Top=-1 or NULL
Pseudo code and C++ code (Push)
 Procedure 6.1
void push(int MaxStk, int* top1,
Push(Stack,Top,MaxStk,Item) char *STK
,char item)
{
1. [Stack already filled?] if( *top1==MaxStk-1)
If Top=MaxStk-1, then print {
Overflow, and return cout<<"\n\nStack is
Full";
1. Set Top=Top+1. [increase exit(1);
Top by 1] }
else
2. Set Stack[Top]:=Item, {
[Inserts Item in new TOP *top1=*top1+1;
position] STK[*top1]=item;
}
cout<<"\n\ntop = "<<*top1;
3. Return
}
Pseudo code and C++ code (Pop)
 Procedure 6.2
void pop(int top1, char *STK )
Pop(Stack, Top, Item) {
if(*top1==-1)
 [Stack has an item to be
{
removed?]
cout<<"The stack is empty\n";
if Top=0, then: Print: }
Underflow, and Return.
else
{
2. Set Top=Top-1. [Decreses *top1=*top1-1;
Top by 1.] cout<<"the deleted element is\n”;
cout<<STK[*top1+1];
3. Return }
cout<<"top = "<<*top1;
}
Main Program
void main(void)
{
clrscr();
char *STK;
int MaxStk=0, top=-1;
int* top1;
top1=&top;
cout<<"Enter the max number of items in array: ";
cin>>MaxStk;
STK=new char[MaxStk];
push(MaxStk,top1,STK,'[');
Main Program…
getch();
push(MaxStk,top1,STK,'c');
getch();
push(MaxStk,top1,STK,']');
getch();
pop(top1,STK);
getch();
pop(top1,STK);
getch();
pop(top1,STK);
getch();
}
Stack Implementation in OOP
class Stack { Stack::Stack(){
private: size = 10;
int size, top; top = -1;
int * arr; arr = new int[size];}

public: Stack::Stack(int array_size){


Stack(); size = array_size;
Stack(int array_size); top = -1;
arr = new int[size]; }
//Stack(int num, string file_name);
bool isEmpty(); bool Stack::isEmpty(){
bool isFull(); return top == -1; }
int size(); bool Stack::isFull(){
int max_size(); return top == size - 1; }
void display(); int Stack::size(){
void push(int num); return top+1; }
void pop(); int Stack::max_size(){
int topval(); }; return size; }
void Stack::display(){ void Stack::push(int num){
if(isFull())
if (isEmpty())
cout<<"Error! Over Flow!"<<endl;
cout<<"Stach is Empty!\n";
else {
else { top++;
for (int i = top; i>=0; i--)
cout<<" | "<<arr[i]<<" |\n";
}
arr[top] = num; }
void Stack::pop(){
}}
cout<<"-------\n";
if(isEmpty())
int Stack::topval(){ cout<<"Error! UnderFlow!\n";
if (!isEmpty()) else {
cout<<arr[top]<<": is poped\n";
return arr[top];
top--;
cout<<"Stack is Empty!\n";
}}
return NULL; }
Main Program
void main() cout<<"\npop 1st time\n"; st.display();
{ st.pop(); getch();
Stack st(5); cout <<"Top Val:" << cout<<"\npop 5th time\n";
st.display(); st.topval()<<endl; st.pop();
getch(); st.display();
cout <<"\nTop Val:" <<
st.push(5); getch(); st.topval()<<endl;
st.display();
cout<<"\npop 2nd time\n"; st.display();
getch();
st.pop(); getch();
st.push(2);
cout <<"Top Val:" << cout<<"\npop 6th time\n";
st.display();
st.topval()<<endl;
getch(); st.pop();
st.display();
st.push(3); cout <<"Top Val:" <<
getch();
st.display(); st.topval()<<endl;
getch(); cout<<"\npop 3rd time\n";
st.display();
st.push(7); st.pop();
getch();
st.display(); cout << "Top Val:" <<
st.topval()<<endl; cout<<"\npop 7th time\n";
getch();
st.display(); st.pop();
st.push(1);
getch(); cout <<"Top Val:" <<
st.display();
st.topval()<<endl;
getch(); cout<<"\npop 4the time\n";
st.display();
st.push(3); st.pop();
st.display(); cout <<"Top Val:" <<
getch();
getch(); st.topval()<<endl; }
Output
Questions

????

You might also like