04 Linked List - Insert & Delete Operations
04 Linked List - Insert & Delete Operations
Array Limitations
What are the limitations of an array, as a data
structure?
1. Fixed size
2. Physically stored in consecutive memory locations
3. To insert or delete items, may need to shift data
List Overview
4-7
Linked List Operations
class Node {
public:
int info; // data
Node* next; // pointer to next
};
A Simple Linked List Class
Declare List, which contains
– Operations on List
class List {
public:
List(void) {head = NULL;} //
constructor
~List(void); // destructor
private:
Node* head;
};
A Simple Linked List Class
Operations of List
– IsEmpty: determine whether or not the list is empty
1. New node should be connected to the first node, which means the
head. This can be achieved by assigning the address of node to
the head.
front
node
1. Make the new node point to the first node (i.e.
the node that front points to)
front
node
2. Make front point to the new node (i.e the
node that node points to)
front
Algorithm
In this case, we don’t disturb the head and tail nodes. Rather, a new
node is inserted between two consecutive nodes. So, these two nodes
should be accessible by our code. We call one node as current and the
other as previous, and the new node is placed between them. Now the
new node can be inserted between the previous and current node by
just performing two steps:
1. Pass the address of the new node in the next field of the previous
node.
2. Pass the address of the current node in the next field of the new
node.
front
current
2. Make the new node point to the node after the
insertion point (i.e. the node pointed to by the node
node that current points to)
front
current
node
3. Make the node pointed to by current point to
the new node
front
X
current
Algorithm
void insert_position(int pos, int val)
{ node *pre;
node *cur;
node *temp=new node;
temp->data=val;
cur=head;
for(int i=1;i<pos;i++)
{ pre=cur; cur=cur->next; }
pre->next=temp;
temp->next=cur; }
Algorithm
void insert_specificValue(int sp_val, int data)
{ node *pre;
node *cur;
node *temp=new node;
temp->data=data;
cur=head;
while (cur->data!= sp_val)
{ pre=cur; cur=cur->next; }
temp->next=cur;
cur->next=temp;
}
Deleting a Node from a Linked List
We will consider three cases and then see how deletion is done in each case.
UNDERFLOW.
Underflow is a condition that occurs when we try to delete a node from a linked
list that is empty. This happens when Head = NULL or when there are no more
nodes to delete.
Note that when we delete a node from a linked list, we actually have to free the
memory occupied by that node. The memory is returned to the free pool so that it
can be used to store other programs and data.
Deleting the First Node from a Linked List
To delete a node from the beginning of the list, then the following changes
will be done in the linked list
Step 1, check if the linked list exists or not. If Head = NULL, then it
signifies that there are no nodes in the list and the control is transferred
to the last statement of the algorithm. (UNDERFLOW)
Step 2: However, if there are nodes in the linked list, then we use a pointer
variable PTR that is set to point to the first node of the list.
For this, we initialize PTR with Head that stores the address of the first node
of the list.
Step 3, Head is made to point to the next node in sequence and finally the
memory occupied by the node pointed by PTR (initially the first node of the
list) is freed and returned to the free pool.
Head
Head
Head
if (head==NULL)
cout<<“Underflow<<endl;
else
node *ptr;
ptr = head;
head=headnext;
delete ptr;
Deleting the Last Node from a Linked List
To delete the last node from the linked list, then the following changes
will be done
Step 1: check if the linked list exists or not. If head = NULL, then it
signifies that there are no nodes in the list and the control is
transferred to the last statement of the algorithm.
Step 2: take a pointer variable PTR and initialize it with head. That
is, PTR now points to the first node of the linked list. In the while
loop, we take another pointer variable PREPTR such that it always
points to one node before the PTR. Once we reach the last node and
the second last node, we set the NEXT pointer of the second last node
to NULL, so that it now becomes the (new) last node of the linked list.
The memory of the previous last node is freed and returned back to
the free pool.
Deleting the Specific Node in a Linked List
Then the following changes will be done in the linked list: