Docsity

Link List data structure using c++, Exercises of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structure using C++
Single Link List
Inserting at Head
Inserting at End
Inserting at Specific Location
Deleting first node
Deleting last node
Deletion at specific location
Bubble Sorting of list
Searching a value in list
Display or Traverse through nodes
Counting no. of nodes in list
Find Middle node
Swap two nodes
Creating Student Record List
Include Data of 5 Students
Double link list
Inserting at Head
Inserting at End
Inserting at Specific Location
Deleting first node
Deleting last node
Deletion at specific location
Counting no. of nodes in the list
Display Nodes Traversing forward direction
Display Nodes in backward direction
Circular Link List
Insertion and Deletion
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Link List data structure using c++ and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Data Structure using C++

 Single Link List

 Inserting at Head

 Inserting at End

 Inserting at Specific Location

 Deleting first node

 Deleting last node

 Deletion at specific location

 Bubble Sorting of list

 Searching a value in list

 Display or Traverse through nodes

 Counting no. of nodes in list

 Find Middle node

 Swap two nodes

 Creating Student Record List

 Include Data of 5 Students

 Double link list

 Inserting at Head

 Inserting at End

 Inserting at Specific Location

 Deleting first node

 Deleting last node

 Deletion at specific location

 Counting no. of nodes in the list

 Display Nodes Traversing forward direction

 Display Nodes in backward direction

 Circular Link List

 Insertion and Deletion

//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;

}

};

// Function to insert data at the

beginning of the linked list

void insertAtStart(node*& start, int val) {

node* n = new node(val); n->link = start;

start = n;

}

// Function to insert data at the end

of the linked list

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;

}

// Function to insert data at a

specific position in the linked list

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;

}

// Bubble Sort to sort the linked list

in ascending order

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; }

}

// Function to search for a value in

the linked list

node* searchValue(node* start, int val) { node* current = start;

while (current != NULL) { if (current->data == val) {

return current; }

current = current->link; }

return NULL; }

// Function to display the data of all

nodes by traversing

void display(node* start) {

node* current = start; while (current != NULL) {

cout << current->data << "->"; current = current->link;

} cout << "NULL" << endl;

}

// Function to count the total

number of nodes in the linked list

int countNodes(node* start) {

int count = 0;

node* temp = start; while (temp != NULL) {

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

return count;

}

//Implementation in Main Function

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;

}

//Swap Two Nodes

#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; }

};

//Inserting data by using end insertion

technique

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;

//Display

void display(node* head) { node* temp = head;

while (temp != NULL) { cout << "\nRoll number: " << temp-

rollNumber << "\nName: " << temp->name << "\n------------------" << endl;

temp = temp->next; }

}

//Implementation

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;

}

};

// Function to insert data at the

beginning of the doubly linked list

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; }

// Function to insert data at the end

of the doubly linked list

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; }

// Function to insert data at a

specific location in the doubly linked

list

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;

}

// Function to delete a node from a

specific location in the doubly linked

list

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;

}

// Function to count nodes in double

link list

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; }

// Function to display the data of all

nodes in the doubly linked list by

traversing forward direction

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;

}

// Function to display the data of all

nodes in the doubly linked list by

traversing backward direction

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; }

//Implementation in Main Function

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;

}

//Insert at the 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;

}

//Delete From Head

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;

}

//Delete from End

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;

}

//Implementation of Main Function

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;

}