0% found this document useful (0 votes)
11 views24 pages

C Practial2

The document contains a series of programming experiments focused on data structures, specifically stack and queue implementations in C. Each experiment includes a menu-driven program that allows users to perform various operations such as push, pop, enqueue, and dequeue. The experiments cover different types of queues, including linear, circular, input-restricted, and output-restricted queues.

Uploaded by

deepsamalarc212
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)
11 views24 pages

C Practial2

The document contains a series of programming experiments focused on data structures, specifically stack and queue implementations in C. Each experiment includes a menu-driven program that allows users to perform various operations such as push, pop, enqueue, and dequeue. The experiments cover different types of queues, including linear, circular, input-restricted, and output-restricted queues.

Uploaded by

deepsamalarc212
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

C. V.

RAMAN GLOBAL UNIVERSITY


BHUBANESWAR, ODISHA – 752054
DEPARTEMENT OF COMPUTER SCIENCE & ENGINEERING

DS LABORATORY
RECORD

NAME :- SHREYASHREE ADAK


REGESTRATIO 2501020178
N NO :-
SEMESTER:- 2ND
BRANCH:- CSE (CORE)
EXPERIMENT 2
AIM OF THE EXPERIMENT :-Menu driven program on
stack.
#include <stdio.h>

#define MAX 5

int stack[MAX];

int top = -1;

/* PUSH operation */

