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

DS Lab Programs

The document contains multiple C programs demonstrating various linked list operations, including insertion, deletion, reversal, duplicate removal, and polynomial addition. Each section provides code snippets for implementing these functionalities, along with user interaction for input and output. The document also includes a program for a doubly linked list with insertion and deletion operations.

Uploaded by

Kandula Anusha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DS Lab Programs

The document contains multiple C programs demonstrating various linked list operations, including insertion, deletion, reversal, duplicate removal, and polynomial addition. Each section provides code snippets for implementing these functionalities, along with user interaction for input and output. The document also includes a program for a doubly linked list with insertion and deletion operations.

Uploaded by

Kandula Anusha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Exercise 2

1. A) Write a C program to insert an element in a single linked list


#include <stdio.h>
#include <stdlib.h>
// Structure for a single linked list node
struct Node {
int data;
struct Node* next;
};
// Function to insert a node at the beginning
void insertAtBeginning(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}
// Function to insert a node at the end
void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// Function to insert a node at a specific position
void insertAtPosition(struct Node** head, int value, int position) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if (position == 1) {
newNode->next = *head;
*head = newNode;
return;
}
struct Node* temp = *head;
for (int i = 1; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
// Function to print the linked list
void display(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main function to take user input and perform insertion operations
int main() {
struct Node* head = NULL;
int choice, value, position;
while (1) {
printf("\nLinked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Display List\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at the beginning: ");
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
printf("Enter value to insert at the end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
printf("Enter value to insert: ");
scanf("%d", &value);
printf("Enter position: ");
scanf("%d", &position);
insertAtPosition(&head, value, position);
break;
case 4:
printf("Current Linked List: ");
display(head);
break;
case 5:
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
return 0;
}

OUTPUT:
1. B) Write a C program to delete an element in a single linked list
#include <stdio.h>
#include <stdlib.h>
// Structure for a single linked list node
struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

// Function to delete a node from the beginning


void deleteAtBeginning(struct Node** head) {
if (*head == NULL) {
printf("List is empty! Nothing to delete.\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
printf("Deleted element from the beginning.\n");
}

// Function to delete a node from the end


void deleteAtEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty! Nothing to delete.\n");
return;
}
struct Node* temp = *head;
struct Node* prev = NULL;
// Traverse to the last node
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
printf("Deleted element from the end.\n");
}

// Function to delete a node by value


void deleteByValue(struct Node** head, int value) {
if (*head == NULL) {
printf("List is empty! Nothing to delete.\n");
return;
}
struct Node* temp = *head;
struct Node* prev = NULL;
// Search for the node to be deleted
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
// Unlink and free memory
prev->next = temp->next;
free(temp);
printf("Deleted element %d from the list.\n", value);
}

// Function to display the linked list


void display(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty!\n");
return;
}
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function to take user input and perform deletion operations


int main() {
struct Node* head = NULL;
int choice, value;
while (1) {
printf("\nLinked List Operations:\n");
printf("1. Insert at End\n");
printf("2. Delete from Beginning\n");
printf("3. Delete from End\n");
printf("4. Delete by Value\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at the end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 2:
deleteAtBeginning(&head);
break;
case 3:
deleteAtEnd(&head);
break;
case 4:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteByValue(&head, value);
break;
case 5:
printf("Current Linked List: ");
display(head);
break;
case 6:
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}

OUTPUT:
2. C program to reverse a linked list both iteratively and recursively

#include <stdio.h>
#include <stdlib.h>

// Structure for a linked list node


struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

if (*head == NULL) {
*head = newNode;
return;
}

struct Node* temp = *head;


while (temp->next != NULL) {
temp = temp->next;
}

temp->next = newNode;
}

// Function to reverse the linked list


void reverseList(struct Node** head) {
struct Node *prev = NULL, *current = *head, *next = NULL;

while (current != NULL) {


next = current->next; // Store next node
current->next = prev; // Reverse current node's pointer
prev = current; // Move pointers forward
current = next;
}
*head = prev; // Update head to the new first node
}

// Function to display the linked list


void display(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty!\n");
return;
}
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function
int main() {
struct Node* head = NULL;
int n, value;

// Taking input for linked list


printf("Enter the number of elements: ");
scanf("%d", &n);

printf("Enter the elements:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &value);
insertAtEnd(&head, value);
}

// Display original linked list


printf("\nOriginal Linked List:\n");
display(head);

// Reverse the linked list


reverseList(&head);

// Display reversed linked list


printf("\nReversed Linked List:\n");
display(head);

return 0;
}

OUTPUT:

Exercise 3
1. Create a program to detect and remove duplicates form a linked list

#include <stdio.h>
#include <stdlib.h>

// Structure for a linked list node


struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

if (*head == NULL) {
*head = newNode;
return;
}

struct Node* temp = *head;


while (temp->next != NULL) {
temp = temp->next;
}

temp->next = newNode;
}

// Function to remove duplicates from an unsorted linked list


void removeDuplicates(struct Node* head) {
struct Node *current, *prev, *temp;
current = head;

while (current != NULL && current->next != NULL) {


prev = current;
temp = current->next;

while (temp != NULL) {


if (current->data == temp->data) {
prev->next = temp->next;
free(temp);
temp = prev->next;
} else {
prev = temp;
temp = temp->next;
}
}
current = current->next;
}
}

// Function to display the linked list


void display(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty!\n");
return;
}
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function
int main() {
struct Node* head = NULL;
int n, value;

// Taking input for linked list


printf("Enter the number of elements: ");
scanf("%d", &n);

printf("Enter the elements:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &value);
insertAtEnd(&head, value);
}
// Display original linked list
printf("\nOriginal Linked List:\n");
display(head);

// Remove duplicates
removeDuplicates(head);

// Display linked list after removing duplicates


printf("\nLinked List After Removing Duplicates:\n");
display(head);

return 0;
}

OUTPUT:

2. Implement a linked list to represent polynomial addition

#include <stdio.h>
#include <stdlib.h>

// Structure for a polynomial term


struct Node
{
int coeff;
int exp;
struct Node* next;
};

// Function to create a new term


struct Node* createNode(int coeff, int exp)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
}

// Function to insert a term into the polynomial


void insertTerm(struct Node** poly, int coeff, int exp)
{
struct Node* newNode = createNode(coeff, exp);
if (*poly == NULL)
{
*poly = newNode;
return;
}

struct Node* temp = *poly;


while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}

// Function to display a polynomial


void displayPolynomial(struct Node* poly)
{
if (poly == NULL)
{
printf("0\n");
return;
}

while (poly != NULL)


{
printf("%dx^%d", poly->coeff, poly->exp);
if (poly->next != NULL)
printf(" + ");
poly = poly->next;
}
printf("\n");
}

// Function to add two polynomials


struct Node* addPolynomials(struct Node* poly1, struct Node* poly2)
{
struct Node* result = NULL;
struct Node* temp;

while (poly1 != NULL && poly2 != NULL)


{
if (poly1->exp > poly2->exp)
{
insertTerm(&result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
}
else if (poly1->exp < poly2->exp)
{
insertTerm(&result, poly2->coeff, poly2->exp);
poly2 = poly2->next;
}
else
{
int sumCoeff = poly1->coeff + poly2->coeff;
if (sumCoeff != 0)
{
insertTerm(&result, sumCoeff, poly1->exp);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}
while (poly1 != NULL)
{
insertTerm(&result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
}
while (poly2 != NULL)
{
insertTerm(&result, poly2->coeff, poly2->exp);
poly2 = poly2->next;
}
return result;
}

// Main function
int main()
{
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* result = NULL;
int n1, n2, coeff, exp;

// Input first polynomial


printf("Enter the number of terms in first polynomial: ");
scanf("%d", &n1);
printf("Enter the terms (coefficient and exponent):\n");

for (int i = 0; i < n1; i++)


{
scanf("%d %d", &coeff, &exp);
insertTerm(&poly1, coeff, exp);
}

// Input second polynomial


printf("Enter the number of terms in second polynomial: ");
scanf("%d", &n2);
printf("Enter the terms (coefficient and exponent):\n");
for (int i = 0; i < n2; i++)
{
scanf("%d %d", &coeff, &exp);
insertTerm(&poly2, coeff, exp);
}

// Display input polynomials


printf("\nFirst Polynomial: ");
displayPolynomial(poly1);
printf("Second Polynomial: ");
displayPolynomial(poly2);

// Add polynomials
result = addPolynomials(poly1, poly2);

// Display the sum


printf("\nSum of Polynomials: ");
displayPolynomial(result);

return 0;
}

OUTPUT:

Exercise 4
1. C Program for Doubly Linked List with Insertion and Deletion Operations
#include <stdio.h>
#include <stdlib.h>
// Structure for a doubly linked list node
struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}

// Function to insert a node at the end of the doubly linked list


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = createNode(value);

if (*head == NULL) {
*head = newNode;
return;
}

struct Node* temp = *head;


while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}

// Function to insert a node at the beginning of the doubly linked list


void insertAtBegin(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
return;
}
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}

// Function to delete a node from the beginning of the doubly linked list
void deleteAtBegin(struct Node** head) {
if (*head == NULL) {
printf("List is empty!\n");
return;
}

struct Node* temp = *head;


*head = (*head)->next;

if (*head != NULL) {
(*head)->prev = NULL;
}

free(temp);
}

// Function to delete a node from the end of the doubly linked list
void deleteAtEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty!\n");
return;
}

struct Node* temp = *head;

// If there is only one node


if (temp->next == NULL) {
*head = NULL;
free(temp);
return;
}

// Traverse to the last node


while (temp->next != NULL) {
temp = temp->next;
}

temp->prev->next = NULL;
free(temp);
}

// Function to display the doubly linked list


void display(struct Node* head) {
if (head == NULL) {
printf("List is empty!\n");
return;
}

struct Node* temp = head;


while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function
int main() {
struct Node* head = NULL;
int choice, value;

while (1) {
printf("\nDoubly Linked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete from Beginning\n");
printf("4. Delete from End\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBegin(&head, value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
deleteAtBegin(&head);
printf("Node deleted from beginning.\n");
break;
case 4:
deleteAtEnd(&head);
printf("Node deleted from end.\n");
break;
case 5:
printf("Doubly Linked List: ");
display(head);
break;
case 6:
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}

return 0;
}

OUTPUT:
2. C Program for Circular Linked List with Insertion and Deletion Operations

#include <stdio.h>
#include <stdlib.h>

// Structure for a circular linked list node


struct Node {
int data;
struct Node* next;
};

// Function to insert a node at the end of the circular linked list


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

if (*head == NULL) {
newNode->next = newNode; // Point to itself in circular list
*head = newNode;
return;
}

struct Node* temp = *head;


while (temp->next != *head) {
temp = temp->next;
}

temp->next = newNode;
newNode->next = *head;
}

// Function to insert a node at the beginning of the circular linked list


void insertAtBegin(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;

if (*head == NULL) {
newNode->next = newNode; // Point to itself
*head = newNode;
return;
}

struct Node* temp = *head;


while (temp->next != *head) {
temp = temp->next;
}

newNode->next = *head;
temp->next = newNode;
*head = newNode;
}

// Function to delete a node from the beginning of the circular linked list
void deleteAtBegin(struct Node** head) {
if (*head == NULL) {
printf("List is empty!\n");
return;
}

struct Node* temp = *head;


struct Node* last = *head;

if ((*head)->next == *head) { // Single node case


free(*head);
*head = NULL;
return;
}

while (last->next != *head) {


last = last->next;
}

*head = (*head)->next;
last->next = *head;
free(temp);
}

// Function to delete a node from the end of the circular linked list
void deleteAtEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty!\n");
return;
}

struct Node* temp = *head;


struct Node* prev = NULL;

if ((*head)->next == *head) { // Single node case


free(*head);
*head = NULL;
return;
}

while (temp->next != *head) {


prev = temp;
temp = temp->next;
}

prev->next = *head;
free(temp);
}

