-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
60 lines (51 loc) · 828 Bytes
/
Copy pathStack.cpp
File metadata and controls
60 lines (51 loc) · 828 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
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *head = NULL;
void push(int val){
struct node *new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = val;
new_node->next = head;
head = new_node;
}
void pop(){
if(head == NULL){
printf("stack is empty\n");
}else{
struct node *temp = head;
head = head -> next;
free(temp);
}
}
int top(){
return head->data;
}
void show_top(){
printf("%d" , head->data);
}
void print_stack(){
if(head == NULL)
printf("stack is empty\n");
else{
struct node *curr = head;
while(curr != NULL){
printf("%d " , curr->data);
curr = curr->next;
}
}
}
int main(){
push(3);
push(6);
push(9);
push(11);
print_stack();
printf("\n");
pop();
print_stack();
printf("\n");
show_top();
}