Linked Lists
Linked Lists
Example: 0x29cf44
– Accessing the value pointed to by the pointer (by the value of the
pointer)
Cout<<*ptr;
Output: 10
Basics of Pointers
double* myPtr;
double a=3.14;
myPtr=&a; //addressof(a)
myPtr *myPtr
0x29cf44 3.14
Basics of Pointers
• What are pointers for?
• Properties of lists
• Position of elements
• Length of the list
• Beginning of the list
• End of the list
• Sorted
• Unsorted
Lists (array implementation)
• An array is a kind of list where the elements are accessed by
– identifier
– index
?
Linked list
• Creating the first object (element of the list)
• The last line sets the pointer from this node to the next to
NULL
Linked list
Abel
24
1.7
temp->nxt=NULL
start=NULL start?
• After creating the first list element, the start pointer will point
to this node (temp)
start 25
1.65
temp1
nxt
start 29
temp2->nxt=start; 1.70
NULL
nxt
start=temp2;
struct list{ int main() {
int age; list *temp1=new list;
float height; temp1->age=29;
list *nxt; temp1->height=1.70;
}; temp1->nxt=NULL;
list *start = NULL; start=temp1;
NULL data
data data data data
*nxt
*nxt *nxt *nxt *nxt
temp2->nxt=temp;
temp->nxt=NULL;
temp2 temp
start
NULL
data data data data data
*nxt *nxt *nxt *nxt *nxt
Insertion
• After specified number of nodes
– The first step is to navigate through the specified number of
nodes with a temporary node variable, temp1
node temp1=new node;
temp1=start;
cout<<“Enter node number:”;
cin>>node_number;
for(int i=1;i<node_number;i++) {
temp1 = temp1->next; // go to the next node
if( temp1 == NULL ) {
cout<<node_number<<" node is not exist"<< endl;
break;
}
}
start NULL
data data data data
NULL
data data data data
temp->next=temp1->next;
temp1->next=temp;
Navigation
• There may be a need to go through all items in the list for
some kind of operation
• Consider the structure of employee with data members
Name, Age Salary
• Example:
• Observe age groups in the list
• Observe salary distribution
next
data next data next data
NULL
Deletion
• From the end
– The last node`s next of the linked list always point to NULL.
– So, we will track last node and previous node of the last
node in the linked list
ND
head data data data
data
Inserting a node to a circular Linked List
• Inserting node at the end
• Step
1. Create a temporary pointer temp and assign head to
temp
node *temp=new node;
temp=head;
2. Navigate through the list until last node is found
while(temp->next!=head)
temp=temp->next;
3. Create a new node to be inserted and load data field
node *ND=new node;
ND->age=20;
ND->height=1.75;
Inserting a node to a circular Linked List
• Inserting node at the end
data
head data data data
Inserting a node to a circular Linked List
P data N
Doubly linked lists
current
28
prev 1.72 next NULL
NULL
Inserting a node to a Doubly Linked List
• Inserting node at the beginning
• Steps
1. Declare a temporary pointer and move it to current
node *temp=new node;
temp=current;
NULL
data data data
NULL prev prev prev
next next next
node *temp=new node;
temp=current;
temp2 NULL
data data data
temp2->prev=NULL;
temp2->next = temp;
temp->prev = temp2;
Inserting a node to a Doubly Linked List
• Inserting node at the end
• Steps
1. Declare a temporary pointer and move it to current
node *temp=new node;
temp=current;
temp=current;
temp2
temp = current temp temp
temp2->next=NULL;
temp2->prev = temp;
temp->next = temp2;
Inserting a node to a Doubly Linked List
• Inserting after a specified node
• Steps
1. Declare a temporary pointer CR
CR = head;
ND->next = CR->next;
CR->next->prev = ND;
CR->next = ND;
ND->prev = CR;
Deleting a node from a doubly Linked List
• Deleting a node from front
NULL
data data data
NULL prev prev prev
next next next
Deleting a node from a doubly Linked List
• Deleting a node from back(end)