// Function to display the circular linked list


void display(struct Node* head) {
if (head == NULL) {
printf("List is empty!\n");
return;
}

struct Node* temp = head;


do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);

printf("(Head)\n");
}

// Main function
int main() {
struct Node* head = NULL;
int choice, value;
while (1) {
printf("\nCircular Linked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete from Beginning\n");
printf("4. Delete from End\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBegin(&head, value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
deleteAtBegin(&head);
printf("Node deleted from beginning.\n");
break;
case 4:
deleteAtEnd(&head);
printf("Node deleted from end.\n");
break;
case 5:
printf("Circular Linked List: ");
display(head);
break;
case 6:
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}

return 0;
}

OUTPUT:
\
Exercise 5
1. a) Implement a stack using Array
#include <stdio.h>
#define MAX 5

int stack[MAX], top = -1;


void push(int value)
{
if (top == MAX - 1)
{
printf("Stack Overflow!\n");
return;
}
top=top+1;
stack[top] = value;
}
int pop()
{
if (top == -1)
{
printf("Stack Underflow!\n");
return -1;
}
top=top-1;
return stack[top];
}
void display()
{
if (top == -1)
{
printf("Stack is empty\n");
return;
}
printf("Stack: ");
for (int i = 0; i <= top; i++)
{
printf("%d-> ", stack[i]);
}
printf("\n");
}
int main()
{
int choice, value;

while (1)
{
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
printf("Popped: %d\n", pop());
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice!\n");
}
}
}

