0% found this document useful (0 votes)
8 views8 pages

Stacks Programs

The document presents a menu-driven C program that implements stack operations using both arrays and linked lists. It includes functions for pushing, popping, displaying elements, checking if the stack is empty, and retrieving the top element. The program allows users to interactively perform these operations until they choose to exit.

Uploaded by

247r1a05a1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Stacks Programs

The document presents a menu-driven C program that implements stack operations using both arrays and linked lists. It includes functions for pushing, popping, displaying elements, checking if the stack is empty, and retrieving the top element. The program allows users to interactively perform these operations until they choose to exit.

Uploaded by

247r1a05a1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

// Menu Driven program in C implementing operations on stacks using arrays.

#include <stdio.h>

#include<stdlib.h>

int stack[100];

int size;

int top=-1;

void push()

if(top==size-1)

printf("\nStack is overflow.");

else

top++;

printf("Enter stack element : ");

scanf("%d",&stack[top]);

void display()

int i;

if(top==-1)

printf("\nStack is empty.");

else

printf("\nStack elements : ");


for(i=0;i<=top;i++)

printf(" %d",stack[i]);

void pop()

int temp;

if(top==-1)

printf("Stack is underflow.");

else

temp=stack[top];

top--;

printf("\nDeleted element : %d",temp);

void topofthestack()

if(top==-1)

printf("Stack is empty.");

else

printf("Top of the stack : %d",stack[top]);


}

void isempty()

if(top==-1)

printf("Stack is empty.");

else

printf("Stack is not empty.");

int main()

int opt;

printf("Enter size of the stack : ");

scanf("%d",&size);

printf("1.Push 2. Pop 3. Top of the stack 4. isEmpty 5. Display 6.Exit \n");

while(1)

printf("\nEnter your option : ");

scanf("%d",&opt);

switch(opt)

case 1 : push();

break;

case 2 : pop();

break;
case 3 : topofthestack();

break;

case 4 : isempty();

break;

case 5 : display();

break;

case 6 : exit(1);

default : printf("\nInvalid option.");

return 0;

//program in C implementing all the stack operations using linked list

#include <stdio.h>

#include <stdlib.h>

struct node

int val;

struct node *next;

};

struct node *head = NULL;

void push();

void pop();

void display();
int main()

int choice;

printf("\n********* Stack operations using linked list *********\n");

printf("\n----------------------------------------------\n");

do

printf("\n\nChoose one from the below options...\n");

printf("\n1. Push\n2. Pop\n3. Show\n4. Exit");

printf("\n Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1: push();

break;

case 2: pop();

break;

case 3: display();

break;

case 4: printf("Exiting...\n");

break;

default:printf("Please Enter a valid choice\n");

} while (choice != 4);


// Free allocated memory before exiting

while (head != NULL)

pop();

return 0;

void push()

int val;

struct node *ptr = (struct node*)malloc(sizeof(struct node));

if (ptr == NULL)

printf("Unable to allocate memory\n");

exit(EXIT_FAILURE);

printf("Enter the value: ");

scanf("%d", &val);

ptr->val = val;

ptr->next = head;

head = ptr;

printf("Item pushed\n");

void pop()

{
if (head == NULL)

printf("Underflow\n");

return;

struct node *ptr = head;

printf("Popped item: %d\n", ptr->val);

head = head->next;

free(ptr);

void display()

struct node *ptr = head;

if (ptr == NULL)

printf("Stack is empty\n");

else

printf("Printing Stack elements\n");

while (ptr != NULL)

printf("%d\n", ptr->val);

ptr = ptr->next;

}
}

You might also like