-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
58 lines (53 loc) · 1.09 KB
/
stack.cpp
File metadata and controls
58 lines (53 loc) · 1.09 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
#include <cstdio>
#include <cstring>
#include <cstdlib>
struct node{
int data;
struct node *next;
};
//top
struct node *top = NULL;
//Push function
int push(int data1){
//if stack is empty
if(top == NULL){
struct node *n = (struct node *)malloc(sizeof(struct node));
n->data = data1;
n->next = NULL;
top = n;
}else{
struct node *n = (struct node *)malloc(sizeof(struct node));
n->data = data1;
n->next = top;
top = n;
}
return 0;
}
//display function
int display(){
//if stack is empty
if(top==NULL){printf("stack is empty");return 0;}
struct node *index = top;
while(index != NULL){
printf("%d \n",index->data);
index = index->next;
}
return 0;
}
//pop function
int pop(){
//if stack is empty
if(top==NULL){printf("stack is empty");return 0;}
struct node *temp = top;
top = top->next;
free(temp);
}
int main(){
push(6);
push(7);
push(8);
display();
pop();
display();
return 0;
}