0% found this document useful (0 votes)
19 views6 pages

Stack 2

The document contains code for implementing stack and queue operations using arrays. For stack, it includes functions for push, pop and display operations. For queue, it includes functions for insertion, serving and display from front to rear.

Uploaded by

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

Stack 2

The document contains code for implementing stack and queue operations using arrays. For stack, it includes functions for push, pop and display operations. For queue, it includes functions for insertion, serving and display from front to rear.

Uploaded by

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

Name – Alok Kumar Sec-c Roll no-06

Q1- Write a code for menu driven program for performing push , pop and display operations
on Stack which is implemented using an array using double pointer.
Code:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void push(struct Node** top, int value);

void pop(struct Node** top);

void display(struct Node* top);

int main() {

struct Node* top = NULL;

int choice, value;

do {

printf("1. Push\n 2.pop\n 3.Display\n 4.Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to push: ");

scanf("%d", &value);

push(&top, value);

break;

case 2:

pop(&top);

break;

case 3:

display(top);
break;

case 4:

printf("Exiting the program.\n");

break;

default:

printf("Invalid choice.\n");

} while (choice != 4);

return 0;

void push(struct Node** top, int value) {

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

newNode->data = value;

newNode->next = *top;

*top = newNode;

void pop(struct Node** top) {

if (*top == NULL) {

printf("Stack is empty.\n");

return;

struct Node* temp = *top;

*top = (*top)->next;

printf("Element %d popped from the stack.\n", temp->data);

free(temp);

void display(struct Node* top) {

if (top == NULL) {

printf("Stack is empty.\n");

return;

}
printf("Stack elements: ");

while (top != NULL) {

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

top = top->next;

printf("\n");

}
Name-Alok kumar Sec-C Roll no-06

Q2: write a program to queue using array operation insertion ,serving and display front to
rear

Code:

#include <stdio.h>

#include <stdlib.h>

typedef struct {

int array[10];

int front;

int rear;

} Queue;

void initQueue(Queue *queue);

void insertion(Queue *queue, int value);

int serving(Queue *queue);

void display(Queue *queue);

int main() {

Queue queue;

int choice, value;

initQueue(&queue);

do {

printf("1. insertion\n 2.serving\n 3.Display\n 4.Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insertion: ");

scanf("%d", &value);

insertion(&queue, value);
break;

case 2:

printf("Serving value: %d\n", serving(&queue));

break;

case 3:

printf("Queue contents:\n");

display(&queue);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice!\n");

} while (choice != 4);

return 0;

void initQueue(Queue *queue) {

queue->front = -1;

queue->rear = -1;

void insertion(Queue *queue, int value) {

if (queue->rear == 10 - 1) {

printf("Queue is full\n");

return;

if (queue->front == -1) {

queue->front = 0;

}
queue->array[++(queue->rear)] = value;

int serving(Queue *queue) {

if (queue->front == -1 || queue->front > queue->rear) {

printf("Queue is empty\n");

return -1;

return queue->array[(queue->front)++];

void display(Queue *queue) {

if (queue->front == -1 || queue->front > queue->rear) {

printf("Queue is empty.\n");

return;

for (int i = queue->front; i <= queue->rear; i++) {

printf("%d ", queue->array[i]);

printf("\n");

You might also like