forked from bhaveshkalra/DSA-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks using linked lists.c
167 lines (142 loc) · 2.86 KB
/
stacks using linked lists.c
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
//int is_empty(struct node*);
void push_an_element(int ,struct node*);
int pop_an_element(struct node*);
void display_stack(struct node*);
int top_element(struct node*);
void delete_stack(struct node*);
struct node*create_stack()
{
struct node*ptr;
ptr=malloc(sizeof(struct node*));
ptr->data=0;
ptr->next=NULL;
return ptr;
}
int main()
{
int i,data_value,data_2;
int k;
struct node *head;
head=create_stack();
char ch='y';
printf("\n");
do
{
printf("menu of operations to be performed:\n");
printf("1.push an element into stack\n");
printf("2.pop an element\n");
printf("3.display stack\n");
printf("4.what is top element?\n5.delete stack\n6.exit\n");
printf("enter your choice from menu:\n");
scanf("%d",&k);
switch(k)
{
case 1:printf("enter data to be pushed:\n");
scanf("%d",&data_value);
push_an_element(data_value,head);
display_stack(head);
break;
case 2: data_value=pop_an_element(head);
printf("the popped element is:%d\n",data_value);
display_stack(head);
break;
case 3:printf("my stack is:\n");
display_stack(head);
break;
case 4:
data_value=top_element(head);
printf("top element is=%d\n",data_value);
break;
case 5:
if(ch=='y')
{delete_stack(head);
}
break;
case 6:break;
default: printf("please enter any one choice:\n");
}
}
while(k!=6 || k>6);
return 0;
}
void push_an_element(int data_value ,struct node* ptr)
{
struct node*temp;
temp=malloc(sizeof(struct node*));
temp->data=data_value;
if (ptr->next==NULL)
{
temp->next=ptr->next;
ptr->next=temp;
}
else
{
temp->next=ptr->next;
ptr->next=temp;
}
}
int pop_an_element(struct node*ptr)
{
struct node*temp;
if(ptr->next==NULL)
{printf("underflow/n");
return;}
int data;
if((ptr->next)->next==NULL)
{
temp=ptr->next;
data=temp->data;
ptr->next=NULL;
free(temp);
}
else
{
temp=ptr->next;
data=temp->data;
ptr->next=(ptr->next)->next;
free(temp);
}
return data;
}
void display_stack(struct node*ptr)
{
if(ptr->next==NULL)
{
printf("stack is empty underflow condition\n");
return;
}
printf("TOP->");
while(ptr->next!=NULL)
{
ptr=ptr->next;
printf("%d->",ptr->data);
}
printf("NULL\n");
}
int top_element(struct node *ptr)
{
int data;
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node*));
temp=ptr->next;
data=temp->data;
return data;
}
void delete_stack(struct node*ptr)
{
struct node*temp;
while(ptr->next!=NULL)
{
temp=ptr->next;
ptr->next=temp->next;
free(temp);
}
printf("the Stack is deleted\n");
}