Data Structures and Algorithms - #8
Data Structures and Algorithms - #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.
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.
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.
.
.
.
.
.