Introduction:
This C++ code demonstrates various operations on a singly linked list. A linked list is a data structure composed of nodes, each containing data and a reference to the next node in the sequence. The code covers operations like inserting nodes at the end, start, and middle of the linked list, searching for a value, and deleting nodes.
#include
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
node *head;
node *temp;
node *last;
last=0;
int choice;
do
{
temp=new node();
cout<<"Enter values in a node:"<<endl; cin>>temp->data;
if(last==0)
{
head=last=temp;
}
else
{
last->next=temp;
last=temp;
}
cout<<"Enter your choice:"<<endl; cin>>choice;
}while(choice==1);
last->next=0;
temp=head;
while(temp!=0)
{
cout<data<<" "; temp=temp->next;
}
/************insertion at the start of the node***************************/
do{
temp=new node();
cout<<"Enter value at the start of the node: "<<endl; cin>>temp->data;
temp->next=head;
head=temp;
cout<<"Enter your choice: "<<endl; cin>>choice;
}while(choice==1);
temp=head;
while(temp!=0)
{
cout<data<<" "; temp=temp->next;
}
/************** iNSERTION AT MIDDLE OF THE LINKLIST***********/
node *temp2;
int location=0;
cout<<"\n Enter your location"<<endl; cin>>location;
temp=new node();
cout<<"\n Enter data in node: "<<endl; cin>>temp->data;
temp2=head;
while(location-1>0)
{
location--;
temp2=temp2->next;
}
temp->next=temp2->next;
temp2->next=temp;
temp=head;
while(temp!=0)
{
cout<data<<" "; temp=temp->next;
}
/**************************Search traverse*************/
int value;
int count=1;
int flag=0;
cout<<"\n Enter value you want to search in LinkList: "<<endl; cin>>value;
temp=head;
while(temp!=0)
{
if(temp->data==value)
{
cout<<"Data found successfully at index "<<count<<endl; flag=1; break; } else { temp=temp->next;
count++;
}
}
if(flag==0)
{
cout<<"Data not found"<<endl;
}
/**************************deleting node******************/
int b=0;
int item;
node *temp3;
cout<<"which data do you want to delete?"<<endl; cin>>item;
temp=head;
while(temp->next->next!=0)
{
if(temp->next->data==item)
{
cout<<"Data found successfully"<<endl; b=1; break; } else { temp=temp->next;
temp3=temp->next;
}
}
if(b==0)
{
cout<<"Data not found"<<endl; } else { temp->next=temp->next->next;
delete temp3;
}
temp=head;
while(temp!=0)
{
cout<data<<" "; temp=temp->next;
}
return 0;
}
Steps:
- Node Structure Definition: A
nodestructure is defined to hold an integerdataand a pointernextto the next node. - Main Function:
head,temp, andlastare declared as pointers to nodes.lastis initialized to0(null).choiceis declared to control the execution of the main loop.
- Main Loop for Inserting at the End:
- A loop (
do-while) allows the user to input values for nodes. - A new node
tempis created, and its data is input. - If
lastis null, the new node becomes theheadandlast. - Otherwise, the new node is attached to the
lastnode’snextpointer, andlastis updated. - The user is prompted to continue or exit.
- A loop (
- Display Linked List:
- The loop is exited when the user chooses to stop.
last->nextis set to null to terminate the linked list.- A new pointer
tempis set tohead. - A loop traverses the linked list and prints the data of each node.
- Insertion at the Start:
- Another loop (
do-while) allows the user to insert nodes at the start. - Similar to end insertion, a new node
tempis created and its data is input. - The new node
nextpoints to the current, andheadis updated to the new node. - The user is prompted to continue or exit.
- Another loop (
- Display Linked List after Start Insertion:
- The loop is exited when the user chooses to stop.
- The updated linked list is displayed using the same traversal mechanism.
- Insertion in the Middle:
- The user inputs the
locationwhere they want to insert the node. - A new node
tempis created with the given data. - A pointer
temp2is set tohead. - A loop advances
temp2to the node before the desired location. - The new node’s
nextis set totemp2‘s originalnext, andtemp2‘snextpoints to the new node.
- The user inputs the
- Display Linked List after Middle Insertion:
- The updated linked list is displayed again.
- Search Operation:
- The user inputs a
valueto search for in the linked list. - A loop traverses the linked list, counting the nodes.
- If the desired value is found, its index is displayed and
flagis set. - If not found, an appropriate message is displayed.
- The user inputs a
- Deletion Operation:
- User inputs an
itemto be deleted from the linked list. - A loop searches for the item.
- If found, the preceding node’s
nextpointer is adjusted to skip the node to be deleted. - The memory of the deleted node is released using
delete.
- User inputs an
- Display Linked List after Deletion:
- The updated linked list is displayed again.
Conclusion:
This C++ code illustrates the implementation of basic singly linked list operations, including insertion at the end, start, and middle, searching for a value, and deleting nodes. It provides an example of managing dynamic data structures using pointers and dynamic memory allocation.
FAQs:
- What is a linked list? A linked list is a data structure consisting of a sequence of elements called nodes, where each node contains data and a reference (or pointer) to the next node.
- Why is dynamic memory used in linked lists? Linked lists allow for efficient dynamic memory allocation, enabling the creation and deletion of nodes as needed, which is especially useful when the size of the list changes frequently.
- What’s the purpose of the
lastpointer? Thelastpointer keeps track of the last node in the linked list. This facilitates efficient insertion at the end without traversing the entire list each time. - Why is the first loop structured as a
do-whileloop? Thedo-whileloop ensures that the insertion process runs at least once, allowing the creation of the initial node. Subsequent insertions are controlled by the user’s choice. - Why is deletion handled differently from insertion? Deletion requires locating the node to be deleted and adjusting pointers in the previous node. It involves memory deallocation, which is not required during insertion.
