-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_list.c
More file actions
70 lines (55 loc) · 965 Bytes
/
stack_list.c
File metadata and controls
70 lines (55 loc) · 965 Bytes
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
//stack_list.c
#include <stdio.h>
#include <stdlib.h>
typedef
struct list
{ int data;
struct list *next;
} node;
node *top=NULL;
int isEmpty() {
if(top==NULL) return 1;
else return 0;
}
int onTop() {
return top->data;
}
node* newNode(int element)
{
node *temp=(node*)malloc(sizeof(node));
temp->data=element;
temp->next=NULL;
return temp;
}
void push(int element)
{
node *temp=newNode(element);
if(isEmpty())
top=temp;
else
{ temp->next=top;
top=temp;
}
}
void pop()
{ int element;
node *temp;
if(isEmpty())
printf("Stack empty\n");
else
{ temp=top;
top=top->next;
free(temp);
}
}
main()
{
push(10); push(11); push(12); push(101); push(21);
if(!isEmpty())
printf("On top we have %d\n",onTop());
pop(); pop(); pop();
if(!isEmpty())
printf("On top we have %d\n",onTop());
pop(); pop();
pop(); // should get stack empty message
}