-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstcak_using_ll.c
More file actions
103 lines (95 loc) · 1.57 KB
/
stcak_using_ll.c
File metadata and controls
103 lines (95 loc) · 1.57 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
100
101
102
#include<stdio.h>
#include<stdlib.h>
struct nodes
{
int info;
struct nodes *next;
};
void push();
void pop();
void traverse();
typedef struct nodes snodes;
snodes *top=NULL,*x,*y;
int nodes=0,j,k=0;
char a[20];
void main()
{
int ch;
while(1)
{
printf("Enter your choice:\n 1.Push elements into stack\n2.Pop\n3.Traverse\n4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
traverse();
break;
default:
exit(0);
}
}
}
void push()
{
int i;
if(top!=NULL)
{
x=(snodes*)malloc(sizeof(snodes));
printf("Enter data:");
scanf(" %d",&(x->info));
/*The %d conversion specifier won't automatically skip any leading whitespace, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately. scanf(" %d", &c);*/
x->next=NULL;
top=x;
printf("If you wish to continue press 1 else 0:");
scanf("%d",&i);
while(i)
{
y=(snodes*)malloc(sizeof(snodes));
nodes++;
printf("Enter data:");
scanf(" %d",&y->info);
y->next=x;
top=y;
x=y;
printf("If you wish to continue,press 1 ,else 0:");
scanf("%d",&i);
}
}
else
{
x=top;
while(i)
{
y=(snodes*)malloc(sizeof(snodes));
nodes++;
printf("Enter data:");
scanf(" %d",&y->info);
y->next=x;
top=y;
x=y;
printf("If you wish to continue,press 1 ,else 0:");
scanf("%d",&i);
}
}
}
void pop()
{
x=top;
top=top->next;
free(x);
}
void traverse()
{
x=top;
while(x!=NULL)
{
printf("%d\n",x->info);
x=x->next;
}
}