OUTPUT:
1. b) Implement a stack using Linked list
#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)
{
printf("Stack Overflow!\n");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
}

int pop()
{
if (!top)
{
printf("Stack Underflow!\n");
return -1;
}
struct Node* temp = top;
int value = temp->data;
top = top->next;
free(temp);
return value;
}

void display()
{
struct Node* temp = top;
printf("Stack: ");
while (temp)
{
printf("%d ->", temp->data);
temp = temp->next;
} printf("\n");
}
int main()
{
int choice, value;
while (1)
{
printf("\n1. Push 2. Pop 3. Display 4. Exit\nEnter choice: ");
scanf("%d", &choice);
if (choice == 1)
{
printf("Enter value: ");
scanf("%d", &value);
push(value);
}
else if (choice == 2)
{
printf("Popped: %d\n", pop());
}
else if (choice == 3)
{
display();
}
else if (choice == 4)
{
break;
}
else
{
printf("Invalid choice!\n");
}
}
return 0;
}
OUTPUT:

Evaluation of a Postfix Expression using Stack


A postfix expression (Reverse Polish Notation) is evaluated using a stack because operators
appear after their operands, eliminating the need for parentheses.

Steps to Evaluate a Postfix Expression

1. Read the expression from left to right.


2. If the character is an operand (number), push it onto the stack.
3. If the character is an operator, pop two operands from the stack:
o Apply the operator on the operands.
o Push the result back onto the stack.
4. Continue this process until the entire expression is scanned.
5. The final result will be the only element left in the stack.