void push() {

int value;

if (top == MAX - 1)

printf("Stack Overflow\n");

else

printf("Enter value to push: ");

scanf("%d", &value);

stack[++top] = value;

printf("Pushed %d successfully\n", value);

/* POP operation */

void pop()
{

if (top == -1)

printf("Stack Underflow\n");

else

printf("Popped element: %d\n", stack[top]);

top--;

/* PEEK operation */

void peek()

if (top == -1)

printf("Stack is Empty\n");

else

printf("Top element (PEEK): %d\n", stack[top]);

int main()

{
int choice = 0;

while (choice != 4)

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

printf("1. PUSH\n");

printf("2. POP\n");

printf("3. PEEK\n");

printf("4. EXIT\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

printf("Exiting program\n");

break
default:

printf("Invalid choice\n");

} return 0;

}
EXPERIMENT 3:-

AIM:- MENU DRIVEN LINEAR QUEUE


#include <stdio.h>

#define SIZE 5

int queue[SIZE];

int front = -1, rear = -1;

/* Enqueue Operation */

void enqueue(int val)

if (rear == SIZE - 1)

printf("Queue Overflow! Cannot insert.\n");

return;

if (front == -1) // First insertion

front = 0;

rear++;

queue[rear] = val;

printf("%d inserted successfully.\n", val);

/* Dequeue Operation */

void dequeue()

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


printf("Queue Underflow! Cannot delete.\n");

return;

printf("%d deleted successfully.\n", queue[front]);

front++;

if (front > rear) // Reset when empty

front = rear = -1;

/* Display Queue */

void display()

int i;

if (front == -1)

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

return;

printf("Queue Elements: ");

for (i = front; i <= rear; i++)

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

printf("\n");

/* Main Program */

int main() {
int choice, val;

while (1)

{ printf("\n---- QUEUE MENU ----\n");

printf("1. Enqueue (Insert)\n");

printf("2. Dequeue (Delete)\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

{ case 1:

printf("Enter value to insert: ");

scanf("%d", &val);

enqueue(val);

break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 4:

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

return 0;
default;

printf("Invalid Choice! Try again.\n");

return 0;

}
EXPERIMENT 4

AIM:- MENU DRIVEN CIRCULAR QUEUE


#include <stdio.h>

#define MAXSIZE 5

int queue[MAXSIZE];

int front = -1, rear = -1;

/* Enqueue Operation */

void enqueue(int val)

{ /* Overflow Check */

if ((rear == MAXSIZE - 1 && front == 0) ||

(rear + 1 == front))

printf("Circular Queue Overflow!\n");

return;

/* First Insertion */

if (front == -1)

front = rear = 0;

/* Rear at Last Position */

else if (rear == MAXSIZE - 1)

rear = 0;

/* Normal Increment */
else

rear++;

queue[rear] = val;

printf("%d inserted successfully.\n", val);

/* Dequeue Operation */

void dequeue()

/* Underflow Check */

if (front == -1)

printf("Circular Queue Underflow! Queue is Empty.\n");

return;

printf("%d deleted successfully.\n", queue[front]);

/* Only One Element */

if (front == rear)

front = rear = -1;

/* Front at Last Position */

else if (front == MAXSIZE - 1)

front = 0; }

/* Normal Increment */
else

front++; }

/* Display Queue */

void display()

{ int i;

if (front == -1)

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

return;

printf("Queue Elements: ");

if (front <= rear)

for (i = front; i <= rear; i++)

printf("%d ", queue[i]); }

else

for (i = front; i < MAXSIZE; i++)

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

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

printf("%d ", queue[i]); }

printf("\n");

/* Main Program */

int main()
{ int choice, val;

while (1)

printf("\n---- CIRCULAR QUEUE MENU ----\n");

printf("1. Enqueue (Insert)\n");

printf("2. Dequeue (Delete)\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("Enter value to insert: ");

scanf("%d", &val);

enqueue(val);

break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 4:

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

return 0;

default:
printf("Invalid Choice! Try Again.\n");

} }

return 0;

}
EXPERIMENT 5

AIM:- MENU DEIVEN INPUT RESTRICTED QUEUE


#include <stdio.h>

#define MAXSIZE 5

int deque[MAXSIZE];

int front = -1, rear = -1;

/* Insert at Rear (Only Allowed) */

void insertRear(int val)

/* Overflow Check */

if (rear == MAXSIZE - 1)

printf("Queue Overflow!\n");

return;

/* First Insertion */

if (front == -1)

front = rear = 0;

else

rear++;

deque[rear] = val;

printf("%d inserted at rear.\n", val);

/* Delete from Front */

void deleteFront()

{ if (front == -1)
{

printf("Queue Underflow!\n");

return;

printf("%d deleted from front.\n", deque[front]);

if (front == rear)

front = rear = -1;

else

front++;

/* Delete from Rear */

void deleteRear()

if (front == -1)

printf("Queue Underflow!\n");

return;

printf("%d deleted from rear.\n", deque[rear]);

if (front == rear)

front = rear = -1;

else

rear--;

/* Display Deque */

void display()

{ int i;
if (front == -1)

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

return;

printf("Queue Elements: ");

for (i = front; i <= rear; i++)

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

printf("\n");

/* Main Program */

int main()

int choice, val;

while (1)

printf("\n---- INPUT RESTRICTED DEQUE MENU ----\n");

printf("1. Insert at Rear\n");

printf("2. Delete from Front\n");

printf("3. Delete from Rear\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1: printf("Enter value: ");


scanf("%d", &val);

insertRear(val);

break;

case 2:

deleteFront();

break;

case 3:

deleteRear();

break;

case 4:

display();

break;

case 5:

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

return 0;

default:

printf("Invalid Choice! Try Again.\n");

} }

return 0; }
EXPERIMENT 6

AIM:- MENU DRIVEN OUTPUT RESTRICTED QUEUE


#include <stdio.h>

#define MAXSIZE 5

int deque[MAXSIZE];

int front = -1, rear = -1;

/* Insert at Rear */

void insertRear(int val)

{ if (rear == MAXSIZE - 1)

printf("Queue Overflow at Rear!\n");

return; }

if (front == -1)

front = rear = 0;

else

rear++;

deque[rear] = val;

printf("%d inserted at rear.\n", val);

/* Insert at Front */

void insertFront(int val)

{ if (front == 0)

printf("Queue Overflow at Front!\n");

return; }

if (front == -1)
front = rear = 0;

else

front--;

deque[front] = val;

printf("%d inserted at front.\n", val);

/* Delete from Front (Only Allowed) */

void deleteFront()

if (front == -1)

{ printf("Queue Underflow!\n");

Return; }

printf("%d deleted from front.\n", deque[front]);

if (front == rear)

front = rear = -1;

else

front++; }

/* Display */

void display(){

int i;

if (front == -1)

{ printf("Queue is Empty.\n");

return;

printf("Queue Elements: ");

for (i = front; i <= rear; i++)

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


printf("\n");

/* Main Program */

int main()

{ int choice, val;

while (1)

{ printf("\n---- OUTPUT RESTRICTED DEQUE MENU ----\n");

printf("1. Insert at Rear\n");

printf("2. Insert at Front\n");

printf("3. Delete from Front\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

{ case 1:

printf("Enter value: ");

scanf("%d", &val);

insertRear(val);

break;

case 2:

printf("Enter value: ");

scanf("%d", &val);

insertFront(val);

break;

case 3:

deleteFront();
break;

case 4:

display();

break;

case 5:

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

return 0;

default:

printf("Invalid Choice! Try Again.\n");

}}

return 0; }

You might also like