0% found this document useful (0 votes)
13 views41 pages

Avani DS Final

Uploaded by

Shreya Painter
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)
13 views41 pages

Avani DS Final

Uploaded by

Shreya Painter
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/ 41

Prajapati Avaniben Kanjibhai 220280116099

Government of Gujarat
L. D. College of Engineering

LABORATORY MANUAL
Information Technology Department

Semester III

3130702 DATA STRUCTURES

Year 2023-24
[Prajapati Avaniben Kanjibhai]
[ 220280116099]

L. D. College of Engineering
Ahmedabad -380015
Prajapati Avaniben Kanjibhai 220280116099

Government of Gujarat
L. D. College of Engineering

This is to certify that Mr./Mrs.

Enrollment No. of Semester III

Information Technology has successfully completed

the prescribed term work/laboratory work of DATA

STRUCTURES 3130702 course within the four walls of

L. D. College of Engineering. This is required as a

partial fulfillment of the said course of Gujarat

Technological University.

Date:

Course In-Charge HOD


Prajapati Avaniben Kanjibhai 220280116099

Government of Gujarat
L. D. College of Engineering

Initial of
Exp. Page
Title Date Course
No. No.
In-charge
1 Introduction to pointers. Call by Value and Call by reference.
2 Introduction to Dynamic Memory Allocation. DMA functions malloc(),
calloc(), free() etc.
3 Implement a program for stack that performs following operations
using array.
(a) PUSH (b) POP (c) PEEP (d) PEEK (e) CHANGE (f) Count (g) DISPLAY
4 Implement a program to convert infix notation to postfix notation
using stack.
5 Write a program to implement QUEUE using arrays that performs
following operations (a) (a) INSERT (b) DELETE (c) DISPLAY
6 Write a program to implement Circular Queue using arrays that
performs following operations. (a) INSERT (b) DELETE (c) DISPLAY
7 Write a menu driven program to implement following operations on
the singly linked list.
⚫ Insert a node
◦ At beginning of the list
◦ At end of the list
◦ After specific node
◦ Before specific node
⚫ Delete a node
◦ From begin of the list
◦ From end of the list
◦ Specific node
⚫ Searching a node
⚫ List traversal
⚫ Reverse the list
⚫ Count total number of nodes
8 Write a program to implement stack using linked list.
9 Write a program to implement queue using linked list.
Prajapati Avaniben Kannjibhai 220280116099

Government of Gujarat
L. D. College of Engineering

Initial of
Exp. Page
Title Date Course
No. No.
In-charge
10 Write a program to implement following operations on the doubly
linked list.
⚫ Insert a node
◦ At beginning of the list
◦ At end of the list
◦ After specific node
◦ Before specific node
⚫ Delete a node
◦ From begin of the list
◦ From end of the list
◦ Specific node
⚫ Searching a node
⚫ List traversal
⚫ Count total number of nodes
11 Write a program to implement following operations on the circular
linked list.
⚫ Insert a node
◦ At beginning of the list
◦ At end of the list
◦ After specific node
◦ Before specific node
⚫ Delete a node
◦ From begin of the list
◦ From end of the list
◦ Specific node
⚫ Searching a node
⚫ List traversal
⚫ Count total number of nodes
12 Write a program to implement Bubble Sort and Selection Sort.
13 Write a program to implement Insertion Sort and Quick Sort.
14 Write a program to implement Merge Sort.
15 Write a program to implement Linear Search and Binary Search.
16 Write a program which create binary search tree.
17 Implement recursive and non-recursive tree traversing methods
inorder, preorder and post-order traversal.
Prajapati Avaniben Kanjibhai 220280116099

Practical 5

Aim: Write a program to implement QUEUE using arrays that performs following operations (a) (a) INSERT (b)
DELETE (c) DISPLAY.

Ans :
#include <stdio.h>
#define MAX_SIZE 100

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

void insert(int data) {


if (rear == MAX_SIZE - 1) {
printf("Queue is full. Cannot insert.\n");
} else {
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = data;
printf("Inserted %d into the queue.\n", data);
}
}

void delete() {
if (front == -1 || front > rear) {
printf("Queue is empty. Cannot delete.\n");
} else {
printf("Deleted %d from the queue.\n", queue[front]);
front++;
}
}

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

int main() {
int choice, data;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
Prajapati Avaniben Kanjibhai 220280116099

printf("Enter your choice: ");


scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to insert: ");
scanf("%d", &data);
insert(data);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
}

