Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Data Structure (3130702) Enrollment No:220220131147

Experiment No: 3

AIM : Singly linked list

3.1 Write a menu driven program to implement 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 a first node of the linked list.
(e) Delete a node before specified position.
(f) Delete a node after specified position.
3.2 Write a program to implement stack using linked list
3.3 Write a program to implement queue using linked list.

Date:22-09-2023

Competency and Practical Skills: Logic building and programming

Relevant CO: CO2, CO5

Objectives: (a) To understand the concepts of singly linked list


(b) To analyze different algorithms on singly link list
(c) To implement various operations on singly link list

Equipment/Instruments: Computer System with turbo C/C++

Safety and necessary Precautions:

✓ Operate computer system carefully and responsibly.


✓ Use required lab resources cautiously

Theory:

Singly link list

A linked list is a type of data structure that stores a collection of non-sequential data items. Unlike
arrays, linked lists are dynamic and their size can be changed during program execution. Each data
item in a linked list has a pointer that holds the memory address of the next data item in the list. The
data items in a linked list may not be stored in consecutive memory locations, but their pointers
make it easy to access them in any order.

A singly linked list, also known as a linear linked list, is a type of linked list in which all nodes are
connected together sequentially. Each node in a singly linked list contains data and a pointer to the

19
Data Structure (3130702) Enrollment No:220220131147
next node. The last node's pointer is set to null. The limitation of a singly linked list is that it can
only be traversed in one direction, in a forward direction.

Operations on singly linked list

✓ Insert
- Insert at first position
- Insert at last position
- Insert into ordered list
✓ Delete
✓ Traverse list (Print list)
✓ Copy linked list

3.1 Write a menu driven program to implement 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 a first node of the linked list.
(e) Delete a node before specified position.
(f) Delete a node after specified position.

Program:

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

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

struct node *start = NULL;

void displaylist(struct node *start);


struct node *insertatfirst(struct node *start);
struct node *insertatend(struct node *start);
struct node *insertInAscOrd(struct node *start);
struct node *deletefirst(struct node *start);
struct node *deletebefore(struct node *start);
struct node *deleteafter(struct node *start);

void main()
{

20
Data Structure (3130702) Enrollment No:220220131147
int n;
clrscr();
do
{
printf("\nEnter 1 to Insert at First\n");
printf("Enter 2 to Insert at End\n");
printf("Enter 3 to Insert in ascending order\n");
printf("Enter 4 to Delete First Element\n");
printf("Enter 5 to Delete before an Element\n");
printf("Enter 6 to delete after an Element\n");
printf("Enter 7 to display Linked List\n");
printf("Enter 8 to Exit\n");

scanf("%d", &n);

switch (n)
{
case 1:
start = insertatfirst(start);
break;

case 2:
start = insertatend(start);
break;

case 3:
start = insertInAscOrd(start);
break;

case 4:
start = deletefirst(start);
break;

case 5:
start = deletebefore(start);
break;

case 6:
start = deleteafter(start);
break;

case 7:
displaylist(start);
break;

case 8:

21
Data Structure (3130702) Enrollment No:220220131147
printf("\n\tEXIT\t\n");
break;
default:
printf("\nEnter valid Input(1 to 8)\n");
break;
}
} while (n != 8);
getch();
}

void displaylist(struct node *start)


{
struct node *ptr;
ptr = start;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}

struct node *insertatfirst(struct node *start)


{
int val;
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the value you want to insert : \n");
scanf("%d", &val);
newnode->data = val;

if (start == NULL)
{
newnode->next = NULL;
start = newnode;
}

else
{
newnode->next = start;
start = newnode;
}

return start;
}

struct node *insertatend(struct node *start)

22
Data Structure (3130702) Enrollment No:220220131147
{
int val;

struct node *newnode;


struct node *ptr = start;

newnode = (struct node *)malloc(sizeof(struct node));

printf("\nEnter the value you want to insert : \n");


scanf("%d", &val);
newnode->data = val;

while (ptr->next != NULL)


{
ptr = ptr->next;
}
newnode->next = NULL;
ptr->next = newnode;

return start;
}

struct node *insertInAscOrd(struct node *start)


{
int val;
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the value you want to insert : \n");
scanf("%d",&val);
newnode->data=val;

struct node *current = start;


while (current->next != NULL && current->next->data < newnode->data)
{
current = current->next;
}

newnode->next = current->next;
current->next = newnode;
return start;
}

struct node *deletefirst(struct node *start)


{
struct node *ptr = start;
start = ptr->next;

23
Data Structure (3130702) Enrollment No:220220131147
free(ptr);
return start;
}

struct node *deletebefore(struct node *start)


{
int val;
struct node *ptr = start;
struct node *pptr = ptr;
struct node *ppptr = pptr;

printf("\nEnter the element whose before element is to be deleted\n");


scanf("%d",&val);

while(ptr->data != val)
{
ppptr=pptr;
pptr=ptr;
ptr=ptr->next;
}

ppptr->next=ptr;
free(pptr);
return start;
}

struct node *deleteafter(struct node *start)


{
int val;
struct node *ptr,*preptr;
ptr=start;
preptr=ptr;

printf("\nEnter the element whose before element is to be deleted\n");


scanf("%d",&val);

while(preptr->data != val)
{
preptr=ptr;
ptr=ptr->next;
}

preptr->next=ptr->next;
free(ptr);
return start;
}

24
Data Structure (3130702) Enrollment No:220220131147
Output:1)

2)

25
Data Structure (3130702) Enrollment No:220220131147
3)

3.2 Write a program to implement stack using linked list

Program:
//Stack using Linked List
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};
struct node *top = NULL;
void display(struct node *);
struct node *push(struct node *);
struct node *pop(struct node *);
int peek(struct node *);

void main()
{
int n;
int v;
clrscr();

26
Data Structure (3130702) Enrollment No:220220131147
do
{
printf("\nEnter 1 to Push\n");
printf("Enter 2 to Pop\n");
printf("Enter 3 to Peek\n");
printf("Enter 4 to Display\n");
printf("Enter 5 to Exit\n");
scanf("%d", &n);

switch (n)
{
case 1:
top = push(top);
break;
case 2:
top = pop(top);
break;

case 3:
v = peek(top);
if (v != -1)
printf("\nThe top Element is %d\n", v);

else
printf("\nThe Stack is Empty\n");
break;

case 4:
display(top);
break;
case 5:
printf("\n\tEXIT\t\n");
break;

default:
printf("\nEnter valid Input(1 to 5)\n");
break;
}
} while (n != 5);
getch();
}

void display(struct node *top)


{
struct node *ptr = top;
if (top == NULL)

27
Data Structure (3130702) Enrollment No:220220131147
{
printf("\nThe Stack is Empty\n");
}

else
{
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
}
struct node *push(struct node *top)
{
int val;
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));

if (newnode == NULL)
{
printf("Stack Overflow");
} else
{
printf("\nEnter the number you want to insert\n");
scanf("%d", &val);
newnode->data = val;
if (top == NULL)
{
newnode->next = NULL;
top = newnode;
}
else
{
newnode->next = top;
top = newnode;
}
}
return top;
}
struct node *pop(struct node *top)
{
if (top == NULL)
{
printf("\nStack Underflow\n");
}

28
Data Structure (3130702) Enrollment No:220220131147
else
{
struct node *ptr = top;
top = ptr->next;
printf("\nThe %d Element is Deleted \n", ptr->data);
free(ptr);
}
return top;
}

int peek(struct node *top)


{
if (top == NULL)
return -1;
else
return top->data;
}

Output:

3.3 Write a program to implement queue using linked list.

29
Data Structure (3130702) Enrollment No:220220131147

Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n***********************Main Menu***************************\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice : \n");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

30
Data Structure (3130702) Enrollment No:220220131147

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value: \n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;

31
Data Structure (3130702) Enrollment No:220220131147
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{
while(ptr != NULL)
{
printf("%d ",ptr -> data);
ptr = ptr -> next;
}
}
}

Output:

32
Data Structure (3130702) Enrollment No:220220131147

Observations:
• Insertion/deletion operations at the front of the linked list are the most efficient. This
is because only the head pointer needs to be updated.
• Insertion/deletion operations at the end of the linked list require traversing the entire
linked list to reach the tail node. This can be inefficient for long linked lists.
• Inserting a node in ascending order requires scanning the linked list to find the
appropriate position to insert the new node. This can be inefficient for large linked
lists.
• Deleting a node before/after a specified position requires traversing the linked list to
reach the node to be deleted and the node before/after it. This can be inefficient for
large linked lists.

