Hello friends!!!!!
Linked stack
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* link;
} Node;
typedef struct
{
Node* top;
}Stack;
void initialisation(Stack *s)
{
s->top = NULL;
}
int isEmpty(Stack *s)
{
return s->top == NULL;
}
void push(Stack *s, int value)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->link = s->top;
s->top = newNode;
}
int pop(Stack *s)
{
Node *temp;
int pop_value;
if(isEmpty(s))
{
printf("Stack Underflow\n");
return -1;
}
temp = s->top;
pop_value = temp->data;
s->top = s->top->link;
free(temp);
return pop_value;
}
int peek(Stack *s)
{
if(isEmpty(s))
{
printf("Stack is empty\n");
return -1;
}
return s->top->data;
}
void display(Stack *s)
{
if(isEmpty(s))
{
printf("Stack is empty\n");
return;
}
else{
Node* temp = s->top;
printf("Stack elements: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->link;
}
printf("\n");
}
}
int main() {
Stack stack;
initialisation(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
push(&stack, 40);
push(&stack, 50);
display(&stack);
printf("Peek value in the stack is %d\n", peek(&stack));
printf("Popped element is %d\n", pop(&stack));
display(&stack);
return 0;
}