Linked List
Linked Lists
 Collection of components (nodes)
 Node components
 Data: stores relevant information
 Link: stores address
 Every node (except last)
 Contains address of the next node
3
FIGURE 5-1 Structure of a node
4
Linked Lists (cont’d.)
 Head (first)
 Address of the first node in the list
 Arrow points to node address
 Stored in node
 Down arrow in last node indicates NULL link field
FIGURE 5-2 Linked list
FIGURE 5-3 Linked list and values of the links
 The structure shown is called a node
 A linked list is made up of a series of these nodes
 Memory is dynamically allocated to these nodes
 The basic operations performed on a Linked List include
 Traversal
 Insertion
 Deletion
Data Link
Structure and Pointer Declaration
 We can declare a pointer to the node in two ways
struct node {
int data;
node *link;
}*Head; 1st
method
Or
struct node {
int data;
node *link;
};
node *Head; 2nd
method
 Both the statements declare a pointer to the node structure called “Head”
 “Head” pointer will store the address of the first node of the linked list
Adding the first node
 If Head = NULL
 Create a new node using
 Head = new node;
 Use the access operator ”->” to assign values
Head ->data=15;
Head->link=NULL // first and only node, does not point
//towards anything
Adding more nodes
 To add more nodes we need two node pointers
node *q;
node *t;
 Pointer “q” is the loop index variable.
 “q” is used to track the address of the node after which you
insert the new node.
 Pointer “t” is used to refer to the address of the newly created
node.
Traversal
 Traversal can be done using the following loop
for(node *q = Head ; q != NULL ; q = q->link )
cout<<endl<<q->data;
Data
NUL
L
Address: 0xgggg
q=Head = 0xaaaa q=q->link 0xcccc q=q->link 0xdddd
q=q->link 0xgggg
Address: 0xaaaa Address: 0xcccc Address: 0xdddd
Data
0xccc
c
Data
0xdd
dd
Data
0xggg
g
q=q->link NULL
No more nodes to
traverse
Inserting a node in linked list
 New nodes can be inserted in a linked list three ways
a) Append (or Add as Last):
Inserts the new node as the lat node of the Linked List
b) Add as First:
Inserts the new node as the first node of the Linked List
c) Add after:
Inserts the new node after the index you specify
Append
 Inserts the new node at the end of the linked list
 Four things need to be done here
a) Track the address of the last node using traversal and pointer “q”
b) Create a new node using pointer “t”
t=new node;
c) Change the “link address” of node reffered to by pointer “q”.
q->link=t;
d) Change the “link address” of the newly inserted node to “NULL”.
t->link=NULL;
1st
Node 2nd
Node (q Node) New Node
Data Link Data
NUL
L
Data
Link
NUL
L
Code “append”
void linklist::append(int num)
{ node *q,*t;
if( Head == NULL )
{ Head = new node;
Head->data = num;
Head->link = NULL;}
else
{ q = Head;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = num;
t->link = NULL;
q->link = t; }
}
Add as first
 Inserts the new node as the first node of the Linked List
 Three things need to be done
a) Create a new node
q=new node;
b) As the first node is now the 2nd
node of the linked list so we set the “link address” of the
newly created node to “Head”
q->link = Head;
c) In the end we update the address of the “Head pointer” so that it points to the new node
Head=q;
1st
Node 2nd
Node
Data Link Data
NUL
L
NUL
L
New Node
Data
NUL
L
2nd
Node 3rd
Node
Link
Start
Start
Code “add_as_first”
void linklist::add_as_first(int num)
{
node *q;
q = new node;
q->data = num;
q->link = Head;
Head = q;
}
Add after
 Inserts the new node after the index you specify
 Let us suppose we want to insert the new node at index position “1” i.e. as the
second node.
 Four things need to happen
a) Find the address of the node after which you want to insert the address of the new
node using pointer “q” and traversal
b) Create a new node using “t”
t=new node;
c) Update the “link address” of the newly created node “t” and set it equal to the link
address of the node “q”;
d) Update “link address” of node “q” and set it equal to “t”
1st
Node 2nd
Node
Data
0xcde
f
Data
NUL
L
2nd
Node
(new node)
Data
3rd
Node
Start (q)
New
Link
0xcde
f
Address: 0xcdef
Code “add_after”
void linklist::addafter( int c, int num)
{ node *q,*t;
int i;
for(i=0,q=Head;i<c;i++){
q = q->link;
if( q == NULL ){
cout<<"nThere are less than "<<c<<" elements.";
return;
}
}
t = new node;
t->data = num;
t->link = q->link;
q->link = t;
}
Deleting a node in linked list
 We can encounter three cases when deleting a node from the