Example

Evaluate: 53+82-*

Step-by-step Execution:

Step Symbol Stack (Top at Right) Explanation


1 5 5 Push operand 5
2 3 5 3 Push operand 3
3 + 8 Pop 5, 3, compute 5+3=8, push result
4 8 8 8 Push operand 8
5 2 8 8 2 Push operand 2
6 - 8 6 Pop 8, 2, compute 8-2=6, push result
7 * 48 Pop 8, 6, compute 8*6=48, push result

Final result = 48.

2. C Program to Evaluation of a Postfix Expression using Stack

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100

int stack[MAX];
int top = -1;
void push(int value)
{
top=top+1;
stack[top] = value;
}
int pop()
{
return stack[top--];
}
int evaluatePostfix(char *exp)
{
for (int i = 0; exp[i] != '\0'; i++)
{
if (isdigit(exp[i]))
{
push(exp[i]-'0'); // Convert character to integer
}
else
{
int val2 = pop();
int val1 = pop();
switch (exp[i])
{
case '+': push(val1 + val2); break;
case '-': push(val1 - val2); break;
case '*': push(val1 * val2); break;
case '/': push(val1 / val2); break;
}
}
}
return pop(); // Final result
}
int main()
{
char exp[MAX];
printf("Enter a postfix expression: ");
scanf("%s", exp);

printf("Result: %d\n", evaluatePostfix(exp));


return 0;
}

OUTPUT:

Exercise 6

1. Implementation of queue using arrays


#include <stdio.h>
#define SIZE 100

int queue[SIZE];
int front = -1, rear = -1;

// Function to enqueue an element


void enqueue(int value) {
if (rear == SIZE - 1) {
printf("Queue is full (Overflow)\n");
} else {
if (front == -1) front = 0; // First element
rear++;
queue[rear] = value;
printf("%d enqueued to the queue\n", value);
}
}

// Function to dequeue an element


void dequeue() {
if (front == -1 || front > rear) {
printf("Queue is empty (Underflow)\n");
} else {
printf("Dequeued element: %d\n", queue[front]);
front++;
if (front > rear) {
// Reset the queue if it becomes empty
front = rear = -1;
}
}
}

// Function to display the queue


void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

// Main function
int main() {
int choice, value;

while (1) {
printf("\nQueue Operations Menu:\n");
printf("1. Enqueue\n 2. Dequeue\n 3. Display\n 4. 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:
display();
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
}

OUTPUT:
2. Implementation of Queue using Linked List
#include <stdio.h>
#include <stdlib.h>

// Define the node structure


struct Node {
int data;
struct Node* next;
};

struct Node* front = NULL;


struct Node* rear = NULL;

// Function to enqueue an element


void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = NULL;

if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("%d enqueued to the queue\n", value);
}

// Function to dequeue an element


void dequeue() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}

struct Node* temp = front;


front = front->next;
if (front == NULL) {
rear = NULL;
}

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


