-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab13(stack_usingLL).c
90 lines (85 loc) · 1.82 KB
/
lab13(stack_usingLL).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
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 3
struct node
{
int data;
struct node *next;
}*newnode,*top,*temp;
int count=0;
void push(int a) //push is essentially inserting the newnode to the beginning of the linked list
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = a;
if (count == MAX_SIZE) //checking the full condition
{
printf("Stack overflow..");
}
else if (top == NULL)
{
top = newnode;
newnode->next = NULL;
count++; //to check whether the stack has reached to the MAX_SIZE
printf("node pushed successfully..");
}
else{
newnode->next = top;
top = newnode;
count++;
printf("node pushed successfully..");
}
}
void pop(){
if (top == NULL)
{
printf("stack underflow..");
}
else{
temp = top;
top = top->next;
free(temp);
count --;
printf("node popped succesfully..");
}
}
void traverse(){
if (top == NULL)
{
printf("nothing here to display..");
}
else{
temp = top;
while (temp->next!=NULL)
{
printf("%d -> ",temp->data);
temp = temp->next;
}
printf("%d",temp->data);
}
}
void main()
{
int ch,data;
do{
printf("\n\n1.PUSH\n2.POP\n3.TRAVERSE\n");
scanf("%d",&ch);
switch (ch)
{
case 1:
printf("enter the number : ");
scanf("%d",&data);
push(data);
break;
case 2:
pop();
break;
case 3:
traverse();
break;
default:
break;
}
printf("\ndo you want to continue (0/1) : ");
scanf("%d",&ch);
}while(ch!=0);
}