-
Notifications
You must be signed in to change notification settings - Fork 44
/
implementation_of_stack_using_linked_list.cpp
103 lines (97 loc) · 1.44 KB
/
implementation_of_stack_using_linked_list.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
103
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *stack;
int isempty(stack s)
{
if(s==NULL)
return 1;
else
return 0;
}
stack push(stack s,int x)
{
stack temp;
temp=(stack)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space");
exit(0);
}
temp->info=x;
temp->next=s;
s=temp;
return s;
}
stack Pop(stack s)
{
stack temp;
int x;
if(isempty(s))
{
printf("\nstack empty");
exit(0);
}
temp=s;
x=s->info;
printf("\nPopped Element is :%d",x);
s=s->next;
free(temp);
return s;
}
void display(stack s)
{
stack ptr;
ptr = s;
printf("\n The Elements In The Stack Are\n");
if (ptr ==NULL)
{
printf("\n Stack Empty\n\n");
return ;
}
while (ptr!=NULL)
{
printf("%d\n",ptr->info);
ptr=ptr->next;
}
}
int main()
{
stack s =NULL;
int x;
char ch='1';
while(ch!='4')
{
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Display");
printf("\n 4 - Quit");
printf("\nEnter your Choice");
fflush(stdin);
ch=getchar();
switch(ch)
{
case '1':
printf("\n enter the element to be pushed");
scanf("%d",&x);
s=push(s,x);
break;
case '2':
s=Pop(s);
break ;
case '3':
display(s);
break;
case '4':
break ;
default:
printf("\n Wrong Choice Try Again");
break;
}
}
return 0;
}