-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoal24.c
86 lines (78 loc) · 1.46 KB
/
goal24.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
// implementing stack using linked list
// stack operations using linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void linkedlisttraversal(struct node *ptr)
{
printf("the stack is:\n");
while (ptr != NULL)
{
printf("Element is: %d\n", ptr->data);
ptr = ptr->next;
}
}
int isempty(struct node *top)
{
if (top == NULL)
{
return 1;
}
return 0;
}
int isfull(struct node *top)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
if (p == NULL)
{
return 1;
}
return 0;
}
struct node *push(struct node *top, int x)
{
if (isfull(top))
{
printf("stack overflow,cannot push any element.\n");
}
else
{
struct node *n = (struct node *)malloc(sizeof(struct node));
n->data = x;
n->next = top;
top = n;
return top;
}
}
int pop(struct node **top)
{
if (isempty(*top))
{
printf("the stack is underflow, cannot pop any element.\n");
return 0;
}
else
{
struct node *n = *top;
*top = (*top)->next;
int x = n->data;
free(n);
return x;
}
}
int main()
{
struct node *top = NULL;
top = push(top, 11);
top = push(top, 12);
top = push(top, 13);
linkedlisttraversal(top);
int element = pop(&top);
printf("popped element is %d\n", element);
linkedlisttraversal(top);
return 0;
}