Linked Stack Using class

I have made this program to illustrate the concept of Linked Stack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//Push & Pop in Linked Stack
//class and typedef implementation
#include <iostream>

using namespace std;

class stack
{
private:
	typedef struct node
	{
		int info;
		node* next;
	}* nptr;
	nptr top,save,ptr;
public:	
	stack()
	{
		top=NULL;
		save=NULL;
		ptr=NULL;
	}
	nptr create(int n)
	{
		ptr=new node;
		ptr->info=n;
		ptr->next=NULL;
		return ptr;
	}
	void push(nptr np)
	{
		if(np==NULL)
		{
			cout<<"\nOVERFLOW !!!\nABORTING !!!\n";
			exit(1);
		}
		else
		{
			if(top==NULL)
				top=np;
			else
			{
				save=top;
				top=np;
				np->next=save;
			}
		}
	}
	void pop()
	{
		if(top==NULL)
		{
			cout<<"\nUNDERFLOW !!!\nABORTING !!!\n";
			exit(1);
		}
		else
		{
			ptr=top;
			top=top->next;
			delete ptr;
		}
	}
	void display(nptr np);
	nptr topout()
	{
		return top;
	}
	int delout()
	{
		return top->info;
	}
	~stack()
	{
		cout<<"\n\nTHANKYOU FOR USING THIS APPLICATION !!!\n";
	}
};

stack s;

void stack::display(nptr np)
{
	cout<<"\nThe Stack Now is : ";
	cout<<"\n"<<np->info<<"<-";
	while(np!=NULL)
	{
		np=np->next;
		cout<<"\n"<<np->info;
	}
	cout<<"\n!!!";
}

void printhead()
{
	clrscr();
	cout<<"\n\t\tLinked List";
	cout<<"\n\t\t****** ****";

}

void printfoot()
{
	clrscr();
	cout<<"\n\t\tPRESS 1 TO GO TO MAIN MENU";
	cout<<"\n\t\tPRESS 0 TO EXIT APPLICATION\n";
}

void insert()
{
	int a;
	char c='y';
	cout<<"\n\tInsertion into Stack : ";
	cout<<"\n\t--------- ---- -----";
	while((c=='y')||(c=='Y'))
	{
		cout<<"\nEnter Info for Node : ";
		cin>>a;
		s.push(s.create(a));
		s.display(s.topout());
		cout<<"\nPress 'Y' for more nodes : ";
		cin>>c;
	}
}

void remove()	
{	
	char c='y';
	cout<<"\n\tDeletion from Stack : ";
	cout<<"\n\t-------- ---- -----";
	while((c=='y')||(c=='Y'))
	{
		cout<<"\nPop ?(Y/N) : ";
		cin>>c;
		if((c=='Y')||(c=='y'))
		{
			cout<<"\nThe Element to be Deleted is : "<<s.delout();
			s.pop();
		}
		s.display(s.topout());
	}
}

void main()
{
	char ch;
	clrscr();
	menu:
		printhead();
		cout<<"\n1.Insertion into Stack";
		cout<<"\n2.Deletion from Stack";
		cout<<"\n3.Exit";
		cout<<"\nEnter Choice : ";
		cin>>ch;
		switch(ch)
		{
			case 1:
				insert();
				break;
			case 2:
				remove();
				break;
			case 3:
				goto quit;
			default:
				goto menu;
		}
		printfoot();
	quit:
		getch();
}
Last edited on
Topic archived. No new replies allowed.