linked list
a) Deletion of the first:
Inserts the new node as the lat node of the Linked List
b) Deletion of a middle node:
Inserts the new node as the first node of the Linked List
c) Deletion of the last node:
Inserts the new node after the index you specify
Note: Cases (b) and (c) are similar
 During deletion we need the address of two nodes
a) The node we need to delete
b) The node previous to the node we ant to delete
 Thus we require two pointers “q” and “r”
a) Pointer “q” is being used to keep track of the node you want to
delete
b) Pointer “r” is being used to keep track of the node previous to
the node you want to delete.
Note:
For deletion of the first node we do not need the address of
the previous node so “r is not required”
Deletion of the first node
 Deletes the first node of the linked list
 We need to do two things
a) Update the value of “Head”
q=Head;
Head=q->link
b) Delete the first node “q”
2nd
Node 3rd
Node
Data Link Data
NUL
L
NUL
L
1st
Node
Data Link
1st
Node 2nd
Node
Head Head
Deletion at End of the linked list
 The last node of the linked list is deleted
 Three things need to be done when deleting at the end
a) Locate the node which needs to be deleted using traversing and pointer
“q” and “r”
b) Update the “link address” for the previous node
r->link = q->link
c) Delete the last node
delete q;
2nd
Node Last Node
Data Link Data
NUL
L
NUL
L
1st
Node
Data Link
Start rth node qth Node
NUL
L
Deletion in the middle
 Some node between the first and the last node is deleted
 Three things need to be done when deleting at the end
a) Locate the node which needs to be deleted using traversing and pointer
“q” and “r”
b) Update the “link address” for the previous node
r->link = q->link
c) Delete the last node
delete q;
2nd
Node Last Node
Data Link Data
NUL
L
NUL
L
1st
Node
Data Link
Start
(rth node)
qth Node
Link
NUL
L
Code deletion
void linklist::del( int num )
{ node *q,*r;
q = Head;
if( q->data == num )
{ Head = q->link;
delete q;
return;}
r = q;
while( q!=NULL )
{ if( q->data == num )
{ r->link = q->link;
delete q;
return;}
r = q;
q = q->link; }
cout<<"nElement "<<num<<" not Found.";}
Quiz # 1
 Modify the add_after(int c, int num) function such that
 We want to insert the new node after the node containing
the integer “c” in its data field.
 Write an algo to search for value 11 in your link list and
update 11 with 15.
 Write an algo to search for data "college” in your link list and
update “college” with “university”.
Quiz questions
 Add a node Before/after a given node in double linklist
 Code for circular linklist
Assignment 2
 Write code for deletion of double linklist

