









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










//Implementation of all
Operations in single link
list
#include
using namespace std;
class node {
public:
int data; // stores node data node* link; // stores reference of next node using link
// constructor (blue-print for all nodes) node(int val) {
data = val; link = NULL;
}
};
void insertAtStart(node*& start, int val) {
node* n = new node(val); n->link = start;
start = n;
}
void insertAtEnd(node*& start, int val) {
node* n = new node(val); if (start == NULL) {
start = n; return;
} node* temp = start;
while (temp->link != NULL) { temp = temp->link;
} temp->link = n;
}
void insertAtPosition(node*& start, int val, int position) {
if (position <= 0) { cout << "Invalid position. Insertion not possible." << endl; return;
}
if (position == 1) { insertAtStart(start, val); return;
}
return;
} node* current = start;
for (int i = 1; i < position - 1 && current != NULL; i++) {
current = current->link; }
if (current == NULL || current->link == NULL) { cout << "Invalid position. Deletion not possible." << endl;
return;
} node* to_del = current->link;
current->link = current->link->link; delete to_del;
}
void bubbleSort(node*& start) {
int size = 0;
node* temp = start; while (temp != NULL) {
size++; temp = temp->link;
} if (size <= 1) {
return; // No need to sort if the list is empty or has only one node
}
node* current;
node* previous = NULL;
for (int pass = 0; pass < size - 1; pass++) { current = start;
while (current->link != previous) { if (current->data > current->link->data) {
int tempData = current->data; current->data = current->link->data;
current->link->data = tempData; }
current = current->link; }
previous = current; }
}
node* searchValue(node* start, int val) { node* current = start;
while (current != NULL) { if (current->data == val) {
return current; }
current = current->link; }
return NULL; }
void display(node* start) {
node* current = start; while (current != NULL) {
cout << current->data << "->"; current = current->link;
} cout << "NULL" << endl;
}
int countNodes(node* start) {
int count = 0;
node* temp = start; while (temp != NULL) {
count++; temp = temp->link; }
return count;
}
int main() {
node* start = NULL; int option, val, position;
while (true) {
cout << "\nEnter Option:" << endl;
cout << "1. Insert at Beginning" << endl;
cout << "2. Insert at End" << endl; cout << "3. Insert at Specific Location" << endl; cout << "4. Delete from Beginning" << endl;
cout << "5. Delete from End" << endl; cout << "6. Delete from Specific Location" << endl; cout << "7. Sort the List" << endl;
cout << "8. Search Value" << endl; cout << "9. Display Data of Nodes" << endl;
cout << "10. Count Nodes of the List" << endl;
cout << "-1. Exit" << endl;
cin >> option; switch (option) {
case 1: cout << "Enter value to insert at the beginning: "; cin >> val;
insertAtStart(start, val); break;
case 2: cout << "Enter value to insert at the end: "; cin >> val;
insertAtEnd(start, val); break;
case 3:
// constructor (blue-print for all nodes )
node(int val) { data = val;
next = NULL; }
};
// inserting data at the end
void insertAtEnd(node*& start, int val) {
node* n = new node(val);
if (start == NULL) { start = n;
return; }
node* temp = start; while (temp->next != NULL) {
temp = temp->next; }
temp->next = n;
}
node* middle(node* head) {
int length = 0;
node* current = head;
// Calculate the length of the linked list while (current != NULL) {
length++; current = current->next;
}
// Find the middle index
int middleIndex = length / 2; current = head;
// Traverse to the middle node for (int i = 0; i < middleIndex; i++) {
current = current->next; }
return current; }
//display void display(node* start) {
node* temp = start; while (temp != NULL) {
cout << temp->data << "->"; temp = temp->next;
} cout << "NULL" << endl;
} // implementation
int main() { node* start = NULL;
insertAtEnd(start, 1); insertAtEnd(start, 2);
insertAtEnd(start, 3); insertAtEnd(start, 4);
cout << "Displaying nodes after insertion at the end: ";
display(start); // middle of node
node* middleNode = middle(start);
if (middleNode != NULL) { cout << "Middle node data: " << middleNode->data << endl;
} else {
cout << "Linked list is empty." << endl; }
return 0;
}
#include
using namespace std;
class node {
public:
int data; node* next;
node(int val) {
data = val; next = NULL;
}
};
//Inserting Data into nodes
void insertAtEnd(node*& start, int val) {
node* n = new node(val); if (start == NULL) {
start = n;
return; }
node* temp = start; while (temp->next != NULL) {
temp = temp->next; }
temp->next = n; }
//Swaping nodes void swapNodes(node*& start, int x, int y) {
if (x == y) { return;
} node* prevX = NULL;
node* prevY = NULL; node* currX = start;
node* currY = start; // Find nodes with values x and y and their previous nodes while (currX != NULL && currX->data != x) {
prevX = currX; currX = currX->next;
} while (currY != NULL && currY->data != y) {
prevY = currY; currY = currY->next;
}
//Student Record List
#include
using namespace std;
class node {
public:
int rollNumber; //stores ID field
string name; //stores name field node* next; //stores reference
//Constructor
node(int roll, string n) {
rollNumber = roll; name = n;
next = NULL; }
};
void insertData(node* &head, int roll, string name) {
node* n = new node(roll, name); if (head == NULL) {
head = n; return;
} node* temp = head;
while (temp->next != NULL) { temp = temp->next;
} temp->next = n;
void display(node* head) { node* temp = head;
while (temp != NULL) { cout << "\nRoll number: " << temp-
rollNumber << "\nName: " << temp->name << "\n------------------" << endl;
temp = temp->next; }
}
int main() { node* head = NULL;
for (int i = 1; i <= 5; i++) { cout << "Enter record#" << i << endl;
int roll; string name;
cout << "Roll number: "; cin >> roll;
cout << "Name: "; cin.ignore(); // To clear the buffer before reading the name getline(cin, name);
insertData(head, roll, name); }
display(head); return 0; }
//Implementation of all
Operations in double link
list
#include
using namespace std;
class node {
public:
int data; // stores node data
node* next; // stores reference of next node node* prev; // stores reference of previous node
node(int val) { data = val;
next = NULL; prev = NULL;
}
};
void insertAtBeginning(node*& start, int val) {
node* n = new node(val); if (start == NULL) {
start = n; return;
n->next = start; start->prev = n;
start = n; }
void insertAtEnd(node*& start, int val) {
node* n = new node(val); if (start == NULL) {
start = n; return;
} node* temp = start;
while (temp->next != NULL) { temp = temp->next;
} temp->next = n;
n->prev = temp; }
void insertAtSpecificLocation(node*& start, int val, int position) { if (position <= 0) {
cout << "Invalid position. Insertion not possible." << endl;
deleteAtBeginning(start);
return; }
node* current = start;
while (current->next != NULL) { current = current->next;
}
current->prev->next = NULL; delete current;
}
void deleteAtSpecificLocation(node*& start, int position) {
if (start == NULL) {
cout << "Linked list is empty. Deletion not possible." << endl;
return; }
if (position == 1) {
deleteAtBeginning(start); return;
}
node* current = start;
int count = 1;
while (current != NULL && count < position) { current = current->next;
count++; }
if (current == NULL) {
cout << "Invalid position. Deletion not possible." << endl;
return; }
current->prev->next = current->next;
if (current->next != NULL) { current->next->prev = current->prev;
} delete current;
}
void count_node_to_display(node* start) { node* current = start;
int count = 0; while (current != NULL) {
count++; current = current->next;
}
cout << "Total number of nodes in the doubly linked list: " << count << endl; }
void displayForward(node* start) {
cout << "Data in doubly linked list (Forward direction): ";
node* current = start; while (current != NULL) {
cout << current->data << " <-> "; current = current->next;
} cout << "NULL" << endl;
}
void displayBackward(node* start) {
cout << "Data in doubly linked list (Backward direction): ";
node* current = start;
while (current->next != NULL) { current = current->next;
} while (current != NULL) {
cout << current->data << " <-> "; current = current->prev;
cout << "NULL" << endl; }
int main() {
node* start = NULL;
// Insert at the beginning insertAtBeginning(start, 3);
insertAtBeginning(start, 2); insertAtBeginning(start, 1);
// Insert at the end
insertAtEnd(start, 4); insertAtEnd(start, 5);
// Insert at specific location (position 3)
insertAtSpecificLocation(start, 6, 3);
// Display data in forward and backward direction
displayForward(start); displayBackward(start);
// Delete at the beginning
deleteAtBeginning(start);
// Delete at the end deleteAtEnd(start);
n->next = head;
}
void insert_at_Head(node* &head, int val) {
node* n = new node(val); if (head == NULL) {
head = n; n->next = head;
return; }
node* temp = head; while (temp->next != head) {
temp = temp->next; }
temp->next = n; n->next = head;
head = n;
}
void delete_from_Head(node* &head) {
if (head == NULL) {
cout << "List is empty. Cannot delete from head." << endl;
return; }
node* temp = head;
while (temp->next != head) {
temp = temp->next; }
node* toDelete = head;
head = head->next; temp->next = head;
delete toDelete;
}
void delete_from_End(node* &head) {
if (head == NULL) { cout << "List is empty. Cannot delete from end." << endl; return;
}
node* temp = head; while (temp->next->next != head) {
temp = temp->next; }
node* toDelete = temp->next;
temp->next = head;
delete toDelete; }
void display(node* head) {
if (head == NULL) {
cout << "List is empty." << endl; return;
} node* temp = head;
do { cout << temp->data << " ";
temp = temp->next; } while (temp != head);
cout << endl;
}
int main() {
node* head = NULL; //Inserting at end
insert_at_End(head, 1); insert_at_End(head, 2);
insert_at_End(head, 3); display(head);
//Inserting at head insert_at_Head(head, 5);
display(head); //Deletion at head
delete_from_Head(head); display(head);
// Deletion at end delete_from_End(head);
display(head);
return 0;
}