-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
59 lines (48 loc) · 793 Bytes
/
stack.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
//
// Created by moezgen on 12/20/24.
//
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *top;
void push(int x) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL) {
printf("Stack Overflow\n");
return;
}
temp->data = x;
temp->next = top;
top = temp;
}
int pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return -1;
}
struct node *temp = top;
int x = temp->data;
top = temp->next;
free(temp);
return x;
}
int peek() {
if (top == NULL) {
printf("Stack is empty\n");
return -1;
}
return top->data;
}
int main() {
top = NULL;
push(5);
push(6);
push(3);
push(4);
printf("%d\n", pop());
printf("%d\n", peek());
return 0;
}