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

Data Structures and Algorithms - #8

The document provides an overview of linked lists, detailing their types, structures, and operations such as creation, insertion, deletion, and traversal. It includes code snippets in C++ for implementing a single linked list, demonstrating how to append nodes, display the list, count nodes, and delete nodes. Key concepts include the use of pointers for node linking and the significance of the head pointer in managing the list.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structures and Algorithms - #8

The document provides an overview of linked lists, detailing their types, structures, and operations such as creation, insertion, deletion, and traversal. It includes code snippets in C++ for implementing a single linked list, demonstrating how to append nodes, display the list, count nodes, and delete nodes. Key concepts include the use of pointers for node linking and the significance of the head pointer in managing the list.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

DATA STRUCTURES AND ALGORITHMS Lab #8

LINKED LIST
Type of Linked Lists Depending on the requirements the pointer are maintained, and accordingly
the linked list can be classified into three major groups: single linked list, circular linked list and
double linked list. Single Linked List A single linked list is a linear collection of data elements.
The elements in a single linked list can be visited starting from beginning and towards its end i.e.
only in one direction. Therefore the single linked list is also called one way list or one way chain.
Each node in a single linked list consists of at least two fields. The first field contains the data or
value called data field. The second field contains the pointer or link to the next node in the list.
This field is called the linked field or pointer field. The linked field of the last node contains a
NULL value. A NULL value I the pointer field indicated that pointer does not point to any other
node.
The variable head contains the memory address of the first node of the list. The linked list is accessed with reference to
this pointer variable. If head has a NULL value it means that the list is empty. The last node in the list contains a NULL
value in its pointer field. If a new item is to be added at the end of the linked list, then a memory address is assigned to
the pointer field of the last node and a new item is added. In a single linked list each node has only the address of the
next node in the link. It does not contain the address of the previous node. Thus it can only be visited in one direction i.e.
starting from the beginning towards the end of the list.
Creating, Storing and Traversing a Linked List
#include<iostream.h>
struct node { int data;
node *link; };
node* link is a pointer to the next node in the linked list.
node *head, *current, *temp;
•head: Points to the first node of the linked list (initially NULL).
•current: Used for traversing the list.
•temp: Temporarily holds a newly created node.
•void append(int n) {
• if (head == NULL) {
• // List is empty; create the first node.
• head = new node;
• cout << "list created" << endl;
• head->data = n; // Set data for the first node.
• head->link = NULL; // No other nodes, so link is NULL.
• cout << "Data inserted in the first node: " << n << endl;
• } else {
• // List already exists; add a new node at the end.
• current = head;
• // Traverse to the last node.
• while (current->link != NULL)
• current = current->link;
• // Create a new node and add it at the end.
• temp = new node;
• cout << "New node inserted at the end of the list" << endl;
• temp->data = n; // Set data for the new node.
• temp->link = NULL; // The new node points to NULL.
• current->link = temp; // The previous last node links to the new node.
• cout << "Data inserted at the end of the list: " << n << endl;
• }
•}
void display_list() {
cout << "Following are the elements in the linked list: " <<
endl;
current = head;
// Traverse the list until the last node.
while (current->link != NULL) {
cout << current->data << endl; // Print current node
data.
current = current->link; // Move to the next node.
. }
// Print the data of the last node.
cout << current->data << endl;
}

main() {
append(1); // Adds 1 to the list (first node).
append(2); // Adds 2 to the list (second node).
display_list(); // Displays all the elements in the list.
}
#include<iostream.h> Count Number of Nodes in Linked List
struct node {
int data;
node *link;
};
node *head, *current, *temp;
int count() {
int c = 0; // Counter initialized to 0.
current = head; // Start traversal from the head of the list.

// Traverse the list until the last node.


while (current->link != NULL) {
c++; // Increment the counter for each node.
. current = current->link; // Move to the next node.
}

return c + 1; // Add 1 to include the last node.


}

•It starts from the head node.


•The loop continues until current->link == NULL (end of the list).
•Inside the loop:
•c is incremented for each node encountered.
•current is updated to the next node (current = current->link).
•After the loop, c + 1 accounts for the last node, which has a NULL link.
main() {
cout << "Number of nodes in the list are: " << count() <<
endl;
}
Insertion in Linked List A new
node can be inserted in a linked
. list at any of these locations:
At the end of the list
At the beginning of the list
At specified location in the list
#include<iostream.h> Insertion at the End of the List
struct node {
int data;
node *link;
};
node *head, *current, *temp;
if (head == NULL) {
head = new node; // Create the first node.
cout << "list created" << endl;
head->data = n; // Assign the data value.
.
head->link = NULL; // Set the link to NULL since it's the only node.
cout << "Data inserted in the first node: " << n << endl;
}
•A new node is created, and head points to it.
•The data field of the node is set to n.
•The link is set to NULL, indicating the end of the list.

else { current=head;
//go to end of list
while(current->link!=NULL)
current=current->link; //create and add a new node
temp = new node;
cout<<"New node inserted at the end of the list"<data=n;
temp->link=NULL;
current->link=temp;
cout<<"Data inserted at the end of the list: "<<n<
.
.
•If the list has only one node (head->link == NULL), set head = NULL.
•Otherwise, set prev->link = NULL to disconnect the last node.
•Redundant Line: prev->link = NULL; is repeated unnecessarily.

Deletion in Linked List A node can


be deleted in a linked list at any of
these locations:
At the end of the list
At the beginning of the list
At specified location in the list
struct node {
int data;
node *link;
};
node *head, *ptr, *prev, *current, *temp;
int item;
int d;
cout << "Enter a value to store in linked list";
cin >> d;
Input d: The user is prompted to enter a value to initialize the list
head = new node;
cout << "New list created" << endl;
head->data = d;
head->link = NULL;
cout << "Item inserted: " << d << endl;
•A new node is allocated in memory and assigned to head.
•data is set to the input value d.
•link is set to NULL, indicating the end of the list
.
if (head == NULL) {
cout << "Link List is empty. It must have at least one node. Creating Link List." << endl;
create_list();
}
•This check is unnecessary because the function already creates a node with the user's input.
•If the list is empty (head == NULL), this code calls create_list() recursively, leading to unnecessary reinitialization.
ptr = head;
prev = head;
while (ptr->link != NULL) {
prev = ptr;
ptr = ptr->link;
•If the list is not empty, ptr is used to traverse to the last node.
•prev keeps track of the node before ptr.

item = ptr->data;
if (head->link == NULL)
head = NULL;
else
prev->link = NULL;
prev->link = NULL;
•Store Last Node Data: The value of the last node (ptr->data) is stored in item.
•Remove Node Logic:
•If the list has only one node (head->link == NULL), set head = NULL.
•Otherwise, set prev->link = NULL to disconnect the last node.
•Redundant Line: prev->link = NULL; is repeated unnecessarily.

.
.
.
.
.

You might also like