This commit is contained in:
Light
2025-12-08 21:20:15 +05:30
parent a7625875fb
commit 9edd9ac19f
5 changed files with 431 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *ptr;
};
struct Node *top = NULL;
void push(int v) {
struct Node *new = (struct Node *)malloc(sizeof(struct Node));
new->data = v;
new->ptr = top;
top = new;
}
int pop() {
struct Node *temp = top;
int v = temp->data;
top = temp->ptr;
free(temp);
return v;
}
int peek() {
if (top == NULL) {
printf("Stack empty\n");
return -1;
}
return top->data;
}
void display() {
struct Node *temp = top;
printf("Stack: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->ptr;
}
printf("\n");
}
int main() {
push(10);
push(20);
push(30);
display();
printf("Popped: %d\n", pop());
display();
printf("Top: %d\n", peek());
}