0% found this document useful (0 votes)
13 views

Week 02 Lecture 02

The document discusses linked lists and their implementation using pointers in C++. It describes the node class with data and next pointer fields, and the list class containing a head pointer. It provides algorithms for inserting nodes at the start, end or middle of the list by allocating memory for new nodes and adjusting pointer connections between nodes.

Uploaded by

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

Week 02 Lecture 02

The document discusses linked lists and their implementation using pointers in C++. It describes the node class with data and next pointer fields, and the list class containing a head pointer. It provides algorithms for inserting nodes at the start, end or middle of the list by allocating memory for new nodes and adjusting pointer connections between nodes.

Uploaded by

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

Data Structures

Instructor:
Hafiz Tayyeb Javed

7. Linked Lists
Insertion(At Start, At End, At Middle Take
Away), Finding will discuss

7-Link Lists 1
Roadmap

• List as an ADT
• An array-based implementation of lists

• Introduction to linked lists


• A pointer-based implementation in C++
• Variations of linked lists

7-Link Lists 2
Pointers-Based Implementation of Lists
(Linked List)

7-Link Lists 3
Linked List

• Linked list nodes composed of two parts


– Data part
➢ Stores an element of the list
– Next part
➢ Stores link/pointer to next element
➢ Stores Null value, when no next element

7-Link Lists 4
Simple Linked List Class (1)

• We use two classes: Node and List


• Declare Node class for the nodes
– data: double-type data in this example
– next: a pointer to the next node in the list

class Node {
public:
double data; // data
Node* next; // pointer to next
};

7-Link Lists 5
Simple Linked List Class (2)

• 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
class List {
public:
List(void) { head = NULL; } // constructor
~List(void); // destructor

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


bool Insert(int index, double x);
int Find(double x);
int Delete(double x); Cover later
void DisplayList(void); Cover Later
private:
Node* head;
};

7-Link Lists 6
Simple Linked List Class (3)

Operations of List

• IsEmpty: determine whether or not the list is empty

• Insert: insert a new node at a particular position

• Find: find a node with a given value

• Delete: delete a node with a given value Discussed later

• DisplayList: print all the nodes in the list Analyze

7-Link Lists 7
Inserting a New Node Algorithm
• bool Insert(int index, double x)
– Insert a node with data equal to x after the index elements
– If the insertion is successful
➢ Return true
➢ Otherwise, return false
– If index is <= 0 or > length of the list, the insertion will fail

• Steps
1. Locate the element at the index position
2. Allocate memory for the new node, copy data into node
3. Point the new node to its successor (next node)
4. Point the new node’s predecessor (preceding node) to the new node

7-Link Lists 8
Insertion After The Last Element (1)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this

A1 A2 A3

head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 9
Insertion After The Last Element (2)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this

A1 A2 A3

head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 10
Insertion After The Last Element (3)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this

A1 A2 A3

head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 11
Insertion After The Last Element (4)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this

A1 A2 A3 x

head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 12
Insertion After The Last Element (4)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this

A1 A2 A3 x

head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 13
Insertion At The Middle (1)
• Suppose current points to the middle element of the list
– We can add a new item x by doing this

A1 A2 A3

head current

tmp = new Node();


tmp->data= x;
tmp->next = current->next;
current->next = tmp;
7-Link Lists 14
Insertion At The Middle (1)
• Suppose current points to the middle element of the list
– We can add a new item x by doing this

A1 A2 A3

head current
Steps
• Locate the index element
• Allocate memory for the new node
• Copy data into node tmp
• Point the new node to its successor tmp = new Node();
(next node) tmp->data= x;
• Point the new node’s predecessor tmp->next = current->next;
(preceding node) to the new node current->next = tmp;
7-Link Lists 15
Insertion At The Middle (1)
• Suppose current points to the middle element of the list
– We can add a new item x by doing this

A1 A2 A3

