Data stractureande Algorithm linked list
Data stractureande Algorithm linked list
Submitted To Mr . KIBRU G.
Haramaya, Ethiopia
MAY 5. 2025GC
1
Data Structures and Algorithms
Individual Assignment One
1. Write a C++ program to implement the following operations on Doubly Linked List.
a. Insert element at the beginning
b. Insert element at the End
c. Insert element at the Middle
d. Delete element at the beginning
e. Delete element at the End
f. Delete element at the Middle
g. Sort elements of the list in ascending and/or descending order
h. Search an Element on a Linked List
ANSWER for StudentInfo (BY Doubly Linked List)
#include <iostream>
#include <string>
using namespace std;
struct StudentInfo {
int ID;
string name;
char sex;
string college;
string dept;
int classYear;
int semester;
int section;
float cgpa;
StudentInfo* next;
StudentInfo* prev;
};
StudentInfo inputStudent() {
StudentInfo s;
cout << "Enter Student Details:\n";
cout << "ID: "; cin >> s.ID;
cout << "Name: "; cin >> s.name;
cout << "Sex: "; cin >> s.sex;
cout << "College: "; cin >> s.college;
cout << "Department: "; cin >> s.dept;
cout << "Class Year: "; cin >> s.classYear;
cout << "Semester: "; cin >> s.semester;
cout << "Section: "; cin >> s.section;
cout << "CGPA: "; cin >> s.cgpa;
s.next = s.prev = nullptr;
return s;
}
void insertAtBeginning(StudentInfo s) {
StudentInfo* newNode = new StudentInfo(s);
if (!head) {
head = newNode;
} else {
2
newNode->next = head;
head->prev = newNode;
head = newNode;
}
cout << "Inserted at beginning.\n";
}
void insertAtEnd(StudentInfo s) {
StudentInfo* newNode = new StudentInfo(s);
if (!head) {
head = newNode;
} else {
StudentInfo* temp = head;
while (temp->next)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
cout << "Inserted at end.\n";
}
void deleteAtBeginning() {
if (!head) {
cout << "List is empty.\n";
return;
}
StudentInfo* temp = head;
head = head->next;
if (head)
head->prev = nullptr;
delete temp;
cout << "Deleted from beginning.\n";
}
void deleteAtEnd() {
if (!head) {
cout << "List is empty.\n";
3
return;
}
StudentInfo* temp = head;
if (!temp->next) {
delete temp;
head = nullptr;
} else {
while (temp->next)
temp = temp->next;
temp->prev->next = nullptr;
delete temp;
}
cout << "Deleted from end.\n";
}
4
void searchByID(int id) {
if (!head) {
cout << "List is empty.\n";
return;
}
StudentInfo* temp = head;
while (temp) {
if (temp->ID == id) {
cout << "\nStudent Found:\n";
cout << "ID: " << temp->ID << "\nName: " << temp->name
<< "\nSex: " << temp->sex << "\nCollege: " << temp->college
<< "\nDepartment: " << temp->dept << "\nClass Year: " << temp->classYear
<< "\nSemester: " << temp->semester << "\nSection: " << temp->section
<< "\nCGPA: " << temp->cgpa << endl;
return;
}
temp = temp->next;
}
cout << "Student not found.\n";
}
void displayAll() {
if (!head) {
cout << "List is empty.\n";
return;
}
StudentInfo* temp = head;
cout << "\nAll Student Records:\n";
while (temp) {
cout << "----------------------------\n";
cout << "ID: " << temp->ID
<< "\nName: " << temp->name
<< "\nSex: " << temp->sex
<< "\nCollege: " << temp->college
<< "\nDepartment: " << temp->dept
<< "\nClass Year: " << temp->classYear
<< "\nSemester: " << temp->semester
<< "\nSection: " << temp->section
<< "\nCGPA: " << temp->cgpa << "\n";
temp = temp->next;
}
}
int main() {
int choice, position, id;
StudentInfo s;
while (true) {
cout << "\n->Student Management System (By Doubly Linked List)<-\n"
<< "1. Insert Student at the beginning\n"
<< "2. Insert Student at the End\n"
<< "3. Insert Student at the Middle\n"
<< "4. Delete Student at the beginning\n"
<< "5. Delete Student at the End\n"
<< "6. Delete Student at the Middle\n"
<< "7. Sort Student list in ascending order\n"
<< "8. Sort Student list in descending order\n"
5
<< "9. Search Student by ID\n"
<< "10. Display All\n"
<< "11. Exit\n"
<< "----------------------------------\n"
<< "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
s = inputStudent();
insertAtBeginning(s);
break;
case 2:
s = inputStudent();
insertAtEnd(s);
break;
case 3:
s = inputStudent();
cout << "Enter position: ";
cin >> position;
insertAtMiddle(s, position);
break;
case 4:
deleteAtBeginning();
break;
case 5:
deleteAtEnd();
break;
case 6:
cout << "Enter position: ";
cin >> position;
deleteAtMiddle(position);
break;
case 7:
sortList(true);
break;
case 8:
sortList(false);
break;
case 9:
cout << "Enter ID to search: ";
cin >> id;
searchByID(id);
break;
case 10:
displayAll();
break;
case 11:
return 0;
default:
cout << "Invalid choice.\n";
}
}
return 0;
}
--------------------------------------------END OF CODE PART 1-----------------------------------------------------
--
6
2. Write a C++ program to implement the following operations on Singly Circular Linked List.
a. Insert element at the beginning
b. Insert element at the End
c. Insert element at the Middle
d. Delete element at the beginning
e. Delete element at the End
f. Delete element at the Middle
g. Sort elements of the list in ascending and/or descending order
h. Search an Element on a Linked List.
ANSWER for StudentInfo (BY Singly Circular Linked List.)
#include <iostream>
#include <string>
using namespace std;
struct studentInfo {
int ID;
string name;
char sex;
string college;
string dept;
int classYear;
int semester;
int section;
float cgpa;
studentInfo* next;
};
studentInfo inputStudent() {
studentInfo s;
cout << "Enter Student Details:\n";
cout << "ID: "; cin >> s.ID;
cout << "Name: "; cin >> s.name;
cout << "Sex: "; cin >> s.sex;
cout << "College: "; cin >> s.college;
cout << "Department: "; cin >> s.dept;
cout << "Class Year: "; cin >> s.classYear;
cout << "Semester: "; cin >> s.semester;
cout << "Section: "; cin >> s.section;
cout << "CGPA: "; cin >> s.cgpa;
s.next = nullptr;
return s;
}
void insertAtBeginning(studentInfo s) {
studentInfo* newNode = new studentInfo(s);
if (!head) {
head = newNode;
newNode->next = head;
} else {
studentInfo* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newNode;
newNode->next = head;
head = newNode;
7
}
cout << "Inserted at beginning.\n";
}
void insertAtEnd(studentInfo s) {
studentInfo* newNode = new studentInfo(s);
if (!head) {
head = newNode;
newNode->next = head;
} else {
studentInfo* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
cout << "Inserted at end.\n";
}
void deleteAtBeginning() {
if (!head) {
cout << "List is empty.\n";
return;
}
if (head->next == head) {
delete head;
head = nullptr;
} else {
studentInfo* temp = head;
studentInfo* last = head;
while (last->next != head)
last = last->next;
head = head->next;
last->next = head;
delete temp;
}
cout << "Deleted from beginning.\n";
}
void deleteAtEnd() {
8
if (!head) {
cout << "List is empty.\n";
return;
}
if (head->next == head) {
delete head;
head = nullptr;
} else {
studentInfo* temp = head;
studentInfo* prev = nullptr;
while (temp->next != head) {
prev = temp;
temp = temp->next;
}
prev->next = head;
delete temp;
}
cout << "Deleted from end.\n";
}
9
}
}
cout << "List sorted in " << (ascending ? "ascending" : "descending") << " order.\n";
}
void displayAll() {
if (!head) {
cout << "List is empty.\n";
return;
}
studentInfo* temp = head;
cout << "\nAll Student Records:\n";
do {
cout << "----------------------------\n";
cout << "ID: " << temp->ID
<< "\nName: " << temp->name
<< "\nSex: " << temp->sex
<< "\nCollege: " << temp->college
<< "\nDepartment: " << temp->dept
<< "\nClass Year: " << temp->classYear
<< "\nSemester: " << temp->semester
<< "\nSection: " << temp->section
<< "\nCGPA: " << temp->cgpa << "\n";
temp = temp->next;
} while (temp != head);
}
int main() {
int choice, position, id;
studentInfo s;
while (true) {
cout << "\n--- Student Management System ---\n"
<< "1. Insert Student at the beginning\n"
<< "2. Insert Student at the End\n"
<< "3. Insert Student at the Middle\n"
<< "4. Delete Student at the beginning\n"
<< "5. Delete Student at the End\n"
10
<< "6. Delete Student at the Middle\n"
<< "7. Sort Student list in ascending order\n"
<< "8. Sort Student list in descending order\n"
<< "9. Search Student by ID\n"
<< "10. Display All\n"
<< "11. Exit\n"
<< "----------------------------------\n"
<< "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
s = inputStudent();
insertAtBeginning(s);
break;
case 2:
s = inputStudent();
insertAtEnd(s);
break;
case 3:
s = inputStudent();
cout << "Enter position: ";
cin >> position;
insertAtMiddle(s, position);
break;
case 4:
deleteAtBeginning();
break;
case 5:
deleteAtEnd();
break;
case 6:
cout << "Enter position: ";
cin >> position;
deleteAtMiddle(position);
break;
case 7:
sortList(true);
break;
case 8:
sortList(false);
break;
case 9:
cout << "Enter ID to search: ";
cin >> id;
searchByID(id);
break;
case 10:
displayAll();
break;
case 11:
return 0;
default:
cout << "Invalid choice.\n";
}
}
return 0;
}
11
--------------------------------------------END OF CODE PART 2---------------------------------------------------
3.Write a C++ program to implement the following operations on Doubly Circular Linked List.
a. Insert element at the beginning
b. Insert element at the End
c. Insert element at the Middle
d. Delete element at the beginning
e. Delete element at the End
f. Delete element at the Middle
g. Sort elements of the list in ascending and/or descending order
h. Search an Element on a Linked List
#include <iostream>
#include <string>
using namespace std;
struct SthdentInfo {
int ID;
string name;
char sex;
string college;
string dept;
int classYear;
int semester;
int section;
float cgpa;
SthdentInfo* next;
SthdentInfo* prev;
};
SthdentInfo inputSthdentInfo() {
SthdentInfo s;
cout << "Enter Student Details:\n";
cout << "ID: "; cin >> s.ID;
cout << "Name: "; cin >> s.name;
cout << "Sex: "; cin >> s.sex;
cout << "College: "; cin >> s.college;
cout << "Department: "; cin >> s.dept;
cout << "Class Year: "; cin >> s.classYear;
cout << "Semester: "; cin >> s.semester;
cout << "Section: "; cin >> s.section;
cout << "CGPA: "; cin >> s.cgpa;
s.next = s.prev = nullptr;
return s;
12
}
void insertAtBeginning(SthdentInfo s) {
SthdentInfo* newNode = new SthdentInfo(s);
if (!head) {
head = newNode;
newNode->next = newNode->prev = newNode;
} else {
SthdentInfo* tail = head->prev;
newNode->next = head;
newNode->prev = tail;
head->prev = newNode;
tail->next = newNode;
head = newNode;
}
cout << "Inserted at beginning.\n";
}
void insertAtEnd(SthdentInfo s) {
SthdentInfo* newNode = new SthdentInfo(s);
if (!head) {
head = newNode;
newNode->next = newNode->prev = newNode;
} else {
SthdentInfo* tail = head->prev;
tail->next = newNode;
newNode->prev = tail;
newNode->next = head;
head->prev = newNode;
}
cout << "Inserted at end.\n";
}
void deleteAtBeginning() {
if (!head) {
cout << "List is empty.\n";
return;
}
13
if (head->next == head) {
delete head;
head = nullptr;
} else {
SthdentInfo* tail = head->prev;
SthdentInfo* temp = head;
head = head->next;
head->prev = tail;
tail->next = head;
delete temp;
}
cout << "Deleted from beginning.\n";
}
void deleteAtEnd() {
if (!head) {
cout << "List is empty.\n";
return;
}
if (head->next == head) {
delete head;
head = nullptr;
} else {
SthdentInfo* tail = head->prev;
SthdentInfo* newTail = tail->prev;
newTail->next = head;
head->prev = newTail;
delete tail;
}
cout << "Deleted from end.\n";
}
14
if ((ascending && i->ID > j->ID) || (!ascending && i->ID < j->ID)) {
swap(i->ID, j->ID);
swap(i->name, j->name);
swap(i->sex, j->sex);
swap(i->college, j->college);
swap(i->dept, j->dept);
swap(i->classYear, j->classYear);
swap(i->semester, j->semester);
swap(i->section, j->section);
swap(i->cgpa, j->cgpa);
}
}
}
cout << "List sorted in " << (ascending ? "ascending" : "descending") << " order.\n";
}
void displayAll() {
if (!head) {
cout << "List is empty.\n";
return;
}
SthdentInfo* temp = head;
cout << "\nAll Student Records:\n";
do {
cout << "----------------------------\n";
cout << "ID: " << temp->ID
<< "\nName: " << temp->name
<< "\nSex: " << temp->sex
<< "\nCollege: " << temp->college
<< "\nDepartment: " << temp->dept
<< "\nClass Year: " << temp->classYear
<< "\nSemester: " << temp->semester
<< "\nSection: " << temp->section
<< "\nCGPA: " << temp->cgpa << "\n";
temp = temp->next;
15
} while (temp != head);
}
int main() {
int choice, position, id;
SthdentInfo s;
while (true) {
cout << "\n--- Student Management System (Doubly Circular Linked List) ---\n"
<< "1. Insert Student at the beginning\n"
<< "2. Insert Student at the End\n"
<< "3. Insert Student at the Middle\n"
<< "4. Delete Student at the beginning\n"
<< "5. Delete Student at the End\n"
<< "6. Delete Student at the Middle\n"
<< "7. Sort Student list in ascending order\n"
<< "8. Sort Student list in descending order\n"
<< "9. Search Student by ID\n"
<< "10. Display All\n"
<< "11. Exit\n"
<< "----------------------------------\n"
<< "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
s = inputSthdentInfo();
insertAtBeginning(s);
break;
case 2:
s = inputSthdentInfo();
insertAtEnd(s);
break;
case 3:
s = inputSthdentInfo();
cout << "Enter position: ";
cin >> position;
insertAtMiddle(s, position);
break;
case 4:
deleteAtBeginning();
break;
case 5:
deleteAtEnd();
break;
case 6:
cout << "Enter position: ";
cin >> position;
deleteAtMiddle(position);
break;
case 7:
sortList(true);
break;
case 8:
sortList(false);
break;
case 9:
cout << "Enter ID to search: ";
cin >> id;
16
searchByID(id);
break;
case 10:
displayAll();
break;
case 11:
return 0;
default:
cout << "Invalid choice.\n";
}
}
return 0;
}
4. Briefly discuss the applications of linked list:
a. Singly Linked List
b. Doubly Linked List
c. Circular Linked Lis
There are many applications of linked lists, be it in computer science or the real
world.
In music players, we can create our song playlist and can play a song either from starting or
ending of the list. And these music players are implemented using a linked list.
We watch the photos on our laptops or PCs, and we can simply see
the next or previous images easily. This feature is implemented using a linked list.
You must be reading this article on your web browser, and in web browsers, we open multiple
URLs, and we can easily switch between those URLs using the previous and next buttons
because they are connected using a linked list.
17
A. ) Singly Linked List :
A singly linked list is a linear data structure in which each node contains two
elements: the data and a pointer (or reference) to the next node in the sequence.
The last node in the list points to null, indicating the end of the list.
A doubly linked list is a more flexible version of a singly linked list, where each
node contains three elements: the data, a pointer to the next node, and a pointer
to the previous node. This bidirectional structure allows traversal in both
directions (forward and backward).
The doubly linked list is used to implement data structures like a stack, queue, binary
tree, and hash table.
It is also used in algorithms of LRU (Least Recently used) and MRU(Most Recently Used)
cache.
The undo and redo buttons can be implemented using a doubly-linked list.
The doubly linked list can also be used in the allocation and deallocation of memory.
In a circular linked list, the last node in the list points back to the first node instead
of null, forming a closed loop. Circular linked lists can be singly or doubly linked and
are particularly useful in applications that require continuous cycling through
elements.
18