-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstackusingsll.c
More file actions
98 lines (96 loc) · 1.3 KB
/
Copy pathstackusingsll.c
File metadata and controls
98 lines (96 loc) · 1.3 KB
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
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct node *front=NULL;
struct node *rear=NULL;
struct node
{
int key;
struct node *addr;
};
void push(int k)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(front==NULL)
{
temp->key=k;
temp->addr=NULL;
front=rear=temp;
}
else
{
temp->addr=NULL;
temp->key=k;
rear->addr=temp;
rear=temp;
}
}
void pop()
{
if(front==NULL)
{
printf("The Stack is empty!!\n");
}
else if(front->addr==NULL)
{
free(front);
front=NULL;
rear=NULL;
}
else
{
struct node *temp;
temp=front;
while(temp->addr!=rear)
{
temp=temp->addr;
}
temp->addr=NULL;
free(rear);
rear=temp;
}
}
void print()
{
if(front==NULL)
{
printf("The Stack is empty!!\n");
}
else
{
printf("The Stack elements are:\n");
struct node *a;
a=front;
while(a!=NULL)
{
printf("%d\n",a->key);
a=a->addr;
}
}
}
void main()
{
int a,k;
while(1)
{
printf("Select your option:\n");
printf("1.Push\n2.Pop\n3.Print elements\n4.exit\n");
scanf("%d",&a);
switch(a)
{
case 1:printf("Enter the element to be pushed into the stack:");
scanf("%d",&k);
push(k);
break;
case 2:pop();
break;
case 3:print();
break;
case 4:exit(0);
default: printf("Error!!\n");
}
}
getch();
}