Stack 2
Stack 2
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;
};
int main() {
do {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(&top, value);
break;
case 2:
pop(&top);
break;
case 3:
display(top);
break;
case 4:
break;
default:
printf("Invalid choice.\n");
return 0;
newNode->data = value;
newNode->next = *top;
*top = newNode;
if (*top == NULL) {
printf("Stack is empty.\n");
return;
*top = (*top)->next;
free(temp);
if (top == NULL) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
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;
int main() {
Queue queue;
initQueue(&queue);
do {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
insertion(&queue, value);
break;
case 2:
break;
case 3:
printf("Queue contents:\n");
display(&queue);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
return 0;
queue->front = -1;
queue->rear = -1;
if (queue->rear == 10 - 1) {
printf("Queue is full\n");
return;
if (queue->front == -1) {
queue->front = 0;
}
queue->array[++(queue->rear)] = value;
printf("Queue is empty\n");
return -1;
return queue->array[(queue->front)++];
printf("Queue is empty.\n");
return;
printf("\n");