singlelinkedlistasdfghzxcvbnmqwertyuiopa

  • 1.
  • 3.
    Linked Lists  Collectionof components (nodes)  Node components  Data: stores relevant information  Link: stores address  Every node (except last)  Contains address of the next node 3 FIGURE 5-1 Structure of a node
  • 4.
    4 Linked Lists (cont’d.) Head (first)  Address of the first node in the list  Arrow points to node address  Stored in node  Down arrow in last node indicates NULL link field FIGURE 5-2 Linked list FIGURE 5-3 Linked list and values of the links
  • 5.
     The structureshown is called a node  A linked list is made up of a series of these nodes  Memory is dynamically allocated to these nodes  The basic operations performed on a Linked List include  Traversal  Insertion  Deletion Data Link
  • 6.
    Structure and PointerDeclaration  We can declare a pointer to the node in two ways struct node { int data; node *link; }*Head; 1st method Or struct node { int data; node *link; }; node *Head; 2nd method  Both the statements declare a pointer to the node structure called “Head”  “Head” pointer will store the address of the first node of the linked list
  • 7.
    Adding the firstnode  If Head = NULL  Create a new node using  Head = new node;  Use the access operator ”->” to assign values Head ->data=15; Head->link=NULL // first and only node, does not point //towards anything
  • 8.
    Adding more nodes To add more nodes we need two node pointers node *q; node *t;  Pointer “q” is the loop index variable.  “q” is used to track the address of the node after which you insert the new node.  Pointer “t” is used to refer to the address of the newly created node.
  • 9.
    Traversal  Traversal canbe done using the following loop for(node *q = Head ; q != NULL ; q = q->link ) cout<<endl<<q->data; Data NUL L Address: 0xgggg q=Head = 0xaaaa q=q->link 0xcccc q=q->link 0xdddd q=q->link 0xgggg Address: 0xaaaa Address: 0xcccc Address: 0xdddd Data 0xccc c Data 0xdd dd Data 0xggg g q=q->link NULL No more nodes to traverse
  • 10.
    Inserting a nodein linked list  New nodes can be inserted in a linked list three ways a) Append (or Add as Last): Inserts the new node as the lat node of the Linked List b) Add as First: Inserts the new node as the first node of the Linked List c) Add after: Inserts the new node after the index you specify
  • 11.
    Append  Inserts thenew node at the end of the linked list  Four things need to be done here a) Track the address of the last node using traversal and pointer “q” b) Create a new node using pointer “t” t=new node; c) Change the “link address” of node reffered to by pointer “q”. q->link=t; d) Change the “link address” of the newly inserted node to “NULL”. t->link=NULL; 1st Node 2nd Node (q Node) New Node Data Link Data NUL L Data Link NUL L
  • 12.
    Code “append” void linklist::append(intnum) { node *q,*t; if( Head == NULL ) { Head = new node; Head->data = num; Head->link = NULL;} else { q = Head; while( q->link != NULL ) q = q->link; t = new node; t->data = num; t->link = NULL; q->link = t; } }
  • 13.
    Add as first Inserts the new node as the first node of the Linked List  Three things need to be done a) Create a new node q=new node; b) As the first node is now the 2nd node of the linked list so we set the “link address” of the newly created node to “Head” q->link = Head; c) In the end we update the address of the “Head pointer” so that it points to the new node Head=q; 1st Node 2nd Node Data Link Data NUL L NUL L New Node Data NUL L 2nd Node 3rd Node Link Start Start
  • 14.
    Code “add_as_first” void linklist::add_as_first(intnum) { node *q; q = new node; q->data = num; q->link = Head; Head = q; }
  • 15.
    Add after  Insertsthe new node after the index you specify  Let us suppose we want to insert the new node at index position “1” i.e. as the second node.  Four things need to happen a) Find the address of the node after which you want to insert the address of the new node using pointer “q” and traversal b) Create a new node using “t” t=new node; c) Update the “link address” of the newly created node “t” and set it equal to the link address of the node “q”; d) Update “link address” of node “q” and set it equal to “t” 1st Node 2nd Node Data 0xcde f Data NUL L 2nd Node (new node) Data 3rd Node Start (q) New Link 0xcde f Address: 0xcdef
  • 16.
    Code “add_after” void linklist::addafter(int c, int num) { node *q,*t; int i; for(i=0,q=Head;i<c;i++){ q = q->link; if( q == NULL ){ cout<<"nThere are less than "<<c<<" elements."; return; } } t = new node; t->data = num; t->link = q->link; q->link = t; }
  • 17.
    Deleting a nodein linked list  We can encounter three cases when deleting a node from the linked list a) Deletion of the first: Inserts the new node as the lat node of the Linked List b) Deletion of a middle node: Inserts the new node as the first node of the Linked List c) Deletion of the last node: Inserts the new node after the index you specify Note: Cases (b) and (c) are similar
  • 18.
     During deletionwe need the address of two nodes a) The node we need to delete b) The node previous to the node we ant to delete  Thus we require two pointers “q” and “r” a) Pointer “q” is being used to keep track of the node you want to delete b) Pointer “r” is being used to keep track of the node previous to the node you want to delete. Note: For deletion of the first node we do not need the address of the previous node so “r is not required”
  • 19.
    Deletion of thefirst node  Deletes the first node of the linked list  We need to do two things a) Update the value of “Head” q=Head; Head=q->link b) Delete the first node “q” 2nd Node 3rd Node Data Link Data NUL L NUL L 1st Node Data Link 1st Node 2nd Node Head Head
  • 20.
    Deletion at Endof the linked list  The last node of the linked list is deleted  Three things need to be done when deleting at the end a) Locate the node which needs to be deleted using traversing and pointer “q” and “r” b) Update the “link address” for the previous node r->link = q->link c) Delete the last node delete q; 2nd Node Last Node Data Link Data NUL L NUL L 1st Node Data Link Start rth node qth Node NUL L
  • 21.
    Deletion in themiddle  Some node between the first and the last node is deleted  Three things need to be done when deleting at the end a) Locate the node which needs to be deleted using traversing and pointer “q” and “r” b) Update the “link address” for the previous node r->link = q->link c) Delete the last node delete q; 2nd Node Last Node Data Link Data NUL L NUL L 1st Node Data Link Start (rth node) qth Node Link NUL L
  • 22.
    Code deletion void linklist::del(int num ) { node *q,*r; q = Head; if( q->data == num ) { Head = q->link; delete q; return;} r = q; while( q!=NULL ) { if( q->data == num ) { r->link = q->link; delete q; return;} r = q; q = q->link; } cout<<"nElement "<<num<<" not Found.";}
  • 23.
    Quiz # 1 Modify the add_after(int c, int num) function such that  We want to insert the new node after the node containing the integer “c” in its data field.  Write an algo to search for value 11 in your link list and update 11 with 15.  Write an algo to search for data "college” in your link list and update “college” with “university”.
  • 24.
    Quiz questions  Adda node Before/after a given node in double linklist  Code for circular linklist
  • 25.
    Assignment 2  Writecode for deletion of double linklist