Implementation of stack data structure using C++
Objective:
The objectives of this example are:-
- Practice on the implementation of stack data structure
- Learn to use stack operations
Problem Statement:
A purchase officer in ABC Company purchases items from vendors, after purchase, the officer place the items on top of one another. Then another officer picks the purchased items and moves it to store. The latest item purchased is the first one to move into the store and so on, till last item.
You need to create a C++ program that facilitate the purchaser officer to purchase items and move it on to the stack, and also helps the second officer to remove the top item from the stack one by one and move it into the store.
You need to implement stack data structure for this program.
Solution:
//Written and compiled in Dev C++ //This Program uses STACK Data Structure to store values (Using Array) //Written by Tasaddaq Hussain bc110200816 | May 07, 2016 #include<iostream> #include<conio.h> #include<stdio.h> #include<stdlib.h> using namespace std; int pointer=0; int x = -1; int data_items[50]; void tryagain(void); class Push { public: void insert(int item) { if(x >= 50) { cout<<"\aStack is FULL"; } else { data_items[++x] = item; cout<<"\nRecored Entered Successfully\n"; pointer++; } } }; class Pop { public: void deletes() { if(x == -1) { cout<<"\aStack is EMPATY\n"; } else { cout<<"\nRecord [ "<<data_items[x]<<" ] Delected Successfully\n"; x--; pointer--; } } }; int main() { int temp[50]; Push n; Pop m; LABLE: system("cls"); cout<<" ------------------[ ABC Company Ldt. ]--------------\n"; cout<<" ------------------[ PURCHASE MODULE ]--------------\n\n"; cout<<"------------------------------------------------------------------------------\n\n\n"; cout<<"MANU\n\n"; cout<<" 1 : Insert Item\n"; cout<<" 2 : Remove Item\n"; cout<<" 3 : Display Purchased Items\n\n"; cout<<" 4 : Exit\n"; cout<<"-------------------------\n"; cout<<"Enter your choice <1-4>: "; char choice; cin>>choice; switch(choice) { case '1': cout<<"\n\nINSERT ITEM:\n=============\n"; cin>>temp[pointer]; n.insert(temp[pointer]); break; case '2': cout<<"\n\nDelete Item:\n=============\n"; m.deletes(); break; case '3': cout<<"\n\nPurchased Items:\n=================\n"; for(int k=0; k<pointer; k++) { cout<<"["<<data_items[k]<<"], "; } break; case '4': exit(0); default: cout<<"\nWRONG CHOICE\n"; tryagain(); break; } tryagain(); goto LABLE; getch(); return 0; } void tryagain(void) { cout<<"\nPress any key to go to Manu"; getch(); }Screenshot:
No comments: