0% found this document useful (0 votes)
35 views

DS Rec

Uploaded by

gokula1310
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)
35 views

DS Rec

Uploaded by

gokula1310
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/ 44

EX NO: 05 IMPLEMENTATION OF CIRCULAR DOUBLY

DATE:20.09.24 LINKED LIST


+

AIM:
To write a C program to perform the implementation of circular doubly linked list.
ALGORITHM:
Main Method:
Step 1: Initialize head as NULL.
Step 2: Enter an infinite loop to display the menu repeatedly.
Step 3: Print the menu options for user selection as 1.Insert at end, 2.Delete at end, 3.Display
List and 4. Exit the program.
Step 4: Read the user's choice from input.
Step 5: Based on the choice, call the corresponding function for insertion, deletion, display,
or exit the program.
Create Node:
Step 1: Allocate memory for a new node.
Step 2: Check if memory allocation was successful.
Step 3: Set the data field of the new node with the provided value.
Step 4: Point next and prev of the new node to itself (self-referential).
Step 5: Return the pointer to the newly created node.
Insert at End:
Step 1: Call the createNode function to create a new node.
Step 2: Check if the list is empty (i.e., head is NULL).
Step 3:If empty, set head to the new node.
Step 4: If not empty, find the last node (tail) by traversing from head.
Step 5: Update pointers to insert the new node at the end of the list and link it to head.
Delete Node:
Step 1: Check if the list is empty (i.e., head is NULL).
Step 2: Traverse the list to find the node with the specified key.
Step 3: If the node is found and is the only node, free it and set head to NULL.
Step 4: If the node is the head but not the only node, update head and adjust pointers
accordingly.
Step 5: If the node is found but not the head, bypass it by updating the previous and next node
pointers and free the node.
Display List:
Step 1: Check if the list is empty (i.e., head is NULL).
Step 2: Initialize a temporary pointer to traverse from head.
Step 3: Print a message indicating the start of the list display.
Step 4: Use a loop to traverse the list and print each node’s data.
Step 5: Continue until you reach back to the head, indicating the end of the circular list.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev; };
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed!\n");
return NULL; }
newNode->data = data;
newNode->next = newNode;
newNode->prev = newNode;
return newNode; }
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* tail = (*head)->prev;
tail->next = newNode;
newNode->prev = tail;
newNode->next = *head;
(*head)->prev = newNode; }
printf("Node with data %d inserted at the end\n", data);
}
void deleteNode(struct Node** head, int key) {
if (*head == NULL) {
printf("List is empty\n");
return; }
struct Node* curr = *head;
struct Node* prev = NULL;
do {
if (curr->data == key) {
if (curr == *head && curr->next == *head) {
free(curr);
*head = NULL;
printf("Node with data %d deleted\n", key);
return;
} else if (curr == *head) {
struct Node* tail = (*head)->prev;
tail->next = curr->next;
curr->next->prev = tail;
*head = curr->next;
free(curr);
printf("Node with data %d deleted\n", key);
return;
} else {
prev->next = curr->next;
curr->next->prev = prev;
free(curr);
printf("Node with data %d deleted\n", key);
return;
}
}
OUTPUT:
prev = curr;
curr = curr->next;
} while (curr != *head);
printf("Node with data %d not found\n", key); }
void display(struct Node* head) {
if (head == NULL) {
printf("The list is empty!\n");
return; }
struct Node* temp = head;
printf("Circular Doubly Linked List: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n"); }
int main() {
struct Node* head = NULL;
int choice, value;
printf("\nMenu:\n");
printf("1. Insert a node at the end\n2. Delete a node\n 3. Display the list\n 4. Exit\n ");
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &value);
insertEnd(&head, value);
break;
case 2:
printf("Enter the value to delete: ");
scanf("%d", &value);
deleteNode(&head, value);
break;
case 3:
display(head);
break;
case 4: Preparation 15
printf("Exiting\n");
Observation 15
exit(0);
default: Implementation and Output 20
printf("Invalid choice, please try again.\n"); Viva 15
} Record 10
}
Total 75
return 0;
}

RESULT:
Thus, a C program to perform the circular doubly linked list was successfully developed and
executed.
EX NO: 06
POLYNOMIAL ADDITION USING LINKED LIST
DATE:24.09.24
+

AIM:
To write a C program to implement polynomial addition using Linked List.

ALGORITHM:
Main Method:
Step 1: Start the program and include required header files.
Step 2: Create a structure lNode with variables coeff, pow as int and next as lNode pointer.
Step 3: Declare struct lNode as node using typedef.
Step 4: Declare the node pointers poly1, poly2, result as NULL and the variables degree, i,
val as int.
Step 5: Get the value of degree from user.
Step 6: Using for loop, initialize i = degree.
Step 7: Check if i >= 0, if true go to step 8 else go to step 10.
Step 8: Get val from user, call the function insertNode (&poly1, val, i).
Step 9: Increment i by 1 and go to step 7.
Step 10: Get the value of degree of poly 2 from user
Step 11: Using for loop, initialize i= degree
step 12: Check if i>=0, if true go to step 13, else go to step 15.
Step 13: Get val from user, call the function insertNode(&poly2, val, i).
Step 14. Increment i by 1, go to step 12.
Step 15: Display both polynomials with disp() function.
Step 16: Store addPolynomials(poly1, poly2) in result.
Step 17: Display the 'result'
Step 18: Stop the program.

node createNode(int coeff, int pow):


Step 1: Allocate space for node using malloc and store the address in NewNode.
Step 2: Assign values of coeff and pow to newNode->coeff, newNod-> pow and NULL to
newNode->next and return NewNode’s address.
Step 3: Stop the function.

void insert Node (node** poly, int coeff, int pow):


Step 1: Create node using createNode function and Store the address in newNode
Step 2: If *poly==NULL, assign newNode to *poly, else go to step 3.
Step 3: Assign *poly to *temp (a node variable)
Step 4: Using while, check temp->next != NULL, if true, assign temp->next to temp go to
step 5. Else go to step 5.
Step 5: Assign newNode to temp->next.
Step 6: Stop the function.

void display (node* poly):


Step 1: Using while check if poly != NULL, if true go to step 2 else goto step 4
Step 2: Display the poly->coeff, poly->pow. Assign poly->next to poly.
Step 3. If poly != NULL, display "+" else go to step 1.
Step 4: Print a newline and stop the function.

node addPolynomials (node* poly 1, node* poly2):


Step1: Declare a node pointer result as NULL.
Step 2: Using while(), check if poly1 != NULL and poly2 != NULL If true go to step 3, else
go to step 9.
Step 3: Check if poly1->pow == poly2->pow, if true go to step 4, else go to step 5
Step 4. Call insertNode(&result, poly1->coeff + poly2->coeff, poly1 pow) and assign poly1-
>next to poly1 and poly2->next to poly2.
Step 5: Check if poly1->pow > poly2->pow, if true go to step 6, else go to step 7
Step 6: Call insertNode( &result, poly1->coeff, poly1->pow) and assign poly1->next to poly1
Step 7: Call insert Node (&result, poly 2->coeff, poly2->pow) and assign poly2->next to
poly2
Step 8: Go to step 2 as it is a while doop
Step 9: Check while poly 1 != NULL, call insert Node(&result, poly1->coeff, poly1->pow)
and assign poly1->next to poly1. This runs in a while loop. Else go to step 10.
Step 10: Check while poly2 != NULL, call insertNode(&result, poly2->coeff, poly2->pow)
and assign poly2->next to poly2. This runs in a while loop. Else go to step 11.
Step 11: Return the address of result.
Step 12: Stop the function.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct lNode {
int coeff;
int pow;
struct lNode* next;
};
typedef struct lNode node;
node* createNode(int coeff, int pow) {
node* newNode = (node*)malloc(sizeof(node));
newNode->coeff = coeff;
newNode->pow = pow;
newNode->next = NULL;
return newNode;
}
void insertNode(node** poly, int coeff, int pow) {
node* newNode = createNode(coeff, pow);
if (*poly == NULL) {
*poly = newNode;
} else {
node* temp = *poly;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void display(node* poly) {
while (poly != NULL) {
printf("%dx^%d", poly->coeff, poly->pow);
poly = poly->next;
if (poly != NULL)
printf(" + ");
}
printf("\n");
}
node* addPolynomials(node* poly1, node* poly2) {
node* result = NULL;
while (poly1 != NULL && poly2 != NULL) {
if (poly1->pow == poly2->pow) {
insertNode(&result, poly1->coeff + poly2->coeff, poly1->pow);
poly1 = poly1->next;
poly2 = poly2->next;
}
else if (poly1->pow > poly2->pow) {
insertNode(&result, poly1->coeff, poly1->pow);
poly1 = poly1->next;
}
else {
insertNode(&result, poly2->coeff, poly2->pow);
poly2 = poly2->next;
}
}
while (poly1 != NULL) {
insertNode(&result, poly1->coeff, poly1->pow);
poly1 = poly1->next;
}
while (poly2 != NULL) {
insertNode(&result, poly2->coeff, poly2->pow);
poly2 = poly2->next;
}
return result;
}

int main() {
node *poly1 = NULL, *poly2 = NULL, *result = NULL;
int degree, val, i;
printf("Polynomial Addition using Linked List");
printf("\nEnter Degree of Polynomial 1:");
scanf("%d", &degree);
printf("Enter the values for respective powers:");
for(int i = degree;i>=0;i--)
{
printf("Value of power %d:",i);
scanf("%d",&val);
insertNode(&poly1,val,i);
}
printf("\nEnter Degree of Polynomial 2:");
scanf("%d", &degree);
printf("Enter the values for respective powers:");
OUTPUT:
for(int i = degree;i>=0;i--)
{ printf("Value of power %d:",i);
scanf("%d",&val);
insertNode(&poly2,val,i);
}
printf("First Polynomial: ");
display(poly1);
printf("Second Polynomial: ");
display(poly2);
result = addPolynomials(poly1, poly2);
printf("Resultant Polynomial after Addition: ");
display(result);
return 0;
}

Preparation 15
Observation 15
Implementation and Output 20
Viva 15
Record 10
Total 75

Result:
Thus, the C program to implement polynomial addition using Linked List is successfully
developed and executed.
EX NO: 07(a)
STACK IMPLEMENTATION USING ARRAY
DATE:01.10.24
+

AIM:
To write a C program to implement Stack using Array.

ALGORITHM:
Main method:
Step 1: Start the program and include the required header files.
Step 2: define a Macro MAX with value 20.
Step 3: Declare an array ‘stack’ with MAX elements and a variable ‘top’ with value -1 as
int.
Step 4: Open the main() function.
Step 5: Declare the variables choice, value, m as int.
Step 6: Display the operations.
Step 7: Generate a while loop with argument 1 so that it executes continuously.
Step 8: Get the choice from user. With choice as argument, open a switch statement.
Step 9: For case 1, get the value from user and call the push(value) function. And go to
step8.
Step 10: For case 2, call pop() function. And go to step 8.
Step 11: For case 3, call display() function. And go to step 8.
Step 12: For case 4, go to step 14.
Step 13: For default case, display invalid choice and go to step 8.
Step 14: Stop the program.

void push(int value):


Step 1: Check if top equal to MAX - 1, if true display Stack overflow, go to step 3 else go
to step 2.
Step 2: Increment top by 1, assign value to stack[top] and display the value pushed.
Step 3: Stop the function.

void pop():
Step 1: Check if top equal to -1, if true display Stack Underflow, go to step 3 else go to step
2.
Step 2: Display stack[top] and decrement top by 1.
Step 3: Stop the function.

void peek():
Step 1: Check if top equal to -1, if true display Stack Underflow, go to step 3 else go to step
2.
Step 2: Display stack[top].
Step 3: Stop the function.

void display():
Step 1: Check if top equal to -1, if true display Stack is empty, go to step else go to step 2.
Step 2: Display using for loop, initialize int i as top. Generate a series of i values until i
greater than 0, Display stack[i], decrement i by 1.
Step 3: Stop the function.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
int stack[MAX];
int top = -1;
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow Cannot Push %d\n", value);
} else {
top++;
stack[top] = value;
printf("%d pushed onto the stack\n", value);
}
}
void pop() {
if (top == -1) {
printf("Stack Underflow No element to Pop\n");
} else {
printf("%d popped from the stack\n", stack[top]);
top--;
}
}
void peek() {
if (top == -1) {
printf("Stack Underflow \n");
} else {
printf("%d is the peek element \n", stack[top]);
}
}
void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Stack elements are: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
int choice, value, m;
printf("Max size of Stack is 20");
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf (“3. Peek\n”);
printf("4. Display\n");
OUTPUT:
printf("5. Exit\n");
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice Please try again\n");
}
}
return 0;
}

RESULT:
Thus, the C program to implement Stack using Array is successfully developed and
executed.
EX NO: 07(b)
STACK IMPLEMENTATION USING LINKED LIST
DATE:01.10.24
+

AIM:
To write a C program to implement Stack using Linked Lists.

ALGORITHM:
Main method:
Step 1: Start the program and include the required header files.
Step 2: Define a structure Node with variable data as int and a Node pointer next.
Step 3: Create a Node pointer ‘top’ and initialize with NULL.
Step 4: Open the main() function.
Step 5: Declare the variables choice, value, m as int.
Step 6: Display the operations.
Step 7: Generate a while loop with argument 1 so that it executes continuously.
Step 8: Get the choice from user. With choice as argument, open a switch statement.
Step 9: For case 1, get the value from user and call the push(value) function. And go to step
8.
Step 10: For case 2, call pop() function. And go to step 8.
Step 11: For case 3, call display() function. And go to step 8.
Step 12: For case 4, go to step 14.
Step 13: For default case, display invalid choice and go to step 8.
Step 14: Stop the program.

void push(int value):


Step 1: Create a Node pointer newNode and allocate size using malloc(sizeof(struct Node))
Step 2: Check if newNode equal to NULL, if true display Stack Overflow, go to step else
go to step 3.
Step 3: Assign value to newNode->data, top to mewNode->next and newNode to top.
Display value pushed.
Step 4: Stop the function.

void pop():
Step 1: Check if top equal to NULL, if true display Stack Underflow, go to step 3 else go to
step 2.
Step 2: Create a Node pointer temp and assign top to it. Display top->data popped, assign
top->next to top.
Step 3: free the temp.
Step 4: Stop the function.

void peek():
Step 1: Check if top equal to NULL, if true display Stack Underflow, go to step 3 else go to
step 2.
Step 2: Display top->data popped, assign top->next to top.
Step 3: Stop the function.

void display():
Step 1: Check if top equal to NULL, if true display Stack is empty, go to step else go to
step 2.
Step 2: Create a Node pointer temp and assign top to it.
Step 3: Display using while loop. Check if temp not equal to NULL, if true display
temp->data, go to step 4. Else go to step 5.
Step 4: Assign temp->next to temp and go to step 3.
Step 5: Stop the function.

Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL;
void push(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Stack Overflow. Memory could not be allocated\n");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d pushed onto the stack\n", value);
}
void pop() {
if (top == NULL) {
printf("Stack Underflow. No element to pop\n");
return;
}
struct Node* temp = top;
printf("%d popped from the stack\n", top->data);
top = top->next;
free(temp);
}
void peek() {
if (top == NULL) {
printf("Stack Underflow.\n");
return;
}
printf("%d is the peek element\n", top->data);
}
void display() {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
struct Node* temp = top;
OUTPUT:
printf("Stack elements are: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
int choice, value;
printf("Stack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("3. Display\n");
printf("4. Exit\n");
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a value : ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);

default:
printf("Invalid choice\n"); Preparation 15
} Observation 15
}
return 0; Implementation and Output 20
} Viva 15
Record 10
Total 75

RESULT:
Thus, the C program to implement Stack using Linked List is successfully developed and
executed.
EX NO: 08(a)
QUEUE IMPLEMENTATION USING ARRAY
DATE:08.10.24
+

AIM:
To write a c program for queue implementation using array.

ALGORITHM:
Step1: Start the program.
Initialize front = -1 and rear = -1 to represent an empty queue.
Define the maximum size of the queue as MAX = 5.
Step 2: Menu Display and Input:
Step3: Display a menu with the following options:
1. Enqueue (Add an element to the queue)
2. Dequeue (Remove an element from the queue)
3. Peek (Show the front element of the queue)
4. Display (Show all elements in the queue)
5. Exit (Quit the program)
Ask the user to enter a choice.
Step4: Enqueue Operation (Add Element)If choice is 1:
1. Check if the queue is full:
If full, display "Queue is full" and return to the menu.
2. If queue is empty (front == -1), set front = 0 (to indicate the first element is being
inserted).
3. Increment rear by 1.
4. Add the new element to queue[rear].
5. Display "Element inserted" and return to the menu.
Step5: Dequeue Operation (Remove Element)If choice is 2:
1. Check if the queue is empty:
If empty, display "Queue is empty" and return to the menu.
2. Display the element at queue[front] (the front element).
3. If front == rear (i.e., only one element left), set front = rear = -1 (reset the queue).
4. Otherwise, increment front by 1 to point to the next element.
5. Return to the menu.
Step6: Peek Operation (Show Front Element)If choice is 3:
1. Check if the queue is empty:
If empty, display "Queue is empty."
2. If not empty, display the front element located at queue[front].
3. Return to the menu.
Step7: Display Operation (Show All Elements) If choice is 4:
1. Check if the queue is empty:
If empty, display "Queue is empty."
2. If not empty, print all elements from queue[front] to queue[rear] using a loop.
3. Return to the menu.
Step8: Exit. If choice is 5, display "Exiting..." and end the program.
Step9: Repeat the Process. Continue showing the menu and performing the selected operation
until the user chooses "Exit."
Step10: Stop the program.

PROGRAM:
#include <stdio.h>
OUTPUT:
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
int isEmpty() {
return front == -1;
}int isFull() {
return rear == MAX - 1;}
void enqueue(int value) {
if (isFull()) {
printf("Queue is full\n");
return;
} if (isEmpty()) {
front = 0;
}rear++;
queue[rear] = value;
printf("Inserted %d\n", value);
}void dequeue() {
if (isEmpty()) {
printf("Queue is empty\n");
return;}
printf("Removed %d\n", queue[front]);
if (front == rear) {
front = rear = -1;
} else {
front++;
}}void peek() {
if (isEmpty()) {
printf("Queue is empty\n");
} else {
printf("Front element: %d\n", queue[front]);
}}void display() {
if (isEmpty()) {
printf("Queue is empty\n");
return;}printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}printf("\n");}
int main() {
int choice, value;
do{
printf("\n1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");}while(choice!=5);
return 0;
}

RESULT:
Thus, the c program queue implementation using array was successfully developed
and executed.
EX NO: 08(b)
QUEUE IMPLEMENTATION USING LINKED LIST
DATE:08.10.24
+

AIM:
To write a c program for queue implementation using linked list.

ALGORITHM:
Step1:Start the program.
1.Define the structure Node to store the queue's elements, which includes:
int data for the value.
struct Node* next for the pointer to the next node.
Initialize two global pointers:
2.front = NULL (points to the front of the queue).
3. rear = NULL (points to the rear of the queue).

Step 2: Menu Display and Input


Display a menu with the following options:
1. Enqueue (Add an element to the queue)
2. Dequeue (Remove an element from the queue)
3. Peek (View the front element of the queue)
4. Display (View all elements in the queue)
5. Exit (Quit the program)
Ask the user to enter a choice.

Step3: Enqueue Operation (Add Element)If choice is 1:


1. Create a new node using malloc and store the input value in it.
2. Set newNode->next = NULL to indicate the new node will be the last one.
3. Check if rear == NULL (the queue is empty):
If true, set front = rear = newNode to add the new node as the first element.
4. If rear != NULL (queue is not empty), set rear->next = newNode and update rear = newNode to
add the element at the end of the queue.
5. Print "Inserted value" and return to the menu.

Step4: Dequeue Operation (Remove Element) If choice is 2:


1. Check if front == NULL (queue is empty):
If true, print "Queue is empty" and return to the menu.
2. Store front in a temporary pointer temp.
3. Print the value of front->data.
4. Set front = front->next to point to the next element.
5. If front == NULL, set rear = NULL to indicate the queue is now empty.
6. Free the memory of the dequeued node (temp).
7. Return to the menu.

Step5: Peek Operation (View Front Element)


If choice is 3:
1. Check if front == NULL (queue is empty):
If true, print "Queue is empty."
2. If not empty, print the value of front->data (the first element).
2. Return to the menu.

Step6: Display Operation (View All Elements) If choice is 4:


OUT[PUT:
1. Check if front == NULL (queue is empty):
If true, print "Queue is empty."
2. If not empty, use a temporary pointer temp = front to traverse the queue:
While temp != NULL, print temp->data and move temp = temp->next.
3. Return to the menu.

Step7: Exit If choice is 5, print "Exiting..." and end the program.


Step 8: Repeat the Process. Continue showing the menu and performing operations until the user
chooses "Exit."
Step9: Stop the program.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
int isEmpty() {
return front == NULL;
}void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}printf("Inserted %d\n", value);
}void dequeue() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}struct Node* temp = front;
printf("Removed %d\n", front->data);
front = front->next;
if (front == NULL) {
rear = NULL;}
if (isEmpty()) {
printf("Queue is empty\n");
return;
}struct Node* temp = front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}printf("\n");
}int main() {
int choice, value;
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);
return 0;}

Preparation 15
Observation 15
Implementation and Output 20
Viva 15
Record 10
Total 75

RESULT:
Thus the c program queue implementation using linked list was successfully developed and
executed.
EX NO: 09(a) CIRCULAR QUEUE IMPLEMENTATION USING
DATE:08.10.24 ARRAY
+

AIM:
To write a c program for circular queue implementation using array.

ALGORITHM:
Step1: Start the program.
Define an array queue[MAX] with a maximum size of MAX = 5.
Initialize two integer variables:
front = -1 (to track the front element of the queue).
rear = -1 (to track the rear element of the queue).
Step 2: Menu Display and Input
Display a menu with the following options:
1. Enqueue (Add an element to the queue)
2. Dequeue (Remove an element from the queue)
3. Peek (View the front element of the queue)
4. Display (View all elements in the queue)
5. Exit (Quit the program)
Ask the user to enter their choice.
Step3: Enqueue Operation (Add Element)If choice is 1:
1. Check if the queue is full using the condition (rear + 1) % MAX == front:
If full, display "Queue is full" and return to the menu.
2. If queue is empty (front == -1), set front = 0 to indicate the first element is being
inserted.
3. Increment rear using the circular increment: rear = (rear + 1) % MAX.
4. Add the new element to queue[rear].
5. Display "Inserted value" and return to the menu.
Step4: Dequeue Operation (Remove Element) If choice is 2:
1. Check if the queue is empty using the condition front == -1:
If empty, display "Queue is empty" and return to the menu.
2. Display the element at queue[front].
3. Check if front == rear (i.e., only one element left in the queue):
If true, set front = rear = -1 to reset the queue (indicating it's empty).
4. If not true, increment front using the circular increment: front = (front + 1) % MAX.
5. Return to the menu.
Step5: Peek Operation (View Front Element) If choice is 3:
1. Check if the queue is empty using the condition front == -1:
If empty, display "Queue is empty."
2. If not empty, display the front element located at queue[front].
3. Return to the menu.
Step6: Display Operation (View All Elements). If choice is 4:
1. Check if the queue is empty using the condition front == -1:
If empty, display "Queue is empty."
2. If not empty, use a loop to print all elements starting from front to rear, handling the
circular nature:
Use the loop condition i != rear to traverse the queue, and increment i using (i + 1) %
MAX.
3. Print the element at queue[rear] after the loop completes.
4. Return to the menu.
Step7: Exit
If choice is 5, print "Exiting..." and end the program.
Step8: Repeat the Process
Continue showing the menu and performing operations until the user chooses "Exit."
Step9: Stop the program
.
PROGRAM:
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
int isEmpty() {
return front == -1;
}int isFull() {
return (rear + 1) % MAX == front;
}void enqueue(int value){
if (isFull()) {
printf("Queue is full\n");
return;
}if (isEmpty()) {
front = 0;
}rear = (rear + 1) % MAX;
queue[rear] = value;
printf("Inserted %d\n", value);
}void dequeue() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}printf("Removed %d\n", queue[front]);
if (front == rear) {
front = rear = -1;}
else{
front = (front + 1) % MAX;
}}void peek() {
if (isEmpty()) {
printf("Queue is empty\n");
} else {
printf("Front element: %d\n", queue[front]);
}}void display() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}printf("Queue elements: ");
for (int i = front; i != rear; i = (i + 1) % MAX) {
printf("%d ", queue[i]);}
printf("%d\n", queue[rear]);
}
int main() {
int choice, value;
OUTPUT:
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);
return 0;
}

RESULT:
Thus. the c program circular implementation queue using array was successfully
developed and executed.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy