mirror of
https://github.com/Manoj-HV30/ds-lab-codes.git
synced 2026-05-16 19:35:22 +00:00
init
This commit is contained in:
@@ -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());
|
||||
}
|
||||
Reference in New Issue
Block a user