-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack_Implementation.cpp
102 lines (95 loc) · 2.16 KB
/
Stack_Implementation.cpp
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
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
void push(struct node **,int);
int pop(struct node **);
void display(struct node **);
int main()
{
int num,ch,element;
struct node *start=NULL;
while(1)
{
system("CLS");
cout<<"MENU\n-----\n(1) PUSH\n(2) POP\n(3) DISPLAY\n(4) EXIT\n\nENTER CHOICE : ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter element to enter : ";
cin>>num;
push(&start,num);
cout<<"Entered element successfully...Press any key.";
getch();
break;
case 2: element=pop(&start);
if(element!=NULL)
cout<<"Deleted element : "<<element;
else
cout<<"EMPTY STACK......POP operation failed !";
getch();
break;
case 3: cout<<endl;
display(&start);
getch();
break;
case 4: exit(0);
default: cout<<"Wrong choice....Try again....";
getch();
}
}
return 0;
}
void push(struct node **start,int num)
{
struct node *temp=*start;
struct node *neww=new struct node;
neww->data=num;
neww->next=NULL;
if(*start==NULL)
*start=neww;
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=neww;
}
}
int pop(struct node **start)
{
int t;
struct node *temp=*start,*prev=*start;
if(*start==NULL)
return NULL;
else
{
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
t=temp->data;
if(temp==*start)
*start=NULL;
free(temp);
return t;
}
}
void display(struct node **start)
{
int t;
t=pop(start);
if(t==NULL)
return;
else
{
cout<<"| "<<t<<" |"<<endl;
display(start);
push(start,t);
}
}