0% found this document useful (0 votes)
18 views20 pages

Link List

The document describes a linked list data structure. A linked list is a collection of nodes that are connected to each other via links, with each node containing a data element and a pointer to the next node. It explains that each node has two parts - a data part that stores the data and a link part that stores the address of the next node. The last node's link points to null to indicate the end of the list. Linked lists can be implemented using classes and structures in C++.

Uploaded by

Naeem Atif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views20 pages

Link List

The document describes a linked list data structure. A linked list is a collection of nodes that are connected to each other via links, with each node containing a data element and a pointer to the next node. It explains that each node has two parts - a data part that stores the data and a link part that stores the address of the next node. The last node's link points to null to indicate the end of the list. Linked lists can be implemented using classes and structures in C++.

Uploaded by

Naeem Atif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

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

Description about link list:


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 }
}

Insert before does not p=plist;


exist, if required does not while((p!=NULL)&&(p->info!=x))
{
exist pre=p;
p=p->link;
Insert before in case of 1st }
if(p!=NULL)

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

Circular link list is the extension of Single Link List


In SLL, the link/address part of last node is NULL
In SLL, plist/Header always points to the first node, but in
case of CLL, plist/Header can point to any node.
In CLL, the link part of last node point to the first node of
CLL.
It means in CLL, there does not exist NULL value in
link/address part of any node
 in CLL, we can access last and first node at a time.
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 3: In the case of very first node,


possible
void add_node(int x); Case 4: Deletion between 1st and last
void show_list(int x); node is also possible

void delete_node(); Case 5: if the deletion node is very


last node, then deletion also possible
};
CIRCULAR LINK LIST

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

In SLL/CLL, the pointers can move only in forward direction


In DLL, pointers can move in both directions i.e. in forward direction as
well as in backward direction
Plist pointer in DLL, can point to any node
Left and Right pointers are internal pointers
Node structure in DLL is different from node structure of SLL/CLL
Formation or Shape in Memory of DLL
struct node
{
node *left;
int info;
node *right;
};
DOUBLE LINK LIST

INSERTION in DLL SHOW NODE in DLL

p=new node; while(plist->left!=NULL)


p->info=x; {
if(plist==NULL) plist=plist->left;
{ }
plist=p; while(plist->right!=NULL)
plist->left=plist->right=NULL; {
} cout<<plist->info;
else plist=plist->right;
{ }
plist->right=p; cout<<plist->info;
p->left=plist;
p->right=NULL
plist=p;
}
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

 Delete 2nd last node from the link list


 Delete all even nodes of link list
 Delete all odd nodes of the link list
 Delete all nodes from the link list whose info
is 11

You might also like