You are on page 1of 1

Algorithm to Create a One-way linked list

//creation of first node


first -> info =item;
first ->link =NULL;
p = first; // accessing first node
again = ‘y’;
while (again == ‘y’) //creation of other nodes
{
new -> info = item;
new -> link = NULL;
p -> link = new;
p = p -> link;
cout<<”to create another node press ‘y’”;
cin>>again;
}

Insertion Algorithm for One-way linked list


This algorithm inserts a new node at a given location ‘Loc’. i.e. if Loc=1 then the new
node will become first node, if Loc=2 then the new node will become 2nd node and so on.
new -> info=item;
cin>>Loc;
if (Loc==1)
{
new -> link = first;
first = new;
}
else
{
new -> link = Loc -> link;
Loc -> link = new;
}

Deletion Algorithm for One-way linked list


This algorithm deletes a node from a given location ‘Loc’. i.e. if Loc=1 then the first
node will be deleted, if Loc=2 then the 2nd node will be deleted and so on.
cin>>Loc;
if (Loc==1)
first = first ->link;
else
Locp -> link = Loc -> link;
delete Loc;
where ‘Locp’ is the preceding node of ‘Loc’.

Traversing Algorithm for One-way linked list


p = first;
while (p != NULL)
{
cout<<p -> info<<endl;
p = p -> link;
}

You might also like