Link List
Link List
It is a collection of nodes which are interconnected with each other with the help of link part
with each node is called link list. All other data structures except link list and queue are
conceptual data structures.
There are two parts in link list
Data/info part
Link/address part
Structure of Node:
struct node
{
int data;
node * link;
};
Link List Description
The link part of a node not only link the data part but it
points to the complete node.
The address of one node is saved with in the link part of
the previous (already existing) node.
Data part is variable and has memory greater than link
part. Link part always takes two byte in DOS but in
case of NT it takes 4 bytes memory.
Link list size is variable.
Link lists’s last node shows the address of NONE.
if there exists no node in list then that link list is called
NULL.
Nodes can be displayed in structures as well as in classes.
LINK LIST USING CLASS &
STRUCTURE
class clinklist
{
private: struct node
node*plist; {
public: int info;
clinklist() node *link;
{ };
plist==NULL;
}
void add_node_atlast(int x);
void showlist();
void del_node(int x);
};
How to create a link list
void clilnklist::add_node_atlast(int x)
{
node*p;
p = new node;
p->info=x;
p->link=NULL;
if(plist=NULL)
plist=p;
else
{
node*last=plist;
while(last->link!=NULL)
{
last=last->link;
}
last->link=p;
}
}
LINK LIST
void main() void clilnklist::showlist(int x)
{ {
clinklist list; node*p;
list.add_node_atlast(10); p=plist;
list.add_node_atlast(20);
while(p->link!=NULL)
list.add_node_atlast(30);
{
list.add_node_atlast(40);
list.add_node_atlast(50);
cout<<p->info<<endl;
list.add_node_atlast(60); p=p->link;
list.add_node_atlast(70); }
list.showlist(); cout<<p->info;
getch(); }
}
LINK LIST
INSERTION AFTER A SPECIFIC NODE
if(plist==NULL)
{
Different Cases for Insertion cout<<“Insert after in empty link list is not
possible”;
1- insert_after(); }
2- insert_before(); p=plist;
while((p!=NULL)&&(p->info!=x))
Insert after cannot possible {
in case of empty link list p=p->link;
Insert after in case of }
single/multiple nodes is if(p!=NULL)
{
possible q=new node;
Insert after a specific node q->info=y;
that cannot exist in link list is q->link=p->link;
not possible p->link=q;
}
else
cout<<“Required Node does not exist in link
LINK LISTINSERTION BEFORE A SPECIFIC
NODE
void clinklist::insert_before(int x, int y)
{
Different Cases for node *p, *pre, *q;
if(plist->info==x)
Insertion {
q=new node;
Insert before doest not q->info=y;
q->link=plist;
exist in case of empty link plist=q;
return;
list }
}
node exists {
q=new node;
q->info=y;
Insert before 1st and last q->link=p;
pre->link=q;
node exists return;
}
else
{
cout<<“Required Node does not exist”;
}
LINK LIST
Deletion between 1st and last node
Different Cases for Deletion node *p, *pre;
p=plist;
Deletion of node does not while((p!=NULL)&&(p->info!=x))
possible in case of empty link list {
Deletion of 1st one in any link pre=p;
list p=p->link;
p=plist; }
if(plist->info==x) Deletion cannot possible in case, if
{ node does not exist in link list
plist=plist->link;
if(p!=NULL)
}
{
delete p;
pre->link=p->link;
delete p;
}
void clinklist::deletenode(int x)
LINK LIST
{
node *p, *pre;
if(plist==NULL)
{cout<<“deletion cannot be possible”;
return;
}
p=plist; For Deletion of Last Node
if(plist->info==x)
{ The link part of last node always
plist=plist->link; denotes to the Null part
delete p;
return; Do yourself…????????
}
while((p!=NULL)&&(p->info!=x))
{
pre=p;
p=p->link;
}
if(p!=NULL)
{
pre->link=p->link;
delete p;
}
else cout<<“No required node exist in list”:
LINK LIST
void clinklist::deleteafter(int x)
{
node *p, *q; For Deletion of Last Node
if(plist==NULL)
{
cout<<“deletion cannot be possible”; The link part of last node always
return;
} denotes to the Null part
p=plist; Do yourself…????????
while((p!=NULL)&&(p->info!=x))
{
Delete after cannot possible in
p=p->link; case of empty link list
}
if(p==NULL)!!(p->link==NULL)
Delete after cannot possible in
{ case of if desired node don't exist
cout<<“no node exist”;
return;
If there exist single node then no
} deletion is possible
else Last node cannot be deleted
{
node *q=p->link; Between last and 1st node deletion
p->link=q->link;
delete q;
after possible
}
Void clinklist::deletebefore(int x)
{
In empty link list not possible
LINK LIST
In case of 1st node delete before is not possible
Second Node
p=plist ->link;
if(p->info==x)
{
delete plist;
plist=p;
}
Between 2nd and Last Node
pre2=plist;
pre1=p;
p=p->link;
while((p!=NULL)&&(p->info!=x))
{
pre2=pre1;
pre1=p;
p=p->link;
}
if(p!=NULL)
{
pre2->link=pre1->link;
delete pre1;
}
CIRCULAR LINK LIST
Some important points relating to Circular Link List
Show/Display in Circular
Insertion in Circular Link List Link List
p=new node; void circularlist:: show_node()
p->info=x; {
if(plist==NULL) node *p;
{ if(plist==NULL)
plist=p; {
p->link=p; cout<<“Nothing node exists”;
} return;
else }
{ p=plist;
p->link=plist->link; do
plist->link=p; {
plist=p; p=p->link;
} cout<<p->info<<endl;
}
while(p!=plist);
}
CIRCULAR LINK LIST
class circular_list
{
private:
node *plist; Cases of Deletion in
public: Circular Link List
circular_list() Case 1: if there is no node in circular
link list, then no deletion is possible
{ Case 2: if there exists only single
plist=NULL; node, then deletion is possible
Case 1
if (plist==NULL)
{
cout<<“Deletion is not possible in CLL”; Cases of Deletion in
}
Case 2 Circular Link List
if ((plist->info==x)&&(plist->link==plist)) Case 1: if there is no node in circular
{ link list, then no deletion is possible
delete plist; Case 2: if there exists only single
plist=NULL; node, then deletion is possible
} Case 3: In the case of very first node,
Case 3:
possible
struct node *p;
p=plist->link; Case 4: Deletion between 1st and last
if(p->info==x) node is also possible
{ Case 5: if the deletion node is very
plist->link=p->link; last node, then deletion also possible
delete p;
}
CIRCULAR LINK LIST
Case 4
while((p->info!=x)&&(p!=plist))
{
pre==p;
p=p->link;
}
if((p==plist)&&(p->info==x))
{
pre->link=plist->link;
plist=pre;
delete p;
}
else
if(p!=plist)
{
pre->link=p->link;
delete p;
}
else
cout<<“No required node exists in CLL”;
DOUBLE LINK LIST
Class DLL
class DLL_list
{
node *plist;
public: Move_last()
DLL_list() {
{ while(plist->right!=NULL)
plist==NULL; {
} plist=plist->right;
void addnode_atlast(int x); }
void shownode_list(); }
Move_first();
Move_last();
};
Assignments