exp 3,5,6,7: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-12-27 19:40:13 +05:30
parent 8fa27b556e
commit c460e95304
9 changed files with 266 additions and 3 deletions

48
6.c Normal file
View File

@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Node node;
struct Node {
int id;
struct Node *next;
};
int peek(node *stack) { return stack->id; }
void push(node **stack, int id) {
node *new = malloc(sizeof(node));
new->id = id;
new->next = *stack;
*stack = new;
}
int pop(node **stack) {
int id = (*stack)->id;
node *next = (*stack)->next;
free(*stack);
*stack = next;
return id;
}
void traverse(node *stack) {
while (stack != NULL) {
printf("%d ", stack->id);
stack = stack->next;
}
printf("\n");
}
int main() {
node *stack = NULL;
push(&stack, 4);
push(&stack, 99);
push(&stack, 23);
printf("Pop: %d\n", pop(&stack));
printf("Traverseal: ");
traverse(stack);
// free
while (stack != NULL)
pop(&stack);
}