Categories
Uncategorised

Stacks : Implementation Using Arrays

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html /

/ Array implementation of a Stack /

#include <iostream.h>
#include <conio.h>
#include <process.h>

class array
{
int num[10], top, max;
public:
array()
{
top = -1;
max = 9;
}
void push(int n)
{
if (top == max)
cout<<endl<<“Stack overflow! Cannot be inserted!”;
else
{
top++;
num[top] = n;
}
}
void pop()
{
if (top == -1)
cout<<endl<<“Stack overflow! Stack does not exist!”;
else
{
int n = num[top];
top–;
cout<<endl<<“Deleted number is: “<<n<<endl;
}
}
void traverse();
};

void array::traverse()
{
if (top == -1)
cout<<endl<<“Stack underflow! Stack does not exist!”;
else
{
for (int i = top; i >= 0; i–)
cout<<num[i]<<” “;
cout<<endl;
}
}

void main()
{
array ft;
int ch;
int n;
char choice;
do
{
clrscr();
cout<<endl<<“Program Menu”
<<endl<<“1. Push (insert)”
<<endl<<“2. Pop (delete)”
<<endl<<“3. Traverse (show whole list)”
<<endl<<“4. Exit”
<<endl<<“Enter your choice: “;
cin>>ch;
switch(ch)
{
case 1 : cout<<endl<<“Enter number to be inserted: “;
cin>>n;
ft.push(n);
break;
case 2 : ft.pop();
break;
case 3 : ft.traverse();
break;
case 4 : cout<<endl<<“Exiting program”<<endl;
exit(0);
default : cout<<endl<<“You entered an invalid choice!”;
}
cout<<endl<<“Do you wish to continue? (y/n): “;
cin>>choice;
} while (choice == ‘Y’ || choice == ‘y’);
}

/ Output */

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit
Enter your choice: 1

Enter number to be inserted: 42

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit
Enter your choice: 1

Enter number to be inserted: 41

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit
Enter your choice: 1

Enter number to be inserted: 43

Do
you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit
Enter your choice: 3
43 41 42

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit
Enter your choice: 2

Deleted number is: 43

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit
Enter your choice: 3
41 42

Do you wish to continue? (y/n): y

Program Menu
1. Push (insert)
2. Pop (delete)
3. Traverse (show whole list)
4. Exit
Enter your choice: 4

Exiting program

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.