-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
99 lines (97 loc) · 1.5 KB
/
stack.c
File metadata and controls
99 lines (97 loc) · 1.5 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
99
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
typedef struct head
{
NODE *head;
}LIST;
void create(LIST *l)
{
NODE *tmp;
tmp = (NODE*)malloc(sizeof(NODE));
int el;
printf("Enter the first element of the linked list\n");
scanf("%d",&el);
l->head->data = el;
l->head->next=NULL;
}
void push(LIST *l,int ele)
{
NODE *tmp,*p;
tmp = (NODE*)malloc(sizeof(NODE));
tmp->data = ele;
tmp->next = l->head;
l->head = tmp;
}
void pop(LIST *l)
{
NODE *tmp;
tmp = l->head;
l->head = l->head->next;
free(tmp);
}
void disp(LIST *l)
{
NODE *p = l->head;
printf("\nThe list is\n");
if(l->head == NULL)
printf("Empty\n");
else
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n\n");
}
}
void rev(LIST *l)
{
NODE *cur,*prev,*next;
cur = l->head;
prev = NULL;
while(cur!=NULL)
{
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
l->head = prev;
}
int main()
{
LIST *l;
l->head;
create(&l);
int ch,ele,res,n,n1;
do
{
printf("1.Push\n2.Pop\n3.Disp\n4.Rev\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter ele\n");
scanf("%d",&ele);
push(&l,ele);
break;
case 2:
pop(&l);
break;
case 3:
disp(&l);
break;
case 4:
rev(&l);
break;
}
}while(ch<5);
return 0;
}