Outputs :
Prajapati Avaniben Kanjibhai 220280116099

Practical-6

Aim: Write a program to implement Circular Queue using arrays that performs following operations. (a) INSERT
(b) DELETE (c) DISPLAY

Ans:
#include <stdio.h>
#define MAX_SIZE 100

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

void insert(int data) {


if ((front == 0 && rear == MAX_SIZE - 1) || (rear == front - 1)) {
printf("Queue is full. Cannot insert.\n");
} else {
if (front == -1) {
front = rear = 0;
} else if (rear == MAX_SIZE - 1) {
rear = 0;
} else {
rear++;
}
queue[rear] = data;
printf("Inserted %d into the circular queue.\n", data);
}
}

void delete() {
if (front == -1) {
printf("Queue is empty. Cannot delete.\n");
} else {
printf("Deleted %d from the circular queue.\n", queue[front]);
if (front == rear) {
front = rear = -1;
} else if (front == MAX_SIZE - 1) {
front = 0;
} else {
front++;
}
}
}

void display() {
Prajapati Avaniben Kanjibhai 220280116099

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

int main() {
int choice, data;
printf("\nCircular Queue Operations:\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");

while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to insert: ");
scanf("%d", &data);
insert(data);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
return 0;
default:
Prajapati Avaniben Kanjibhai 220280116099

printf("Invalid choice. Try again.\n");


}
}
}

Output:
Prajapati Avaniben Kanjibhai 220280116099

Practical-7

Aim: Write a menu driven program to implement following operations on the singly linked list.
⚫ Insert a node
◦ At beginning of the list
◦ At end of the list
◦ After specific node
◦ Before specific node
⚫ Delete a node
◦ From begin of the list
◦ From end of the list
◦ Specific node
⚫ Searching a node
⚫ List traversal
⚫ Reverse the list
Count total number of nodes.

Ans :

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

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

struct Node* head = NULL;

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


void insertAtBeginning(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = head;
head = newNode;
printf("Node with data %d inserted at the beginning.\n", data);
}

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


void insertAtEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
Prajapati Avaniben Kanjibhai 220280116099

}
printf("Node with data %d inserted at the end.\n", data);
}

// Function to insert a node after a specific node


void insertAfterNode(int data, int afterData) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
struct Node* temp = head;
while (temp != NULL && temp->data != afterData) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with data %d not found.\n", afterData);
} else {
newNode->next = temp->next;
temp->next = newNode;
printf("Node with data %d inserted after the node with data %d.\n", data, afterData);
}
}

// Function to insert a node before a specific node


void insertBeforeNode(int data, int beforeData) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (head == NULL) {
printf("List is empty. Cannot insert before a node.\n");
return;
}
if (head->data == beforeData) {
newNode->next = head;
head = newNode;
printf("Node with data %d inserted before the first node.\n", data);
return;
}
struct Node* temp = head;
while (temp->next != NULL && temp->next->data != beforeData) {
temp = temp->next;
}
if (temp->next == NULL) {
printf("Node with data %d not found.\n", beforeData);
} else {
newNode->next = temp->next;
temp->next = newNode;
printf("Node with data %d inserted before the node with data %d.\n", data, beforeData);
}
}

// Function to delete a node from the beginning of the list


void deleteFromBeginning() {
if (head == NULL) {
Prajapati Avaniben Kanjibhai 220280116099

printf("List is empty. Cannot delete from the beginning.\n");


} else {
struct Node* temp = head;

head = head->next;
free(temp);
printf("Node deleted from the beginning.\n");
}
}

// Function to delete a node from the end of the list


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty. Cannot delete from the end.\n");
} else if (head->next == NULL) {
free(head);
head = NULL;
printf("Node deleted from the end.\n");
} else {
struct Node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
printf("Node deleted from the end.\n");
}
}

// Function to delete a specific node


void deleteNode(int dataToDelete) {
if (head == NULL) {
printf("List is empty. Cannot delete.\n");
} else {
if (head->data == dataToDelete) {
struct Node* temp = head;
head = head->next;
free(temp);
printf("Node with data %d deleted.\n", dataToDelete);
return;
}
struct Node* temp = head;
struct Node* prev = NULL;
while (temp != NULL && temp->data != dataToDelete) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Node with data %d not found.\n", dataToDelete);
} else {
prev->next = temp->next;
free(temp);
Prajapati Avaniben Kanjibhai 220280116099

printf("Node with data %d deleted.\n", dataToDelete);


}
}
}

