Ds Experiment 10
Ds Experiment 10
10
Aim: Write programs to implement stack using the linked list data
structures
/**
* Stack implementation using linked list
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> // For INT_MIN
// Stack size
int size = 0;
void main()
{
int choice, data;
while(1)
{
/* Menu */
printf("------------------------------------\n");
printf(" STACK IMPLEMENTATION PROGRAM \n");
printf("------------------------------------\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Size\n");
printf("4. Exit\n");
printf("------------------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter data to push into stack: ");
scanf("%d", &data);
case 2:
data = pop();
case 3:
printf("Stack size: %d\n", size);
break;
case 4:
printf("Exiting from app.\n");
exit(1);
break;
default:
printf("Invalid choice, please try again.\n");
}
printf("\n\n");
}
/**
* Functiont to push a new element in stack.
*/
void push(int element)
{
// Create a new node and push to stack
struct stack * newNode = (struct stack *) malloc(sizeof(struct
stack));
// Check stack overflow
if (size >= CAPACITY)
{
printf("Stack Overflow, can't add more element to stack.\n");
return;
}
// Assign data to new node in stack
newNode->data = element;
/**
* Function to pop element from top of stack.
*/
int pop()
{
int data = 0;
struct stack * topNode;
return data;
}