0% found this document useful (0 votes)
24 views9 pages

DS SE III QP 24-25 Sol

The document provides C code for implementing a Singly Circular Linked List with functions to insert a node at the start and delete a node by value. It also includes a program to implement a Queue using a linked list, featuring functions to enqueue, dequeue, and display the queue contents. The main function demonstrates the queue operations with example data.

Uploaded by

demblamahek02
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)
24 views9 pages

DS SE III QP 24-25 Sol

The document provides C code for implementing a Singly Circular Linked List with functions to insert a node at the start and delete a node by value. It also includes a program to implement a Queue using a linked list, featuring functions to enqueue, dequeue, and display the queue contents. The main function demonstrates the queue operations with example data.

Uploaded by

demblamahek02
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/ 9

Q3 a. Write a Function in C to implement the following for Singly Circular Linked List.

(i) Insert a node in the start the list


(ii) Delete a node given its value.
// Function to insert a node at the start of the circular singly linked list
void insertAtStart(struct Node** head, int data) {
// Allocate memory for the new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
// If the list is empty
if (*head == NULL) {
newNode->next = newNode; // Point to itself, as it's the only node
*head = newNode;
} else {
// Traverse to the last node to maintain the circular property
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
// Insert the new node at the beginning
newNode->next = *head;
temp->next = newNode; // Update the last node's next pointer
*head = newNode; // Update head to the new node
}
}
// Function to delete a node given its value
void deleteNode(struct Node** head, int key) {
// If the list is empty, return
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct Node *current = *head, *prev = NULL;
// Case 1: If the node to be deleted is the head node
if (current->data == key) {
// If there is only one node in the list
if (current->next == *head) {
free(current); // Free the only node
*head = NULL; // The list becomes empty
return;
}
// If there are more than one nodes, find the last node
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = current->next; // Update last node's next pointer
*head = current->next; // Update head to the next node
free(current); // Free the old head node
return;
}
// Case 2: If the node to be deleted is not the head node
while (current->next != *head && current->data != key) {
prev = current;
current = current->next;
}
// If the node with the given key is found
if (current->data == key) {
prev->next = current->next; // Update the previous node's next pointer
free(current); // Free the current node
} else {
printf("Node with value %d not found.\n", key);
}
}

Q3 b. Write a program to implement Queue using Linked List


#include <stdio.h>
#include <stdlib.h>
// Define a node structure
struct Node {
int data;
struct Node* next;
};
// Define a queue structure
struct Queue {
struct Node* front;
struct Node* rear;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to create a new queue
struct Queue* createQueue() {
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
// Function to enqueue an element to the queue
void enqueue(struct Queue* q, int data) {
struct Node* newNode = createNode(data);
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
q->rear->next = newNode;
q->rear = newNode;
}
// Function to dequeue an element from the queue
int dequeue(struct Queue* q) {
if (q->front == NULL) {
printf("Queue is empty\n");
return -1; // Return -1 if queue is empty
}
struct Node* temp = q->front;
int data = temp->data;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
return data;
}
// Function to display the contents of the queue
void displayQueue(struct Queue* q) {
struct Node* temp = q->front;
if (temp == NULL) {
printf("Queue is empty\n");
return;
}
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Main function to test the queue implementation
int main() {
struct Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
displayQueue(q);
printf("Dequeued: %d\n", dequeue(q));
printf("Dequeued: %d\n", dequeue(q));
displayQueue(q);
enqueue(q, 40);
enqueue(q, 50);
displayQueue(q);
// Freeing up memory (optional but good practice)
while (q->front != NULL) {
dequeue(q);
}
free(q);

return 0;
}

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