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

dsa

The document discusses linked lists, a key data structure that allows for efficient storage and organization of elements. It outlines three types of linked lists: single linked lists, double linked lists, and circular linked lists, each with specific advantages for different programming tasks. The document also includes code examples for implementing these linked lists in C, along with acknowledgments and a conclusion on the importance of mastering these data structures.

Uploaded by

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

dsa

The document discusses linked lists, a key data structure that allows for efficient storage and organization of elements. It outlines three types of linked lists: single linked lists, double linked lists, and circular linked lists, each with specific advantages for different programming tasks. The document also includes code examples for implementing these linked lists in C, along with acknowledgments and a conclusion on the importance of mastering these data structures.

Uploaded by

surajpal7442
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Dr.

Sudhir Chandra Sur


Institute of Technology &
Sports Complex

CA 2 ASSIGNMENT
Name: Supratik Mitra
University Roll Number:25500123169
University Registration Number:232550110319
Year:2nd
Semester:3rd
Section: C
Topic Name: Linked List and Its Types
Subject Name: Data Structures and Algorithm
Subject Code: PCC-CS301
Deparment: Computer Science and Engineering
Linked lists are key data structures used for storing and organizing
collections of elements efficiently. Unlike arrays, they can dynamically
adjust their size, allowing flexible memory use. Linked lists consist of
nodes, where each node holds data and a pointer to the next node in the
sequence.

There are different types of linked lists, each serving specific purposes:

1. Single Linked List: This basic form of a linked list connects each node to
the next in sequence. It allows efficient additions and deletions at the
start but can be slower when accessing or modifying nodes in the middle.

2.Double Linked List: Each node has two pointers—one to the next node
and one to the previous. This allows easy traversal in both directions and
makes deleting nodes simpler compared to single linked lists.

3. Circular Linked List: In this variation, the last node connects back to the
first, creating a loop. Circular linked lists are useful for tasks that require
continuous traversal, like round-robin scheduling or circular buffers.

Understanding these types is important for creating efficient data


management strategies in programming, as each linked list offers specific
advantages for different tasks.
#include <stdlib.h> switch (choice) {
#include <stdio.h> case 1:
void add_first(); add_first();
void remove_nth(); break;
struct element { case 2:
int value;
remove_nth();
struct element* next;
break;
};
case 3:
struct element* start = NULL;
printf("Exiting
int main() { program.\n");
int choice; exit(0);
default:
while (1) {
printf("Invalid choice!
printf("\nEnter your Please enter 1, 2, or 3.\n");
choice:\n");
}
printf("1. Add at
first\n");
}

printf("2. Remove nth return 0;


element\n"); }
printf("3. Exit\n");
scanf("%d", &choice);
Single Linked list: Source
Code(Continued)
void add_first() { void remove_nth() {
int position, i;
int value;
struct element* current = start;
struct element*
struct element* previous = NULL;
new_node = (struct
element*)malloc(sizeof( if (start == NULL) {
struct element)); printf("The list is empty! Nothing to
remove.\n");
if (new_node ==
return; }
NULL) {
printf("Enter the position of the element to
printf("Memory remove: ");
allocation failed!\n");
scanf("%d", &position);
return; } if (position == 1) {
printf("Enter the start = start->next;
value to insert: "); printf("Element with value %d at position 1
scanf("%d", &value); removed.\n", current->value);
free(current);
new_node->value =
value; return; }
for (i = 1; i < position && current != NULL; i++) {
new_node->next =
start; previous = current;
current = current->next;}
start = new_node;
if (current == NULL) {
printf("Element with
printf("Position %d is out of bounds. Element
value %d added at the
not found.\n", position);
beginning.\n", value);
return; }
}

previous->next = current->next;
printf("Element with value %d at position %d
Single Linked list: Output
Double linked list: source code
#include <stdio.h> switch (choice) {
#include <stdlib.h> case 1:
void remove_first(); append_end();
void append_end(); break;
struct element { case 2:
struct element* previous;
remove_first();
struct element* next;
break;
int value;
case 3:
};
exit(0);
struct element* start = NULL;
default:
int main() {
printf("Invalid
int choice;
choice!\n");
while (1) {
}
printf("\nEnter your
}
choice:\n");
printf("1. Add at end \n");
printf("2. Remove from return 0;
start\n"); }
printf("3. Exit\n");
scanf("%d", &choice);
Double linked list: source code (Continued)
void append_end() { void remove_first() {
struct element *new_node, *current;
struct element *temp;
int num;

if (start == NULL) {
new_node = (struct element*)malloc(sizeof(struct element));

if (new_node == NULL) { printf("No node to


remove.\n");
printf("No node is created\n");

return; } else {
} else { if (start->next == NULL) {
printf("Enter the value: "); printf("%d is removed\n",
scanf("%d", &num); start->value);
new_node->value = num; free(start);
if (start == NULL) {
start = NULL;
new_node->previous = NULL;
} else {
new_node->next = NULL;

start = new_node;
temp = start;

printf("%d is added\n", num); start = start->next;


} else { printf("%d is the removed
current = start; value\n", temp->value);
while (current->next != NULL) { start->previous = NULL;
current = current->next; free(temp);
}
}
current->next = new_node;
}
new_node->previous = current;

new_node->next = NULL; }
printf("%d is added at the end\n", num);

}}

}
Double linked list: Output
Circular linked list(Using double
linked list): source code

#include <stdlib.h> void remove_first_item() {


#include <stdio.h> struct item *temp;
struct item {
struct item* before; if (top == NULL) {
struct item* after; printf("Nothing to remove !!\n");
int number; return;
}; }
struct item* top = NULL; else if (top->after == top) {
int main() { temp = top;
int choice; printf("%d is removed !!\n", temp->number);
while (1) { top = NULL;
printf("Press 1 to Remove from free(temp);
first\n");
}
printf("Press any other key to
else {
exit\n");
temp = top;
scanf("%d", &choice);
top = top->after;
switch (choice) {
top->before = temp->before;
case 1:
temp->before->after = top;
remove_first_item();
printf("%d is removed !!\n", temp->number);
break;
free(temp);
default:
}
exit(0); }}
}
return 0;}
ACKNOWLEDGEMENT

I am deeply grateful to Mr. Soham Mukherjee for his


exceptional guidance and support throughout this project
on Linked List and Its Types. His expertise and insightful
feedback were instrumental in my understanding and
successful implementation of linked list operations in C. His
dedication to teaching and constant encouragement have
been a tremendous source of inspiration.

~ Supratik Mitra
-Thank You!

CONCLUSION
In conclusion, delving into the topic of Linked Lists and
Their Types has significantly broadened my understanding
of data structures and their applications. Mastering the
implementation and manipulation of linked lists has
enhanced my ability to tackle various programming
challenges efficiently. This project has been a crucial part
of my academic development, providing a robust
foundation in data structures and deepening my
appreciation for their practical utility in software
development.

You might also like