0% found this document useful (0 votes)
5 views4 pages

Stack and Queue Implementation in C

The document contains C code for implementing a stack and a queue using linked lists. It includes functions for pushing, popping, inserting, deleting, and displaying elements in both data structures. The code features a menu-driven interface for user interaction.

Uploaded by

rishikreddy7767
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)
5 views4 pages

Stack and Queue Implementation in C

The document contains C code for implementing a stack and a queue using linked lists. It includes functions for pushing, popping, inserting, deleting, and displaying elements in both data structures. The code features a menu-driven interface for user interaction.

Uploaded by

rishikreddy7767
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

#include<stdio.

h>

struct Node

int data;

struct Node *next;

}*top = NULL;

void push(int);

void pop();

void display();

int main()

int choice, value;

printf("\n:: Stack using Linked List ::\n");

while(1){

printf("\n****** MENU ******\n");

printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice){

case 1: printf("Enter the value to be insert: ");

scanf("%d", &value);

push(value);

break;

case 2: pop(); break;

case 3: display(); break;

case 4: exit(0);

default: printf("\nWrong selection!!! Please try again!!!\n");

void push(int value)

struct Node *newNode;

newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = value;

if(top == NULL)

newNode->next = NULL;

else

newNode->next = top;

top = newNode;

printf("\nInsertion is Success!!!\n");

void pop()

if(top == NULL)

printf("\nStack is Empty!!!\n");

else{

struct Node *temp = top;

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

top = temp->next;

free(temp);

void display()

if(top == NULL)

printf("\nStack is Empty!!!\n");

else{

struct Node *temp = top;

while(temp->next != NULL){

printf("%d--->",temp->data);

temp = temp -> next;

printf("%d--->NULL",temp->data);

Stack single linked


#include<stdio.h>

struct Node

int data;

struct Node *next;

}*front = NULL,*rear = NULL;

void insert(int);

void delete();

void display();

int main()

int choice, value;

printf("\n:: Queue Implementation using Linked List ::\n");

while(1){

printf("\n****** MENU ******\n");

printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice){

case 1: printf("Enter the value to be insert: ");

scanf("%d", &value);

insert(value);

break;

case 2: delete(); break;

case 3: display(); break;

case 4: exit(0);

default: printf("\nWrong selection!!! Please try again!!!\n");

void insert(int value)

struct Node *newNode;


newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode -> next = NULL;

if(front == NULL)

front = rear = newNode;

else{

rear -> next = newNode;

rear = newNode;

printf("\nInsertion is Success!!!\n");

void delete()

if(front == NULL)

printf("\nQueue is Empty!!!\n");

else{

struct Node *temp = front;

front = front -> next;

printf("\nDeleted element: %d\n", temp->data);

free(temp);

void display()

if(front == NULL)

printf("\nQueue is Empty!!!\n");

else{

struct Node *temp = front;

while(temp->next != NULL){

printf("%d--->",temp->data);

temp = temp -> next;

printf("%d--->NULL\n",temp->data);

}}queue

You might also like