You are on page 1of 26

Linked Lists

Week 03
Linked Lists / Slide 2

List Overview
 Linked lists
 Abstract data type (ADT)
 Basic operations of linked lists
 Insert, find, delete, print, etc.
 Variations of linked lists
 Circular linked lists
 Doubly linked lists
Linked Lists / Slide 3

Linked Lists
A B C D 

Head
 A linked list is a series of connected nodes
 Each node contains at least
 A piece of data (any type)
 Pointer to the next node in the list
 Head: pointer to the first node node
 The last node points to NULL A

data pointer
Linked Lists / Slide 4

Linked List
 Tail Node: Points to the last element of the list
 In case of only one element in the list the
TAIL and HEAD will both point towards the
same node.
Linked Lists / Slide 5

A Simple Linked List Class


 We use two classes: Node and List
 Declare Node class for the nodes
 data: int-type data in this example
 next: a pointer to the next node in the list

class Node {
public:
int data; // data
Node* next; // pointer to next
};
Linked Lists / Slide 6

A Simple Linked List Class


 Declare List, which contains
 head: a pointer to the first node in the list.
Since the list is empty initially, head is set to NULL
 Operations on List
class List {
public:
List(void) { head = NULL; } // constructor
~List(void); // destructor

bool IsEmpty() { return head == NULL; }


void add_node(int x); // insert at the tail
void front(int x); // insert at the beginning
int FindNode(int x);
int DeleteNode(int x);
void DisplayList(void);
private:
Node* head, *tail;
};
Linked Lists / Slide 7

A Simple Linked List Class


 Operations of List
 IsEmpty: determine whether or not the list is empty
 InsertNode:
Insert a new node at the tail
Insertion at the beginning of the list
insert a new node after a given value
 FindNode: find a node with a given value
 DeleteNode: delete a node with a given value
 DisplayList: print all the nodes in the list
Linked Lists / Slide 8

Inserting a New Node


 Steps to add a node at the tail
1. Create a new node and initialize it with the
given data
2. Check if the list is empty, then insert at the
head
3. Else insert at the tail
Linked Lists / Slide 9

Insert a New Node at the Tail


Linked Lists / Slide 10

Insert a New Node at the


Beginning
 The steps to be followed are as follows:
1. Make a new node
2. Point the ‘next’ of the new node to the ‘head’
of the linked list.
3. Mark new node as ‘head’.
Linked Lists / Slide 11

Insert a New Node at the


Beginning
Linked Lists / Slide 12

Insert a New Node at the


Beginning
Linked Lists / Slide 13

Insert a Node in between


Linked Lists / Slide 14

Insert a Node in between


 The steps for inserting a node after node ‘a’
(as shown in the picture) are:
 Make a new node
 Point the ‘next’ of the new node to the node ‘b’
(the node after which we have to insert the
new node). Till now, two nodes are pointing
the same node ‘b’, the node ‘a’ and the new
node.
Linked Lists / Slide 15
Linked Lists / Slide 16

Finding a node
 int FindNode(double x)
 Search for a node with the value equal to x in the list.
 If such a node is found, return its position. Otherwise, return
0.

Node* List::FindNode(double x) {
Node* currNode = head;
while (currNode->data != x) {
currNode = currNode->next;
}
return currNode;
}
Linked Lists / Slide 17

Deleting a node
 We delete any node of a linked list by connecting
the predecessor node of the node to be deleted by the
successor node of the same node.
 For example, if we have a linked list a → b → c, then to delete
the node ‘b’, we will connect ‘a’ to ‘c’ i.e., a → c.
 But this will make the node ‘b’ inaccessible and this type of
inaccessible nodes are called garbage and we need to clean this
garbage.
 We do this cleaning by the use of ‘delete’ operator.
Linked Lists / Slide 18

Steps for Deleting a node


 Suppose we need to delete node B in the
linked list A->B->C
 Search the previous node of the node to be
deleted, i-e. A in this case
 Create a temporary pointer to the node ‘B’.
 Connect node ‘A’ to ‘C’.
 Delete the node ‘B’.
Linked Lists / Slide 19

Deleting a Node
Linked Lists / Slide 20

Printing all the elements


 void DisplayList(void)
 Print the data of all the elements
 Print the number of the nodes in the list
void List::DisplayList()
{
int num = 0;
Node* currNode = head;
while (currNode != NULL){
cout << currNode->data << endl;
currNode = currNode->next;
num++;
}
cout << "Number of nodes in the list: " << num << endl;
}
Linked Lists / Slide 21

Destroying the list


 ~List(void)
 Use the destructor to release all the memory used by the list.
 Step through the list and delete each node one by one.

List::~List(void) {
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
// destroy the current node
delete currNode;
currNode = nextNode;
}
}
Linked Lists / Slide 22

Concatenating two lists


 Concatenating means joining together
 For example consider the two lists:
 A->B->C and W->X->Y->Z
 The concatenation of these two lists will be:
 A->B->C->W->X->Y->Z

 In order to concatenate two lists, we need to


point the Tail of first list to the head of second
list
Linked Lists / Slide 23

Concatenating two lists

//a and b are pointers to head of the two lists


static void concatenate(node *a, node *b){
node *temp = a;
while(temp->next!=NULL)
temp = temp->next;
temp->next=b;
}
Linked Lists / Slide 24

Variations of Linked Lists


 Circular linked lists
 The last node points to the first node of the list

A B C

Head

 How do we know when we have finished traversing


the list? (Tip: check if the pointer of the current
node is equal to the head.)
Linked Lists / Slide 25

Variations of Linked Lists


 Doubly linked lists
 Each node points to not only successor but the
predecessor
 There are two NULL: at the first and last nodes in the
list
 Advantage: given a node, it is easy to visit its
predecessor. Convenient to traverse lists backwards

 A B C 

Head
Linked Lists / Slide 26

Array versus Linked Lists


 Linked lists are more complex to code and manage
than arrays, but they have some distinct advantages.
 Dynamic: a linked list can easily grow and shrink in size.
We don’t need to know how many nodes will be in the list. They
are created in memory as needed.
In contrast, the size of a C++ array is fixed at compilation time.
 Easy and fast insertions and deletions
To insert or delete an element in an array, we need to copy to
temporary variables to make room for new elements or close the
gap caused by deleted elements.
With a linked list, no need to move other nodes. Only need to
reset some pointers.

You might also like