-
Notifications
You must be signed in to change notification settings - Fork 0
Description
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* link;
}Node;
typedef struct {
Node* top;
}Stack;
int is_empty(Stack* s) {
if (s->top == NULL)
{
return 1;
}
else return 0;
}
void init(Stack *s) {
s->top = NULL;
}
void push(Stack* s, int item) {
Node* NewNode = (Node*)malloc(sizeof(Node));
NewNode->link = s->top;
NewNode->data = item;
s->top = NewNode;
}
int pop(Stack* s) {
if (is_empty(s)) { printf("The Stack is empty\n"); }
else {
Node* temp = s->top;
int data = temp->data;
s->top = temp->link;
free(temp);
return data;
}
}
void print_stack(Stack* s) {
for (Node* p = s->top; p != NULL; p = p->link) {
printf("%d -> ", p->data);
}
printf("NULL\n");
}
int main() {
Stack s;
init(&s);
push(&s, 11); print_stack(&s);
push(&s, 22); print_stack(&s);
push(&s, 33); print_stack(&s);
push(&s, 44); print_stack(&s);
pop(&s); print_stack(&s);
pop(&s); print_stack(&s);
pop(&s); print_stack(&s);
pop(&s); print_stack(&s);
}