free(temp);
}

// Function to display the queue


void display() {
if (front == NULL) {
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");
}

// Main function
int main() {
int choice, value;

while (1) {
printf("\nQueue Operations Menu:\n");
printf("1. Enqueue\n 2. Dequeue\n 3. Display\n 4. 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:
display();
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}

return 0;
}

OUTPUT:
3. C Program for Circular Queue
#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

// Enqueue function
void enqueue(int value) {
if ((rear + 1) % SIZE == front) {
printf("Queue is full\n");
} else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
queue[rear] = value;
printf("%d enqueued to the queue\n", value);
}
}

// Dequeue function
void dequeue() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Dequeued element: %d\n", queue[front]);
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % SIZE;
}
}
}

// Display function
void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear) break;
i = (i + 1) % SIZE;
}
printf("\n");
}
}

// Main function
int main() {
int choice, value;

while (1) {
printf("\nCircular Queue Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. 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:
display();
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
OUTPUT:
4. C Program for Job Scheduling using Queue

#include <stdio.h>
#include <stdlib.h>
#define SIZE 100

typedef struct {
int jobId;
int burstTime;
} Job;

Job queue[SIZE];
int front = -1, rear = -1;

void enqueue(int id, int time) {


if (rear == SIZE - 1) {
printf("Queue is full. Cannot schedule more jobs.\n");
return;
}
if (front == -1)
front = 0;
rear++;
queue[rear].jobId = id;
queue[rear].burstTime = time;
printf("Job %d scheduled with burst time %d.\n", id, time);
}

void dequeue() {
if (front == -1 || front > rear) {
printf("No jobs to execute.\n");
return;
}
printf("Executing Job %d with burst time %d.\n", queue[front].jobId, queue[front].burstTime);
front++;
}

void displayQueue() {
if (front == -1 || front > rear) {
printf("Queue is empty.\n");
return;
}
printf("Jobs in queue:\n");
for (int i = front; i <= rear; i++) {
printf("Job ID: %d, Burst Time: %d\n", queue[i].jobId, queue[i].burstTime);
}
}

int main() {
int choice, id, time;
while (1) {
printf("\n1. Schedule Job\n2. Execute Job\n3. Show Jobs\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter Job ID and Burst Time: ");
scanf("%d %d", &id, &time);
enqueue(id, time);
break;
case 2:
dequeue();
break;
case 3:
displayQueue();
break;
case 4:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}

OUTPUT:
5. C Program for Double Ended Queue
#include <stdio.h>
#include <stdlib.h>
#define MAX 10

int deque[MAX];
int front = -1, rear = -1;

void insertFront(int x) {
if ((front == 0 && rear == MAX - 1) || (front == rear + 1)) {
printf("Deque is full\n");
return;
}
if (front == -1) {
front = rear = 0;
} else if (front == 0) {
front = MAX - 1;
} else {
front--;
}
deque[front] = x;
printf("Inserted %d at front\n", x);
}

void insertRear(int x) {
if ((front == 0 && rear == MAX - 1) || (front == rear + 1)) {
printf("Deque is full\n");
return;
}
if (front == -1) {
front = rear = 0;
} else if (rear == MAX - 1) {
rear = 0;
} else {
rear++;
}
deque[rear] = x;
printf("Inserted %d at rear\n", x);
}

void deleteFront() {
if (front == -1) {
printf("Deque is empty\n");
return;
}
printf("Deleted from front: %d\n", deque[front]);
if (front == rear) {
front = rear = -1;
} else if (front == MAX - 1) {
front = 0;
} else {
front++;
}
}

void deleteRear() {
if (front == -1) {
printf("Deque is empty\n");
return;
}
printf("Deleted from rear: %d\n", deque[rear]);
if (front == rear) {
front = rear = -1;
} else if (rear == 0) {
rear = MAX - 1;
} else {
rear--;
}
}

void display() {
int i = front;
if (front == -1) {
printf("Deque is empty\n");
return;
}
printf("Deque elements: ");
while (1) {
printf("%d ", deque[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}

int main() {
int choice, value;
printf("Double Ended Queue (Deque) - User Input Menu\n");

while (1)
{
printf("\n--- MENU ---\n");
printf("1. Insert at Front\n");
printf("2. Insert at Rear\n");
printf("3. Delete from Front\n");
printf("4. Delete from Rear\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(value);
break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(value);
break;
case 3:
deleteFront();
break;
case 4:
deleteRear();
break;
case 5:
display();
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}

OUTPUT:

You might also like