0% found this document useful (0 votes)
2 views64 pages

Lab Manual (1) DS

The document is a lab manual for the Data Structures course at Adani University, detailing practical exercises for students. It includes various programming tasks related to pointers, dynamic memory allocation, stack, queue, linked lists, and tree structures. Each practical outlines specific aims, code examples, and expected outputs for student implementation.

Uploaded by

Krish Patel
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)
2 views64 pages

Lab Manual (1) DS

The document is a lab manual for the Data Structures course at Adani University, detailing practical exercises for students. It includes various programming tasks related to pointers, dynamic memory allocation, stack, queue, linked lists, and tree structures. Each practical outlines specific aims, code examples, and expected outputs for student implementation.

Uploaded by

Krish Patel
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/ 64

Student’s Roll No.

: CS107

Student’s Name: Krish Patel

FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY


(FEST), ADANI UNIVERSITY
Subject Name & Code: Data Structures, ECSCI24101 (2nd Semester)

LAB MANUAL
Sr. Practical Name Page Grade Faculty
No. No. Signature
1 Introduction to pointers. Call by Value and Call by Reference. 3

2 Introduction to Dynamic Memory Allocation. DMA functions


malloc(), calloc(), free() etc. 7

3 Implement a program for stack that performs following operations


using array.
(a) PUSH, (b) POP, (c) 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) 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 the following
operations on the singly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Insert a node such that linked list is in ascending
order.(according to info. Field)
(d) Delete the first node of the linked list.
(e) Delete a node before specified position.
(f) Delete a node after specified position.
8 Write a program to implement stack using linked list.

9 Write a program to implement queue using linked list.

1
Student’s Roll No.: CS107

Student’s Name: Krish Patel

10 Write a program to implement the following operations on the


doubly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Delete the last node of the linked list.
(d) Delete a node before specified position.
11 Write a program to implement the following operations on the
circular linked list.
(a) Insert a node at the end of the linked list.
(b) Insert a node before specified position.
(c) Delete the first node of the linked list.
(d) Delete a node after specified position.
12 Write a program which create binary search tree.

13 Implement recursive and non-recursive tree traversing methods


inorder, preorder and postorder traversal.
14 Implement BFS and DFS for a graph.

15 Implement hash table with a hash function H(x) maps the value x
at the index x%10 in an Array.

2
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical-1
Aim: Introduction to pointers. Call by Value and Call by Reference.

Pointers
Definition:

A pointer is a variable that stores the memory address of another variable. Instead of holding a
direct value, it holds the address where the value is stored in memory. There are 2 important
operators that we will use in pointers concepts i.e. Dereferencing operator (*) used to declare
pointer variable and access the value stored in the address.
Address operator (&) used to returns the address of a variable or to access the address of a
variable to a pointer.
General form:
Datatype *pointer_name=&variable_name;

Example of Pointers:
Code:

3
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output:

Call by Value

Definition:
In call by value method of parameter passing, the values of actual parameters are copied to the
Function’s formal parameters.
• There are two copies of parameters stored in different memory locations.

• One is the original copy and the other is the function copy.
• Any changes made inside functions are not reflected in the actual parameters of the caller.
Example of Call by Value:

4
Student’s Roll No.: CS107

Student’s Name: Krish Patel


Code:

Output:

Call by reference:

Definition:
In call by reference method of parameter passing, the address of the actual parameters is passed
5
Student’s Roll No.: CS107

Student’s Name: Krish Patel


to
the function as the formal parameters. In C, we use pointers to achieve call-by-reference.
• Both the actual and formal parameters refer to the same locations.

• Any changes made inside the function are actually reflected in the actual parameters of the
caller.
Example of Call by Reference:

Code:

Output:

6
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical-2

Aim: Introduction to Dynamic Memory Allocation. DMA functions malloc(),calloc(), free()


etc.

Dynamic Memory Allocation

Sometimes the size of the array we declared may be insufficient. To solve this issue, we can
allocate memory manually during run-time. This is known as dynamic memory allocation in C
programming. To allocate memory dynamically, library functions are malloc(), calloc(), realloc()
and free() are used. These functions are defined in the header file.
MALLOC()

The malloc() function allocates a single block of requested memory. It doesn't initialize memory at
execution time, so it has garbage value initially. It returns NULL if memory is not sufficient. The
syntax of malloc() function is given below: ptr=(cast-type*)malloc(byte-size)

Program :-

7
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :-

CALLOC()

The calloc() function allocates multiple blocks of requested memory. It initially initializes all bytes
to zero . It returns NULL if memory is not sufficient. The syntax of calloc() function is given
below:

ptr=(cast-type*)calloc(number, byte-size)
Program :-

8
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :-

9
Student’s Roll No.: CS107

Student’s Name: Krish Patel

REALLOC()

If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc()
function. In short, it changes the memory size. The syntax of realloc() function is:

ptr=realloc(ptr, new-size)

Program :-

10
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :-

FREE()

The memory occupied by malloc() or calloc() functions must be released by calling free() function.
Otherwise, it will consume memory until the program exits. The syntax of free() function is :
free(ptr);

11
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical No. :- 3
Insert at the end:
If you are inserting an element at the end, there’s no need for shifting elements. You can directly
add the element at the next available position and increase the size.
Program :

12
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :-

Insert at any position:


Insert operation in an array at any position can be performed by shifting elements to the right,
which are on the right side of the required position.
Program :

13
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :-

Delete Operation:
In the delete operation, the element to be deleted is searched using the linear search, and then the
delete operation is performed followed by shifting the elements.
Program :

14
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :-

15
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical No. :- 4
Aim : Implement a program to convert infix notation to postfix notation using stack.
Code :
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX 100


char stack[MAX];
int top = -1;

// Function to push element onto stack


void push(char ch)
{
if (top == MAX - 1)
{
printf("Stack Overflow!\n");
}
else
{
stack[++top] = ch;

16
Student’s Roll No.: CS107

Student’s Name: Krish Patel


}
}

// Function to pop element from stack


char pop()
{
if (top == -1)
{
return '\0'; // Return NULL character if stack is empty
}
return stack[top--];
}

// Function to return precedence of operators


int precedence(char ch)
{
if (ch == '^')
return 3;
if (ch == '*' || ch == '/')
return 2;
if (ch == '+' || ch == '-')
return 1;

17
Student’s Roll No.: CS107

Student’s Name: Krish Patel


return 0;
}

// Function to convert infix expression to postfix


void infixToPostfix(char infix[])
{
char postfix[MAX];
int j = 0;
int i;

for (i = 0; i < strlen(infix); i++)


{
char ch = infix[i];
if (isalnum(ch))
{
postfix[j++] = ch;
}
else if (ch == '(')
{
push(ch);
}
else if (ch == ')')

18
Student’s Roll No.: CS107

Student’s Name: Krish Patel


{
while (top != -1 && stack[top] != '(')
{
postfix[j++] = pop();
}
pop(); // Pop '(' from stack
}
else
{
while (top != -1 && precedence(stack[top]) >= precedence(ch))
{
postfix[j++] = pop();
}
push(ch);
}
}

// Pop remaining operators from stack


while (top != -1)
{
postfix[j++] = pop();
}

19
Student’s Roll No.: CS107

Student’s Name: Krish Patel


postfix[j] = '\0'; // Null terminate the string

printf("Postfix Expression: %s\n", postfix);


}

// Main function to test the conversion


int main()
{
char infix[MAX];

printf("Enter infix expression: ");


scanf("%s", infix);

infixToPostfix(infix);

return 0;
}
Output :

20
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical No. :- 5
Aim : Write a program to implement Queue using arrays that performs following
operations
(a)INSERT (b)DELETE (c)DISPLAY
#include <stdio.h>
#define MAX 5
int queue[MAX], F = -1, R = -1;
void enqueue(int);
void dequeue();
int main()
{
int choice, value;
printf("Choose Operation: \n1) enqueue\n2) dequeue\n3) display\n4) Exit
Program");
while (1)
{
printf("\nEnter Choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
21
Student’s Roll No.: CS107

Student’s Name: Krish Patel


enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("\nEnter a valid argument (1 / 2 / 3).");
break;
}
}
return 0;
}
void enqueue(int value)
{
if (F == -1 && R == -1) {
F = 0;
queue[++R] = value;
}

22
Student’s Roll No.: CS107

Student’s Name: Krish Patel

else if (R + 1 > MAX - 1) {


printf("\nQueue is Full , Overflow");
}
else {
queue[++R] = value;
}
}
void dequeue(){
if (F == -1 && R == -1) {
printf("\nNo Elements can be deleted. Queue Underflow.");
}
else if (F == R) {
printf("\nElement Deleted: %d", queue[F]);
F = -1;
R = -1;
}
else {
printf("\nElement Deleted: %d", queue[F]);
F++;
}
}
void display()

23
Student’s Roll No.: CS107

Student’s Name: Krish Patel


{
printf("\n");
int i;
for (i = F; i <= R; i++){
printf("%d ", queue[i]);
}
return;}
Output :

24
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical No. :- 6
Aim : Write a program to implement Circular Queue using arrays that performs
following operations
(a)INSERT (b)DELETE (c)DISPLAY
Code :
#include <stdio.h>
#define MAX 5

int queue[MAX] = {NULL}, F = -1, R = -1;

void enqueue(int);
void dequeue();
void display();

int main()
{
int choice, value;
printf("Choose Operation: \n1) enqueue\n2) dequeue\n3) display\n4) Exit
Program");
while (1)
{

25
Student’s Roll No.: CS107

Student’s Name: Krish Patel


printf("\nEnter Choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("\nEnter a valid argument (1 / 2 / 3).");
break;
}

26
Student’s Roll No.: CS107

Student’s Name: Krish Patel


}
return 0;
}

void enqueue(int value)


{
if (F == -1 && R == -1)
{
F = 0;
queue[++R] = value;
}
else if ((R + 1) % MAX == F)
{
printf("Queue Overflow. Cannot insert the element.");
}
else
{
R = (R + 1) % MAX;
queue[R] = value;
}
return;

27
Student’s Roll No.: CS107

Student’s Name: Krish Patel


}

void dequeue()
{
if (F == -1 && R == -1)
{
printf("Queue Underflow , no elements to remove.");
}
else if (F == R && queue[(F + 1) % MAX] == NULL)
{
printf("Element Deleted: %d", queue[F]);
queue[F] = NULL;
F = -1;
R = -1;
}
else
{
printf("Element Deleted: %d", queue[F]);
queue[F] = NULL;
F = (F + 1) % MAX;
}

28
Student’s Roll No.: CS107

Student’s Name: Krish Patel


return;
}

void display()
{
int i = F;
if (F == -1)
{
printf("Queue Underflow.");
}

while (i != R)
{
printf("%d ", queue[i]);
i = (i + 1) % MAX;
}
printf("%d", queue[i]);
}

29
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :

30
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical No. :- 7
Aim : Write a menu driven program to implement the following operations on the
singly linked list.
(a)Insert a node at front of the linked list.
(b)Insert a node at end of the linked list.
(c)Insert a node such that linked list is in ascending order.
(d)Delete first node of linked list.
(e)Delete a node before specified position.
(f)Delete a node after specified position.

Code :
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;

// (a) Insert at front


void insertAtFront(int value)
{
31
Student’s Roll No.: CS107

Student’s Name: Krish Patel


struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
printf("%d inserted at front.\n", value);
}

// (b) Insert at end


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

32
Student’s Roll No.: CS107

Student’s Name: Krish Patel


}
printf("%d inserted at end.\n", value);
}

// (c) Insert in ascending order


void insertInAscendingOrder(int value)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL || head->data >= value)
{
newNode->next = head;
head = newNode;
}
else
{
struct Node *temp = head;
while (temp->next != NULL && temp->next->data < value)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
}

33
Student’s Roll No.: CS107

Student’s Name: Krish Patel


printf("%d inserted in ascending order.\n", value);
}

// (d) Delete first node


void deleteFirst()
{
if (head == NULL)
{
printf("List is empty.\n");
return;
}
struct Node *temp = head;
head = head->next;
printf("Deleted node with value %d from front.\n", temp->data);
free(temp);
}

// (e) Delete node before specified position


void deleteBeforePosition(int position)
{
if (head == NULL || position <= 1)
{
printf("Invalid position!\n");

34
Student’s Roll No.: CS107

Student’s Name: Krish Patel


return;
}

if (position == 2)
{
deleteFirst();
return;
}

struct Node *prev = NULL;


struct Node *curr = head;

for (int i = 1; i < position - 2 && curr->next != NULL; i++)


{
prev = curr;
curr = curr->next;
}

if (curr == NULL || curr->next == NULL)


{
printf("No node exists before position %d.\n", position);
return;
}

35
Student’s Roll No.: CS107

Student’s Name: Krish Patel

struct Node *nodeToDelete = curr->next;


curr->next = nodeToDelete->next;
printf("Deleted node with value %d before position %d.\n", nodeToDelete->data,
position);
free(nodeToDelete);
}

// (f) Delete node after specified position


void deleteAfterPosition(int position)
{
if (head == NULL)
{
printf("List is empty.\n");
return;
}

struct Node *temp = head;

for (int i = 1; i < position && temp != NULL; i++)


{
temp = temp->next;
}

36
Student’s Roll No.: CS107

Student’s Name: Krish Patel


if (temp == NULL || temp->next == NULL)
{
printf("No node exists after position %d.\n", position);
return;
}

struct Node *nodeToDelete = temp->next;


temp->next = nodeToDelete->next;
printf("Deleted node with value %d after position %d.\n", nodeToDelete->data,
position);
free(nodeToDelete);
}

// Display the list


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

printf("Linked List: ");


37
Student’s Roll No.: CS107

Student’s Name: Krish Patel


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

// Main menu
int main()
{
int choice, value, pos;

while (1)
{
printf("\n***** MENU *****\n");
printf("1. Insert at Front\n");
printf("2. Insert at End\n");
printf("3. Insert in Ascending Order\n");
printf("4. Delete First Node\n");
printf("5. Delete Before Position\n");
printf("6. Delete After Position\n");
printf("7. Display\n");

38
Student’s Roll No.: CS107

Student’s Name: Krish Patel


printf("8. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Enter value: ");
scanf("%d", &value);
insertAtFront(value);
break;
case 2:
printf("Enter value: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
printf("Enter value: ");
scanf("%d", &value);
insertInAscendingOrder(value);
break;
case 4:
deleteFirst();

39
Student’s Roll No.: CS107

Student’s Name: Krish Patel


break;
case 5:
printf("Enter position: ");
scanf("%d", &pos);
deleteBeforePosition(pos);
break;
case 6:
printf("Enter position: ");
scanf("%d", &pos);
deleteAfterPosition(pos);
break;
case 7:
display();
break;
case 8:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}

40
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :

41
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical No. :- 8
Aim : Write a program to implement stack using linked list.
Code :
#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));
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d inserted at front.\n", value);
42
Student’s Roll No.: CS107

Student’s Name: Krish Patel


}

void pop()
{
if (top == NULL)
{
printf("List is empty.\n");
return;
}
struct Node *temp = top;
top = top->next;
printf("Deleted node with value %d from front.\n", temp->data);
free(temp);
}

void display()
{
struct Node *temp = top;
if (top == NULL)
{
printf("List is empty.\n");

43
Student’s Roll No.: CS107

Student’s Name: Krish Patel


return;
}
printf("Linked List (Top to Bottom): ");
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
int choice, value;
while (1)
{
printf("\n*** MENU ***\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n"); // Changed from 8 to 4

44
Student’s Roll No.: CS107

Student’s Name: Krish Patel


printf("Enter choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Enter value: ");
scanf("%d", &value);
Push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice! Try again.\n");

45
Student’s Roll No.: CS107

Student’s Name: Krish Patel


}
}
}
Output :

46
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical No. :- 9
Aim : Write a program to implement queue using linked list.

Code :
#include <stdio.h>
#include <stdlib.h>
// Define the node structure
struct node
{
int data;
struct node *link;
};
struct node *front = NULL;
struct node *rear = NULL;
// Enqueue operation in the queue
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->link = NULL;
if (rear == NULL)
{
47
Student’s Roll No.: CS107

Student’s Name: Krish Patel


front = rear = Newnode;
}
else
{
rear->link = Newnode;
rear = Newnode;
}
printf("%d inserted at the rear!!\n", value);
}
// Dequeue operation in the queue
void dequeue()
{
if (front == NULL)
{
printf("Queue underflow\n");
return;
}
struct node *temp = front;
front = front->link;
free(temp);
printf("Front element is deleted\n");
if (front == NULL)
{
rear = NULL;
printf("Queue is underflow now\n");
}
}

48
Student’s Roll No.: CS107

Student’s Name: Krish Patel


// Display operation in the queue
void display()
{
struct node *temp = front;
if (temp == NULL)
{
printf("Queue is underflow\n");
return;
}
printf("Queue elements are: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->link;
}
printf("\n");
}

int main()
{
int choice, value;
while (1)
{
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
49
Student’s Roll No.: CS107

Student’s Name: Krish Patel


case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}

50
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :

51
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical No. :- 10

Aim : Write a program to perform the following operations on the doubly linked
list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Delete the last node of the linked list.
(d) Delete a node before specified position.

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

// Doubly Linked List Node


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

struct Node* head = NULL;

// Insert at the beginning


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

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

head = newNode;
52
Student’s Roll No.: CS107

Student’s Name: Krish Patel


printf("%d inserted at head\n", value);
}

// Insert at the end


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

if (head == NULL) {
newNode->prev = NULL;
head = newNode;
printf("%d inserted at end\n", value);
return;
}

struct Node* temp = head;


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

temp->next = newNode;
newNode->prev = temp;
printf("%d inserted at end\n", value);
}

// Delete from the end


void deleteAtEnd() {
if (head == NULL) {
printf("List is empty\n");
return;
}

if (head->next == NULL) {
printf("Deleted %d from end\n", head->data);
53
Student’s Roll No.: CS107

Student’s Name: Krish Patel


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

struct Node* temp = head;


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

printf("Deleted %d from end\n", temp->data);


temp->prev->next = NULL;
free(temp);
}
void deleteBeforePosition(int pos) {
if (pos <= 2) {
printf("No node exists before position %d to delete.\n", pos);
return;
}

struct Node* temp = head;


int i = 1;
while (i < pos - 2 && temp != NULL) {
temp = temp->next;
i++;
}
if (temp == NULL || temp->next == NULL) {
printf("Invalid position. Cannot delete.\n");
return;
}
struct Node* nodeToDelete = temp->next;

temp->next = nodeToDelete->next;
if (nodeToDelete->next != NULL) {
nodeToDelete->next->prev = temp;
54
Student’s Roll No.: CS107

Student’s Name: Krish Patel


}
printf("Deleted node with value %d before position %d.\n", nodeToDelete-
>data, pos);
free(nodeToDelete);
}
void display() {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty\n");
return;
}
printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
int choice, value;

while (1) {
printf("1. Insert at Head\n");
printf("2. Insert at End\n");
printf("3. Delete at End\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at head: ");
scanf("%d", &value);
insertAtHead(value);
55
Student’s Roll No.: CS107

Student’s Name: Krish Patel


break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
deleteAtEnd();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}

return 0;
}

56
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Output :

57
Student’s Roll No.: CS107

Student’s Name: Krish Patel

Practical No. :- 11
Aim : Write a program to implement the following operations on the circular
linked list.

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

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

struct Node* head = NULL;

void insertAtEnd(int value) {


struct Node* p = (struct Node*)malloc(sizeof(struct Node));
p->data = value;
p->next = NULL;

if (head == NULL) {
head = p;
p->next = head;
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = p;
p->next = head;
}
printf("%d inserted at end.\n", value);
}

58
Student’s Roll No.: CS107

Student’s Name: Krish Patel


void deleteBeforePosition(int pos) {
if (head == NULL || pos <= 1) {
printf("Invalid position or not enough nodes to delete before position %d.\n",
pos);
return;
}

int count = 1;
struct Node* temp = head;
struct Node* prev = NULL;
struct Node* del = NULL;

if (pos == 2) { // Deleting head node


deleteFirstNode();
return;
}

while (count < pos - 2 && temp->next != head) {


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

if (temp->next == head || temp->next->next == head) {


printf("Position out of bounds.\n");
return;
}

del = temp->next;
temp->next = del->next;
printf("Deleted %d before position %d.\n", del->data, pos);
free(del);
}

void deleteAfterPosition(int pos) {


if (head == NULL) {
59
Student’s Roll No.: CS107

Student’s Name: Krish Patel


printf("List is empty. Nothing to delete.\n");
return;
}

struct Node* temp = head;


int count = 1;

while (count < pos && temp->next != head) {


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

if (temp->next == head || count != pos) {


printf("Position out of bounds or no node to delete after position %d.\n",
pos);
return;
}

struct Node* del = temp->next;


temp->next = del->next;

if (del == head) {
head = del->next;
}

printf("Deleted %d after position %d.\n", del->data, pos);


free(del);
}

void deleteFirstNode() {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}

60
Student’s Roll No.: CS107

Student’s Name: Krish Patel


if (head->next == head) {
printf("Deleted %d from beginning.\n", head->data);
free(head);
head = NULL;
return;
}

struct Node* temp = head;


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

struct Node* del = head;


temp->next = head->next;
head = head->next;
printf("Deleted %d from beginning.\n", del->data);
free(del);
}

void display() {
if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


printf("Circular Linked List: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}

int main() {
61
Student’s Roll No.: CS107

Student’s Name: Krish Patel


int choice, value, pos;

while (1) {
printf("\n1. Insert at End\n");
printf("2. Delete Before Position\n");
printf("3. Delete After Position\n");
printf("4. Delete First Node\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 end: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 2:
printf("Enter position to delete before: ");
scanf("%d", &pos);
deleteBeforePosition(pos);
break;
case 3:
printf("Enter position to delete after: ");
scanf("%d", &pos);
deleteAfterPosition(pos);
break;
case 4:
deleteFirstNode();
break;
case 5:
display();
break;
case 6:
62
Student’s Roll No.: CS107

Student’s Name: Krish Patel


exit(0);
default:
printf("Invalid choice\n");
}
}

return 0;
}

Output :

63
Student’s Roll No.: CS107

Student’s Name: Krish Patel

64

You might also like