head current x
Steps
• Locate the index element
• Allocate memory for the new node
• Copy data into node tmp
• Point the new node to its successor tmp = new Node();
(next node) tmp->data= x;
• Point the new node’s predecessor tmp->next = current->next;
(preceding node) to the new node current->next = tmp;
7-Link Lists 16
Insertion At The Middle (1)
• Suppose current points to the middle element of the list
– We can add a new item x by doing this

A1 A2 A3

head current x
Steps
• Locate the index element
• Allocate memory for the new node
• Copy data into node
tmp
• Point the new node to its successor tmp = new Node();
(next node) tmp->data= x;
• Point the new node’s predecessor tmp->next = current->next;
(preceding node) to the new node current->next = tmp;
7-Link Lists 17
Insertion At The Middle (1)
• Suppose current points to the middle element of the list
– We can add a new item x by doing this

A1 A2 A3

head current x
Steps
• Locate the index element
• Allocate memory for the new node
• Copy data into node
tmp
• Point the new node to its successor tmp = new Node();
(next node) tmp->data= x;
• Point the new node’s predecessor tmp->next = current->next;
(preceding node) to the new node current->next = tmp;
7-Link Lists 18
Inserting a New Node (2)
• Possible cases of Insert
1. Insert into an empty list
2. Insert at front
3. Insert at back
4. Insert in middle

• In fact, only need to handle two cases


– Insert as the first node (Case 1 and Case 2)
– Insert in the middle or at the end of the list (Case 3 and Case 4)

– Before moving to Case 3, let’s discuss Display of Linked List

7-Link Lists 19
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;
}

7-Link Lists 20
Inserting a New Node (3)
bool List::Insert(int index, double x) {
if (index <= 0) return false;

int currIndex = 2;
Node* currNode = head;
while (currNode && index > currIndex) { Try to locate index’th node.
currNode = currNode->next; If it doesn’t exist, return
currIndex++; false
}
if (index > 1 && currNode == NULL) return false;

Node* newNode = new Node;


newNode->data = x;
if (index == 1) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return true;
} 7-Link Lists 21
Inserting a New Node (3)
bool List::Insert(int index, double x) {
if (index <= 0) return false;

int currIndex = 2;
Node* currNode = head;
while (currNode && index > currIndex) { Try to locate index’th node.
currNode = currNode->next; If it doesn’t exist, return
currIndex++; false
}
if (index > 1 && currNode == NULL) return false;

Node* newNode = new Node;


Create a new node
newNode->data = x;
if (index == 1) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return true;
} 7-Link Lists 22
Inserting a New Node (3)
bool List::Insert(int index, double x) {
if (index <= 0) return false;

int currIndex = 2;
Node* currNode = head;
while (currNode && index > currIndex) { Try to locate index’th node.
currNode = currNode->next; If it doesn’t exist, return
currIndex++; false
}
if (index > 1 && currNode == NULL) return false;

Node* newNode = new Node;


Create a new node
newNode->data = x;
if (index == 1) {
newNode->next = head;
head = newNode; Insert as first element
} head
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return true;
}
newNode
7-Link Lists 23
Inserting a New Node (3)
bool List::Insert(int index, double x) {
if (index <= 0) return false;

int currIndex = 2;
Node* currNode = head;
while (currNode && index > currIndex) { Try to locate index’th node.
currNode = currNode->next; If it doesn’t exist, return
currIndex++; false
}
if (index > 1 && currNode == NULL) return false;

Node* newNode = new Node;


Create a new node
newNode->data = x;
if (index == 1) {
newNode->next = head;
head = newNode;
} Insert after currNode
else { currNode
newNode->next = currNode->next;
currNode->next = newNode;
}
return true;
} 7-Link Lists 24
newNode
Finding a Node
• int Find(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

int List::Find(double x) {
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode) return currIndex;

return 0;
}

7-Link Lists 25
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;
}

7-Link Lists 26
Any Question So Far?

7-Link Lists 27

You might also like