Conclusion:
The conclusion of the experiment on singly linked list is that it is a versatile and efficient data
structure for a variety of applications. It is flexible, dynamic, and efficient for storing
sequential data.
However, it is important to note that insertion/deletion operations at the end of the linked list
and inserting a node in ascending order can be inefficient for large linked lists. This is because
these operations require traversing the entire linked list.
Overall, singly linked lists are a good choice for implementing data structures that require
frequent insertion/deletion operations at the front of the list or for storing sequential data.

Quiz:
(1) Which are the operations on singly link list?
The following are the common operations on singly linked lists:
• Insertion: Adding a new node to the linked list.
• Deletion: Removing a node from the linked list.
• Search: Finding a specific node in the linked list.
• Traversal: Visiting each node in the linked list in a specific order

(2) State the limitation of singly link list


• Singly linked list can only move forward, so it is difficult to traverse in a reverse
way.
• Singly linked list uses more space than arrays because of the use of pointers and
data storage.
• Accessing the preceding node of a current node is not possible as there is no
backward traversal.
• Accessing a node is very time-consuming.
• Any element of a singly linked list can only be accessed in a sequential manner
making binary search completely ineffective.

(3) Compare array and singly link list

33
Data Structure (3130702) Enrollment No:220220131147
S.No. ARRAY LINKED LIST
1. An array is a grouping of A linked list is a group of
data elements of entities called a node. The
equivalent data type. node includes two
segments: data and
address.
2. It stores the data It stores elements
elements in a contiguous randomly, or we can say
memory zone. anywhere in the memory
zone.
3. In the case of an array, In the linked list, the
memory size is fixed, and placement of elements is
it is not possible to allocated during the run
change it during the run time.
time.
4. The elements are not The data elements are
dependent on each other. dependent on each other.

Suggested Reference:

1. An Introduction to Data Structures with Applications. by Jean-Paul Tremblay & Paul G.


Sorenson Publisher-Tata McGraw Hill.
2. Data Structures using C & C++ -By Ten Baum Publisher – Prenctice-Hall International
3. Fundamentals of Computer Algorithms by Horowitz, Sahni,Galgotia Pub. 2001 ed.
4. http://www.geeksforgeeks.org/data-structures/
5. http://www.coursera.org/specializations/data-structures-algorithms

References used by the students:

https://bard.google.com/chat
https://www.javatpoint.com

Rubric-wise marks obtained:

Problem Coding Completeness


Logic
Understanding Standards and accuracy Q&A
Rubrics Building (2) Total
(2) (2) (2)
Avg. Good Avg. Good Avg. Good Avg. Good Avg. Good
(1) (2) (1) (2) (1) (2) (1) (2) (1) (2)

Marks

34
Data Structure (3130702) Enrollment No:220220131147

Experiment No: 4

AIM : Doubly linked list

4.1 Write a program to implement 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 a last node of the linked list.
(d) Delete a node before specified position.

Date:22-09-2023

Competency and Practical Skills: Logic building and programming

Relevant CO: CO2, CO5

Objectives: (a) To understand the concepts of doubly linked list


(b) To analyze different algorithms on doubly link list
(c) To implement various operations on doubly link list

Equipment/Instruments: Computer System with turbo C/C++

Safety and necessary Precautions:

✓ Operate computer system carefully and responsibly.


✓ Use required lab resources cautiously

Theory:

Doubly linked list

A doubly linked list is a data structure where each node contains data and two pointers - one to point
to the previous node (LPTR) and another to point to the next node (RPTR). The main advantage of
a doubly linked list is that we can traverse it in any direction, either forward or backward. Another
advantage is that we can delete a node with ease since we have pointers to both the previous and
next nodes. In contrast, a node on a singly linked list cannot be removed unless we have a pointer
to its predecessor. However, the drawback of a doubly linked list is that it requires more memory
than a singly linked list since we need an extra pointer to point to the previous node. In the image,
L and R denote the leftmost and rightmost nodes in the list, respectively. The left link of the L node
and the right link of the R node are both NULL, indicating the end of the list for each direction.

Operations on doubly linked list

✓ Insert
- Insert at first position
35
Data Structure (3130702) Enrollment No:220220131147
- Insert at last position
- Insert into ordered list
✓ Delete
✓ Traverse list (Print list)
✓ Copy linked list

4.1 Write a program to implement 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 a last node of the linked list.
(d) Delete a node before specified position.

(a) Insert a node at the front of the linked list.

Program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

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

struct node *start = NULL;

void displaylist(struct node *start);


struct node *insertatfirst(struct node *start);
struct node *insertatend(struct node *start);
struct node *deletelast(struct node *start);
struct node *deletebefore(struct node *start);

void main()
{
int n;
do
{
printf("\nEnter 1 to Insert at First\n");
printf("Enter 2 to Insert at End\n");
printf("Enter 3 to Delete Last Element\n");
printf("Enter 4 to Delete before an Element\n");
printf("Enter 5 to display Linked List\n");
printf("Enter 6 to Exit\n");

36
Data Structure (3130702) Enrollment No:220220131147
scanf("%d", &n);

switch (n)
{
case 1:
start = insertatfirst(start);
break;

case 2:
start = insertatend(start);
break;

case 3:
start = deletelast(start);
break;

case 4:
start = deletebefore(start);
break;

case 5:
displaylist(start);
break;

case 6:
printf("\n\tEXIT\t\n");
break;

default:
printf("\nEnter valid Input(1 to 6)\n");
break;
}
} while (n != 6);
getch();
}

void displaylist(struct node *start)


{
struct node *ptr = start;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}

37
Data Structure (3130702) Enrollment No:220220131147
struct node *insertatfirst(struct node *start)
{
int val;
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the Element you want to insert\n");
scanf("%d", &val);
newnode->data = val;

if (start == NULL)
{
newnode->next = NULL;
newnode->prev = NULL;
start = newnode;
}

else
{
newnode->next = start;
newnode->prev = NULL;
start = newnode;
}

return start;
}
Output:

(b) Insert a node at the end of the linked list.

38
Data Structure (3130702) Enrollment No:220220131147
Program:
struct node *insertatend(struct node *start)
{
int val;
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the Element you want to insert\n");
scanf("%d", &val);
newnode->data = val;
if (start == NULL)
{
newnode->next = NULL;
newnode->prev = NULL;
start = newnode;
}
else
{
struct node *ptr = start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newnode;
newnode->prev = ptr;
newnode->next = NULL;
}
return start;
}Output:

39
Data Structure (3130702) Enrollment No:220220131147
(c) Delete a last node of the linked list.

Program:
struct node *deletelast(struct node *start)
{
struct node *ptr, *preptr;
ptr = start;
preptr = ptr;
while (ptr->next != NULL)
{
preptr = ptr;
ptr = ptr->next;
}

preptr->next = NULL;
free(ptr);

return start;
}

Output:

(d) Delete a node before specified position.

Program:
struct node *deletebefore(struct node *start)
{
int val;

40
Data Structure (3130702) Enrollment No:220220131147
struct node *ptr, *pptr,*ppptr;
ptr=start;
pptr=ptr;
ppptr=pptr;

printf("\nEnter the value before which the Element is to be deleted\n");


scanf("%d",&val);
while(ptr->data != val)
{
ppptr=pptr;
pptr=ptr;
ptr=ptr->next;
}

ppptr->next=ptr;
ptr->prev=ppptr;
free(pptr);

return start;
}
Output:

Observations:
The observations of the experiment to implement insert and delete operations on doubly
linked list in C are as follows:
• Doubly linked lists can be used to implement both insert and delete operations
efficiently.
• Inserting a node at the front of the linked list is a constant time operation, as it only
requires updating the head pointer of the linked list.

41
Data Structure (3130702) Enrollment No:220220131147
• Inserting a node at the end of the linked list requires traversing the entire linked list
to find the last node. However, this operation can be made more efficient by keeping
track of the tail pointer of the linked list.
• Deleting the last node of the linked list is a constant time operation, as it only requires
updating the pointer of the previous node of the last node to NULL.
• Deleting a node before a specified position requires traversing the linked list up to the
node before the specified position. This operation can be made more efficient by
keeping track of a pointer to the previous node of the current node.

Conclusion:
Based on the observations of the experiment, the following conclusions can be drawn:
• Doubly linked lists are a versatile and efficient data structure for implementing a
variety of operations, such as insertion, deletion, and traversal.
• Doubly linked lists are more flexible than singly linked lists, as they allow for traversal
in both directions.
• Doubly linked lists can be used to implement other data structures, such as stacks,
queues, and deques.
• Doubly linked lists are more memory-intensive than singly linked lists, but the
additional memory overhead is justified by the flexibility and efficiency of doubly
linked lists.

Overall, doubly linked lists are a useful data structure with a variety of applications, and they
are often used in real-world software systems.

Quiz:
(1) Explain structure of a node of doubly link list
The structure of a node in a doubly linked list consists of three fields:

a) Data field: This field stores the data of the node. It can be of any type, such as an integer,
string, or object.
b) Previous pointer: This pointer points to the previous node in the linked list. If the node is
the head of the linked list, then the previous pointer is NULL.
c) Next pointer: This pointer points to the next node in the linked list. If the node is the tail of
the linked list, then the next pointer is NULL.

(2) Which is the main advantage of doubly link list?


The main advantage of a doubly linked list is that it allows for traversal in both
directions. This makes it more flexible and efficient than a singly linked list, which can
only be traversed in one direction.

Here are some specific advantages of doubly linked lists:

a) Efficient traversal in both directions: Doubly linked lists can be traversed in both
forward and backward directions. This is useful for applications where we need to

42
Data Structure (3130702) Enrollment No:220220131147
access data from the middle or back of the list.
b) Faster deletion of nodes: Deleting a node from a doubly linked list is faster than
deleting a node from a singly linked list. This is because we do not need to traverse
the entire list to find the previous node of the node to be deleted.
c) Increased flexibility in algorithms and data structures: Doubly linked lists can be
used to implement a variety of algorithms and data structures, such as stacks, queues,
and deques. This is because they allow for both insertion and deletion at both ends
of the list.

(3) What is the drawback of doubly link list?


Here are some other drawbacks of doubly linked lists:

a) More complex implementation: Doubly linked lists are more complex to implement
than singly linked lists, as they require additional code to maintain the previous and
next pointers.
b) Slower insertion and deletion at random positions: Inserting and deleting nodes at
random positions in a doubly linked list is slower than in a singly linked list, as we
need to traverse the list to find the previous and next nodes of the node to be inserted
or deleted.
c) Increased overhead for pointer management: Doubly linked lists require more
pointer management overhead than singly linked lists. This is because we need to
ensure that the previous and next pointers of each node are always pointing to valid
nodes.

Suggested Reference:

1. An Introduction to Data Structures with Applications. by Jean-Paul Tremblay & Paul G.


Sorenson Publisher-Tata McGraw Hill.
2. Data Structures using C & C++ -By Ten Baum Publisher – Prenctice-Hall International
3. Fundamentals of Computer Algorithms by Horowitz, Sahni, Galgotia Pub. 2001 ed.
4. http://www.geeksforgeeks.org/data-structures/
5. http://www.coursera.org/specializations/data-structures-algorithms

References used by the students:


https://www.geeksforgeeks.org
https://www.javatpoint.com

Rubric-wise marks obtained:

Problem Coding Completeness


Logic
Understanding Standards and accuracy Q&A
Rubrics Building (2) Total
(2) (2) (2)
Avg. Good Avg. Good Avg. Good Avg. Good Avg. Good
(1) (2) (1) (2) (1) (2) (1) (2) (1) (2)

Marks

43
Data Structure (3130702) Enrollment No:220220131147

Experiment No: 5

AIM : Circular linked list

5.1 Write a program to implement 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 a first node of the linked list.
(d) Delete a node after specified position.
5.2 Identify widely used application which uses linked list for implementation of its
important feature.

Date:29-09-202

Competency and Practical Skills: Logic building and programming

Relevant CO: CO2, CO5

Objectives: (a) To understand the concepts of circular linked list


(b) To analyze different algorithms on circular link list
(c) To implement various operations on circular link list

Equipment/Instruments: Computer System with turbo C/C++

Safety and necessary Precautions:

✓ Operate computer system carefully and responsibly.


✓ Use required lab resources cautiously

Theory:

Circular linked list

A circular linked list is similar to a singly linked list, except that the last node points to the first
node, creating a circular arrangement of nodes. Unlike a singly linked list, it does not contain null
pointers. Traversal can only be done in one direction, i.e., the forward direction. The biggest
advantage of a circular linked list is that it saves time when we want to go from the last node to the
first node because it directly points to the first node. A good example of an application where a
circular linked list can be used is a time-sharing problem that can be solved by the operating system.

Operations on circular linked list

✓ Insert
- Insert at first position
- Insert at last position
44
Data Structure (3130702) Enrollment No:220220131147
- Insert into ordered list
✓ Delete
✓ Traverse list (Print list)
5.1 Write a program to implement 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 a first node of the linked list.
(d) Delete a node after specified position.

(a) Insert a node at the end of the linked list.

Program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};
struct node *start = NULL;

void displaycll(struct node *start);


struct node *createcll(struct node *start);
struct node *insertatend(struct node *start);
struct node *insertbsp(struct node *start);
struct node *deletefirst(struct node *start);
struct node *deleteasp(struct node *start);

void main()
{
int n;
do
{
printf("\nEnter 1 to Create Circular Linked List\n");
printf("Enter 2 to Insert at end\n");
printf("Enter 3 to Insert before a specified position\n");
printf("Enter 4 to Delete First Node\n");
printf("Enter 5 to Delete a Node after specified position\n");
printf("Enter 6 to display Linked List\n");
printf("Enter 7 to Exit\n");

scanf("%d", &n);

switch (n)

45
Data Structure (3130702) Enrollment No:220220131147
{
case 1:
start = createcll(start);
break;
case 2:
start = insertatend(start);
break;
case 3:
start = insertbsp(start);
break;
case 4:
start = deletefirst(start);
break;
case 5:
start = deleteasp(start);
break;
case 6:
displaycll(start);
break;
case 7:
printf("\n\tEXIT\t\n");
break;
default:
printf("\nEnter valid Input(1 to 8)\n");
break;
}
} while (n != 7);
getch();
}

void displaycll(struct node *start)


{
struct node *ptr;
ptr = start;
do
{
printf("%d ", ptr->data);
ptr = ptr->next;
} while (ptr != start);
}
struct node *createcll(struct node *start)
{
int z, n;
struct node *newnode, *ptr;
printf("Enter How many number you want to enter :\n");
scanf("%d", &z);

46
Data Structure (3130702) Enrollment No:220220131147

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


{
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the value you want to insert :\n");
scanf("%d", &n);
newnode->data = n;
if (start == NULL)
{
newnode->next = newnode;
start = newnode;
}
else
{
ptr = start;
while (ptr->next != start)
{
ptr = ptr->next;
}
ptr->next = newnode;
newnode->next = start;
}
}
return start;
}
struct node *insertatend(struct node *start)
{
int val;
struct node *newnode;
struct node *ptr;
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the value you want to insert\n");
scanf("%d", &val);

newnode->data = val;

ptr = start;
while (ptr->next != start)
{
ptr = ptr->next;
}
ptr->next = newnode;
newnode->next = start;

return start;
}

47
Data Structure (3130702) Enrollment No:220220131147

Output:

(b) Insert a node before specified position.

Program:
struct node *insertbsp(struct node *start)
{
int val,num;
struct node *newnode,*ptr,*preptr;

printf("\nEnter the value before which you want to insert data :\n");
scanf("%d",&val);

printf("\nEnter the value you want to insert :\n");


scanf("%d",&num);

newnode=(struct node *)malloc(sizeof(struct node));


newnode->data=num;
ptr=start;
preptr=ptr;

while(ptr->data != val)
{
preptr=ptr;
ptr=ptr->next;
}

preptr->next=newnode;

48
Data Structure (3130702) Enrollment No:220220131147
newnode->next=ptr;

return start;
}

Output:

(c) Delete a first node of the linked list.

Program:
struct node *deletefirst(struct node *start)
{
struct node *ptr=start;
struct node *ptr1=start;

while(ptr1->next != start)
{
ptr1=ptr1->next;
}

start=start->next;
ptr1->next=start;
free(ptr);

return start;
}

49
Data Structure (3130702) Enrollment No:220220131147
Output:

(d) Delete a node after specified position.

Program: struct node *deleteasp(struct node *start)


{
int val;
struct node *ptr=start;
struct node *aptr=ptr->next;

printf("\nEnter the element before which you want to delete\n");


scanf("%d",&val);

while(ptr->data !=val)
{
ptr=ptr->next;
aptr=aptr->next;
}

ptr->next=aptr->next;

free(aptr);

return start;
}

Output:

50
Data Structure (3130702) Enrollment No:220220131147

5.2 Identify widely used application which uses linked list for implementation of its
important feature.
Here are some other examples of widely used applications that use linked lists for implementation
of their important features:

a) Operating systems: Linked lists are used in operating systems to implement various data
structures, such as memory management, file systems, and process scheduling.
b) Compilers: Linked lists are used in compilers to implement various data structures, such
as the symbol table and the abstract syntax tree.
c) Databases: Linked lists are used in databases to implement various data structures, such
as B-trees and hash tables.
d) Graphics processing units (GPUs): Linked lists are used in GPUs to implement various
data structures, such as linked display lists and vertex buffers.
Overall, linked lists are a versatile and efficient data structure that is used in a wide variety of
applications.

Observations:
The observations of the experiment on circular linked list are as follows:

a) Circular linked lists are efficient for implementing operations such as insertion and
deletion at the beginning and end of the list.
b) Circular linked lists are also efficient for iterating over the list in either direction.
c) Circular linked lists can be used to implement various data structures, such as queues,
stacks, and hash tables.

Conclusion:
The conclusion of the experiment on circular linked lists is that they are an efficient and
versatile data structure with a wide range of applications.

51
Data Structure (3130702) Enrollment No:220220131147

Circular linked lists are efficient for implementing operations such as insertion and deletion
at the beginning and end of the list, as well as for iterating over the list in either direction.
They can be used to implement various data structures, such as queues, stacks, and hash
tables.

Quiz:

(1) What are disadvantages of circular link list?


The disadvantages of circular linked lists are as follows:

a) Complexity: Circular linked lists can be more complex to implement than other types of
linked lists, such as singly linked lists and doubly linked lists.
b) Memory leaks: If the pointers in a circular linked list are not managed properly, memory
leaks can occur. This is because the circular nature of the list can make it difficult to track
and free unused nodes.
c) Traversal can be more difficult: While traversal of a circular linked list can be efficient, it
can also be more difficult than linear traversal, especially if the circular list has a complex
structure.
d) Lack of a natural end: The circular structure of the list can make it difficult to determine
when the end of the list has been reached. This can be a problem in certain applications,
such as when processing a list of data in a linear fashion.

(2) Differentiate Circular link list and Queue


A queue is a first-in-first-out (FIFO) data structure. Elements are added to the back of the
queue and removed from the front of the queue.
Queues are often implemented using circular linked lists, as this allows for efficient insertion
and deletion of elements. However, queues can also be implemented using other data
structures, such as arrays and linked lists.

Differences between circular linked lists and queues

Characteristics Circular Linked List Queue


Definition A linked list where the last A first-in-first-out (FIFO)
node points back to the first data structure.
node, forming a loop.
Common use Implementing queues, Storing and processing data
stacks, B-trees, and hash in a first-in-first-out order.
tables.
Efficiency Efficient for insertion, Efficient for insertion and
deletion, and traversal. deletion.
Drawbacks Can be more complex to May not be the best choice
implement and manage than where random access to
other types of linked lists. elements is required.

52
Data Structure (3130702) Enrollment No:220220131147

(3) Which are the operations on circular link list?


The following are some of the most common operations on a circular linked list:
• Insertion: Inserting a node into a circular linked list can be done at the beginning of the list,
at the end of the list, or after a specific node in the list.
• Deletion: Deleting a node from a circular linked list can be done at the beginning of the list,
at the end of the list, or before a specific node in the list.
• Traversal: Traversing a circular linked list can be done in either direction, starting from any
node in the list.
• Search: Searching for a node in a circular linked list can be done by iterating over the list
and comparing the data in each node to the search key.
• Reversal: Reversing a circular linked list can be done by iterating over the list and swapping
the pointers of adjacent nodes.

Suggested Reference:

1. An Introduction to Data Structures with Applications. by Jean-Paul Tremblay & Paul G.


Sorenson Publisher-Tata McGraw Hill.
2. Data Structures using C & C++ -By Ten Baum Publisher – Prenctice-Hall International
3. Fundamentals of Computer Algorithms by Horowitz, Sahni,Galgotia Pub. 2001 ed.
4. http://www.geeksforgeeks.org/data-structures/
5. http://www.coursera.org/specializations/data-structures-algorithms

References used by the students:

https://www.geeksforgeeks.org
https://www.javatpoint.com

Rubric-wise marks obtained:

Problem Coding Completeness


Logic
Understanding Standards and accuracy Q&A
Rubrics Building (2) Total
(2) (2) (2)
Avg. Good Avg. Good Avg. Good Avg. Good Avg. Good
(1) (2) (1) (2) (1) (2) (1) (2) (1) (2)

Marks

53

You might also like