DS4 LinkedList
DS4 LinkedList
a linked list is a linear data structure that allows the users to store
data in non-contiguous memory locations. A linked list is defined
as a collection of nodes where each node consists of two members
which represents its value and a Link pointer which stores the
address for the next node. In this article, we will learn about the
linked list, its implementation, and its practical applications.
Note: the down arrow in the last node indicates that this link field is
NULL. -the next pointer of the last node contains a special value,
called the NULL pointer, which does not point to any address of the
node. that is NULL pointer indicates the end of the linked list.
fig3: Linked list and values of the links
struct node
{
int data ; //Instead of „data‟ we also use „info‟
node *link ; // Instead of „Next‟ we also use „Link‟
};
Current 2800
Current −> info 92
Current −> link 1500
Current −> link −> info 63
head−> link −>link 1500
head−>link−>link−>info 63
head−>link−>link−>link 3600
head−>link−>link−>link−>info 45
current−> link −>link 3600
Example: the following code outputs the data stored in each node:
current = head ;
while( current != NULL)
{ cout<< current−> info << ” “ ;
current = current −> link ; }
struct node
{ int info;
node * link ; };
node *head , *p , *q ,*newNode;
newNode = new ; // create new
newNode −> info = 50 ; // store 50 in the new node
After the second statement ( that is , p −> link = newNode ;), the
resulting list is as show in figure 10 .
Fig 10: Linked list after the statement p−> link = newNode
Deletion
consider the Linked list shown in figure 12 .
We need pointer to this node (with info 34). the following statement
delete the node from the list and deallocate the memory occupied by
this node :
q = p −> link ;
p −> link = q −> link ;
delete q ;
Fig 13: list after the statement q = p −> link
Operation Description
Deleting the node present at the second position in the linked list
#include <iostream>
using namespace std;
if (position == 1) {
insertAtBeginning(value);
return;
}
if (head->link==NULL)
{
delete head;
head = NULL;
return;
}
if (position == 1)
{
deleteFromBeginning();
return;
}
Node* temp = head;
for (int i = 1; i < position - 1 && temp !=NULL; ++i)
{
temp = temp->link;
}
if ((temp==NULL) || (temp->link==NULL))
{
cout << "Position out of range." << endl;
return;
}
// Save the node to be deleted
Node* nodeToDelete = temp->link;
// Update the link pointer
temp->link = temp->link->link;
// Delete the node
delete nodeToDelete;
}
// Function to print the nodes value of the linked list
void display()
{
if (head==NULL)
{
cout << "List is empty." << endl;
return;
}
int main()
{
int x,p,k;
do
{
cout<<"=================================="<<endl;
cout<<"1) Insert element in the Begining"<<endl;
cout<<"2) Insert element in the End"<<endl;
cout<<"3) Insert element in the Position"<<endl;
cout<<"4) Delete element from the Begining"<<endl;
cout<<"5) Delete element from the End"<<endl;
cout<<"6) Delete element from the Position"<<endl;
cout<<"7) Display elements of the List"<<endl;
cout<<"0) Exit"<<endl;
cout<<"Enter your choice : ";
cin>>k;
switch (k)
{
case 1:{
cout<<"Enter value :";
cin>>x;
insertAtBeginning(x);
cout << "Linked list after insertions: ";
display();
break;
}
case 2:{
cout<<"Enter value :";
cin>>x;
insertAtEnd(x);
cout << "Linked list after insertions: ";
display();
break;
}
case 3:{
cout<<"Enter value :";
cin>>x;
cout<<"Enter position to insert :";
cin>>p;
insertAtPosition(x, p);
cout << "Linked list after insertions: ";
display();
break;
}
case 4:{
deleteFromBeginning();
cout << "Linked list after deleting : ";
display();
break;
}
case 5:{
deleteFromEnd();
cout << "Linked list after deleting : ";
display();
break;
}
case 6:{
cout<<"Enter position to delete :";
cin>>p;
deleteFromPosition(p);
cout << "Linked list after deleting : ";
display();
break;
}
case 7: display();
break;
case 8: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while( k != 0);
return 0;
}