// Function to search for a node with specific data


void searchNode(int searchData) {
struct Node* temp = head;
int position = 1;
while (temp != NULL) {
if (temp->data == searchData) {
printf("Node with data %d found at position %d.\n", searchData, position);
return;
}
temp = temp->next;
position++;
}
printf("Node with data %d not found.\n", searchData);
}

// Function to traverse and display the linked list


void displayList() {
struct Node* temp = head;
printf("List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Function to reverse the linked list


void reverseList() {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
printf("List reversed.\n");
}

// Function to count the total number of nodes in the linked list


int countNodes() {
int count = 0;
struct Node* temp = head;
while (temp != NULL) {
count++;
Prajapati Avaniben Kanjibhai 220280116099

temp = temp->next;
}
return count;
}

int main() {
int choice, data, afterData, beforeData, searchData;
printf("\nSingly Linked List Operations:\n");
printf("1. Insert at Beginning\n2. Insert at End\n3. Insert After Specific Node\n");
printf("4. Insert Before Specific Node\n5. Delete from Beginning\n6. Delete from End\n");
printf("7. Delete Specific Node\n8. Search a Node\n9. List Traversal\n10. Reverse the List\n");
printf("11. Count Total Number of Nodes\n12. Exit\n");

while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 2:
printf("Enter the element to insert at the end: ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 3:
printf("Enter the element to insert: ");
scanf("%d", &data);
printf("Enter the data of the node after which to insert: ");
scanf("%d", &afterData);
insertAfterNode(data, afterData);
break;
case 4:
printf("Enter the element to insert: ");
scanf("%d", &data);
printf("Enter the data of the node before which to insert: ");
scanf("%d", &beforeData);
insertBeforeNode(data, beforeData);
break;
case 5:
deleteFromBeginning();
break;
case 6:
deleteFromEnd();
break;
case 7:
printf("Enter the data of the node to delete: ");
scanf("%d", &data);
deleteNode(data);
Prajapati Avaniben Kanjibhai 220280116099

break;
case 8:
printf("Enter the data to search for: ");
scanf("%d", &searchData);
searchNode(searchData);
break;
case 9:
displayList();
break;
case 10:
reverseList();
break;
case 11:
printf("Total number of nodes: %d\n", countNodes());
break;
case 12:
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}

OUTPUT of Insert in the LinkedList:


Prajapati Avaniben Kanjibhai 220280116099
Prajapati Avaniben Kanjibhai 220280116099

OUTPUTS FOR Delete Nodes :

OUTPUTS FOR SEARCHING NODE IN LINKEDLIST :

OUTPUTS FOR LIST AND REVERSE LINKEDLIST


:

OUTPUTS FOR COUNT NODES IN THE LINKEDLIST :


Prajapati Avaniben Kanjibhai 220280116099

PRACTICAL-8

AIM : Write a program to implement stack using linked list.

ANS:

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

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

struct Node* top = NULL;

// Function to push an element onto the stack


void push(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = top;
top = newNode;
printf("Pushed %d onto the stack.\n", data);
}

// Function to pop an element from the stack


void pop() {
if (top == NULL) {
printf("Stack is empty. Cannot pop.\n");
} else {
struct Node* temp = top;
top = top->next;
printf("Popped %d from the stack.\n", temp->data);
free(temp);
}
}

// Function to display the elements in the stack


void display() {
if (top == NULL) {
printf("Stack is empty.\n");
} else {
struct Node* temp = top;
printf("Stack elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}
Prajapati Avaniben Kanjibhai 220280116099

int main() {
int choice, data;

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

switch (choice) {
case 1:
printf("Enter the element to push onto the stack: ");
scanf("%d", &data);
push(data);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}

OUTPUT :
Prajapati Avaniben Kanjibhai 220280116099

Practical-9
AIM: Write a program to implement queue using linked list.
ANS :
#include <stdio.h>
#include <stdlib.h>

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

struct Node* front = NULL;


struct Node* rear = NULL;

// Function to enqueue an element into the queue


void enqueue(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("Enqueued %d into the queue.\n", data);
}

// Function to dequeue an element from the queue


void dequeue() {
if (front == NULL) {
printf("Queue is empty. Cannot dequeue.\n");
} else {
struct Node* temp = front;
front = front->next;
if (front == NULL) {
rear = NULL;
}
printf("Dequeued %d from the queue.\n", temp->data);
free(temp);
}
}

// Function to display the elements in the queue


Prajapati Avaniben Kanjibhai 220280116099

void display() {
if (front == NULL) {
printf("Queue is empty.\n");
} else {
struct Node* temp = front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}

int main() {
int choice, data;

while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to enqueue into the queue: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
Prajapati Avaniben Kanjibhai 220280116099

OUTPUT :
Prajapati Avaniben Kanjibhai 220280116099

Practical-10
AIM: Write a program to implement following operations on the doubly linked list.
⚫ Insert a node
◦ At beginning of the list
◦ At end of the list
◦ After specific node
◦ Before specific node
⚫ Delete a node
◦ From begin of the list
◦ From end of the list
◦ Specific node
⚫ Searching a node
⚫ List traversal
⚫ Count total number of nodes

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

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

struct Node* head = NULL;

// Function to create a new node


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

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


void insertAtBeginning(int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
printf("Node with data %d inserted at the beginning.\n", data);
}

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


void insertAtEnd(int data) {
Prajapati Avaniben Kanjibhai 220280116099

struct Node* newNode = createNode(data);


if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;

newNode->prev = temp;

}
printf("Node with data %d inserted at the end.\n", data);
}

// Function to insert a node after a specific node


void insertAfterNode(int data, int afterData) {
struct Node* newNode = createNode(data);
if (head == NULL) {
printf("List is empty. Cannot insert after a specific node.\n");
return;
}
struct Node* temp = head;
while (temp != NULL && temp->data != afterData) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with data %d not found.\n", afterData);
} else {
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
printf("Node with data %d inserted after the node with data %d.\n", data, afterData);
}
}

// Function to insert a node before a specific node


void insertBeforeNode(int data, int beforeData) {
struct Node* newNode = createNode(data);
if (head == NULL) {
printf("List is empty. Cannot insert before a specific node.\n");
return;
}
if (head->data == beforeData) {
newNode->next = head;
head->prev = newNode;
head = newNode;
printf("Node with data %d inserted before the first node.\n", data);
Prajapati Avaniben Kanjibhai 220280116099

return;
}
struct Node* temp = head;
while (temp != NULL && temp->data != beforeData) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with data %d not found.\n", beforeData);
} else {

newNode->next = temp;
newNode->prev = temp->prev;
temp->prev->next = newNode;
temp->prev = newNode;
printf("Node with data %d inserted before the node with data %d.\n", data, beforeData);
}
}

// Function to delete a node from the beginning of the list


void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty. Cannot delete from the beginning.\n");
} else {
struct Node* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
printf("Node deleted from the beginning.\n");
}
}

// Function to delete a node from the end of the list


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty. Cannot delete from the end.\n");
} else if (head->next == NULL) {
free(head);
head = NULL;
printf("Node deleted from the end.\n");
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL;
free(temp);
printf("Node deleted from the end.\n");
}
}
Prajapati Avaniben Kanjibhai 220280116099

// Function to delete a specific node


void deleteNode(int dataToDelete) {
if (head == NULL) {
printf("List is empty. Cannot delete.\n");
} else {
struct Node* temp = head;
while (temp != NULL && temp->data != dataToDelete) {
temp = temp->next;
}

if (temp == NULL) {
printf("Node with data %d not found.\n", dataToDelete);
} else {
if (temp->prev != NULL) {
temp->prev->next = temp->next;
} else {
head = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
printf("Node with data %d deleted.\n", dataToDelete);
}
}
}

// Function to search for a node with specific data


void searchNode(int searchData) {
struct Node* temp = head;
int position = 1;
while (temp != NULL) {
if (temp->data == searchData) {
printf("Node with data %d found at position %d.\n", searchData, position);
return;
}
temp = temp->next;
position++;
}
printf("Node with data %d not found.\n", searchData);
}

// Function to traverse and display the linked list


void displayList() {
struct Node* temp = head;
printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Prajapati Avaniben Kanjibhai 220280116099

// Function to count the total number of nodes in the linked list


int countNodes() {
int count = 0;
struct Node* temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}

return count;
}

int main() {
int choice, data, afterData, beforeData, searchData;

while (1) {
printf("\nDoubly Linked List Operations:\n");
printf("1. Insert at Beginning\n2. Insert at End\n3. Insert After Specific Node\n");
printf("4. Insert Before Specific Node\n5. Delete from Beginning\n6. Delete from End\n");
printf("7. Delete Specific Node\n8. Search a Node\n9. List Traversal\n10. Count Total Number of
Nodes\n11. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 2:
printf("Enter the element to insert at the end: ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 3:
printf("Enter the element to insert: ");
scanf("%d", &data);
printf("Enter the data of the node after which to insert: ");
scanf("%d", &afterData);
insertAfterNode(data, afterData);
break;
case 4:
printf("Enter the element to insert: ");
scanf("%d", &data);
printf("Enter the data of the node before which to insert: ");
scanf("%d", &beforeData);
insertBeforeNode(data, beforeData);
break;
case 5:
deleteFromBeginning();
Prajapati Avaniben Kanjibhai 220280116099

break;
case 6:
deleteFromEnd();
break;
case 7:
printf("Enter the data of the node to delete: ");
scanf("%d", &data);
deleteNode(data);
break;

case 8:
printf("Enter the data to search for: ");
scanf("%d", &searchData);
searchNode(searchData);
break;
case 9:
displayList();
break;
case 10:
printf("Total number of nodes: %d\n", countNodes());
break;
case 11:
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}

OUTPUTS FOR Delete Nodes :


Prajapati Avaniben Kanjibhai 220280116099

OUTPUTS FOR SEARCHING NODE IN LINKEDLIST :

OUTPUTS FOR LIST AND REVERSE LINKEDLIST


:

OUTPUTS FOR COUNT NODES IN THE LINKEDLIST :

OUTPUT of Insert in the LinkedList :


Prajapati Avaniben Kanjibhai 220280116099
Prajapati Avaniben Kanjibhai 220280116099

Practical-11
AIM: Write a program to implement following operations on the circular linked list.
⚫ Insert a node
◦ At beginning of the list
◦ At end of the list
◦ After specific node
◦ Before specific node
⚫ Delete a node
◦ From begin of the list
◦ From end of the list
◦ Specific node
⚫ Searching a node
⚫ List traversal
⚫ Count total number of nodes

ANS:

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

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

struct Node* head = NULL;

// 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 insert a node at the beginning of the circular list


void insertAtBeginning(int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
head = newNode;
head->next = head; // Point to itself to make it circular
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
newNode->next = head;
temp->next = newNode;
Prajapati Avaniben Kanjibhai 220280116099

head = newNode;
}
printf("Node with data %d inserted at the beginning.\n", data);
}

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


void insertAtEnd(int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
head = newNode;
head->next = head; // Point to itself to make it circular
} else {

struct Node* temp = head;


while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
printf("Node with data %d inserted at the end.\n", data);
}

// Function to insert a node after a specific node


void insertAfterNode(int data, int afterData) {
if (head == NULL) {
printf("List is empty. Cannot insert after a specific node.\n");
return;
}
struct Node* newNode = createNode(data);
struct Node* temp = head;
while (temp->data != afterData && temp->next != head) {
temp = temp->next;
}
if (temp->data != afterData) {
printf("Node with data %d not found.\n", afterData);
} else {
newNode->next = temp->next;
temp->next = newNode;
printf("Node with data %d inserted after the node with data %d.\n", data, afterData);
}
}

// Function to insert a node before a specific node


void insertBeforeNode(int data, int beforeData) {
if (head == NULL) {
printf("List is empty. Cannot insert before a specific node.\n");
return;
}
struct Node* newNode = createNode(data);
if (head->data == beforeData) {
Prajapati Avaniben Kanjibhai 220280116099

struct Node* temp = head;


while (temp->next != head) {
temp = temp->next;
}
newNode->next = head;
temp->next = newNode;
head = newNode;
printf("Node with data %d inserted before the first node.\n", data);
return;
}
struct Node* prev = NULL;
struct Node* temp = head;
while (temp->data != beforeData && temp->next != head) {

prev = temp;
temp = temp->next;
}
if (temp->data != beforeData) {
printf("Node with data %d not found.\n", beforeData);
} else {
newNode->next = temp;
prev->next = newNode;
printf("Node with data %d inserted before the node with data %d.\n", data, beforeData);
}
}

// Function to delete a node from the beginning of the circular list


void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty. Cannot delete from the beginning.\n");
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
if (temp == head) {
head = NULL;
} else {
temp->next = head->next;
head = head->next;
}
free(temp);
printf("Node deleted from the beginning.\n");
}
}

// Function to delete a node from the end of the circular list


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty. Cannot delete from the end.\n");
Prajapati Avaniben Kanjibhai 220280116099

} else {
struct Node* temp = head;
struct Node* prev = NULL;
while (temp->next != head) {
prev = temp;
temp = temp->next;
}
if (temp == head) {
head = NULL;
} else {
prev->next = head;
}
free(temp);
printf("Node deleted from the end.\n");
}
}

// Function to delete a specific node


void deleteNode(int dataToDelete) {
if (head == NULL) {
printf("List is empty. Cannot delete.\n");
return;
}
struct Node* temp = head;
struct Node* prev = NULL;
while (temp->data != dataToDelete && temp->next != head) {
prev = temp;
temp = temp->next;
}
if (temp->data != dataToDelete) {
printf("Node with data %d not found.\n", dataToDelete);
} else {
if (temp == head) {
struct Node* lastNode = head;
while (lastNode->next != head) {
lastNode = lastNode->next;
}
if (lastNode == head) {
head = NULL;
} else {
lastNode->next = head->next;
head = head->next;
}
} else {
prev->next = temp->next;
}
free(temp);
printf("Node with data %d deleted.\n", dataToDelete);
}
}
Prajapati Avaniben Kanjibhai 220280116099

// Function to search for a node with specific data


void searchNode(int searchData) {
if (head == NULL) {
printf("List is empty. Cannot search.\n");
return;
}
struct Node* temp = head;
int position = 1;
do {
if (temp->data == searchData) {
printf("Node with data %d found at position %d.\n", searchData, position);
return;
}
temp = temp->next;

position++;
} while (temp != head);
printf("Node with data %d not found.\n", searchData);
}

// Function to traverse and display the circular linked list


void displayList() {
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(" (circular)\n");
}

// Function to count the total number of nodes in the circular list


int countNodes() {
int count = 0;
if (head == NULL) {
return count;
}
struct Node* temp = head;
do {
count++;
temp = temp->next;
} while (temp != head);
return count;
}

int main() {
int choice, data, afterData, beforeData, searchData;
Prajapati Avaniben Kanjibhai 220280116099

while (1) {
printf("\nCircular Linked List Operations:\n");
printf("1. Insert at Beginning\n2. Insert at End\n3. Insert After Specific Node\n");
printf("4. Insert Before Specific Node\n5. Delete from Beginning\n6. Delete from End\n");
printf("7. Delete Specific Node\n8. Search a Node\n9. List Traversal\n10. Count Total Number of
Nodes\n11. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(data);
break;

case 2:
printf("Enter the element to insert at the end: ");
scanf("%d", &data);
insertAtEnd(data);
break;

case 3:
printf("Enter the element to insert: ");
scanf("%d", &data);
printf("Enter the data of the node after which to insert: ");
scanf("%d", &afterData);
insertAfterNode(data, afterData);
break;
case 4:
printf("Enter the element to insert: ");
scanf("%d", &data);
printf("Enter the data of the node before which to insert: ");
scanf("%d", &beforeData);
insertBeforeNode(data, beforeData);
break;
case 5:
deleteFromBeginning();
break;
case 6:
deleteFromEnd();
break;
case 7:
printf("Enter the data of the node to delete: ");
scanf("%d", &data);
deleteNode(data);
break;
case 8:
printf("Enter the data to search for: ");
scanf("%d", &searchData);
searchNode(searchData);
Prajapati Avaniben Kanjibhai 220280116099

break;
case 9:
displayList();
break;
case 10:
printf("Total number of nodes: %d\n", countNodes());
break;
case 11:
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}

OUTPUTS FOR Delete Nodes :

OUTPUTS FOR SEARCHING NODE IN LINKEDLIST :


Prajapati Avaniben Kanjibhai 220280116099

OUTPUTS FOR LIST AND REVERSE LINKEDLIST :

OUTPUTS FOR COUNT NODES IN THE LINKEDLIST :

OUTPUT of Insert in the LinkedList :


Prajapati Avaniben Kanjibhai 220280116099
Prajapati Avaniben Kanjibhai 220280116099
Prajapati Avaniben Kanjibhai 220280116099

You might also like