You are on page 1of 20

ENGG1002 Computer Programming and Applications

Prepared by Dr. K.K.Y. Wong


Listed Lists
 Linked lists
 A fundamental data structure
 Consists of a sequence of nodes, each containing an arbitrary number
of data fields and one or more references (links) to other nodes
 Allow insertion and removal of nodes at any point in the list in constant
time
 Do NOT allow random access

data data data data ×


A linked list
a reference to a node
another node
2
Linked Lists in C++
 A linked list can be implemented in C++ using pointers and
structures (or classes)
 A node is defined using structure, with one or more member
variables being pointer variables pointing to the same
structure type, e.g.,
struct Node {
string title;
int year;
Node* next;
}

typedef Node* NodePtr;

3
Building a Linked List
 In order to access a linked list, a pointer variable often called
head is used to store a pointer to the first node of the list,
e.g.,
NodePtr head;

data data data …


head

 Initially when the linked list is empty (i.e., a linked list with no
nodes), head will simply contain a null pointer
head = NULL;

NULL
head
4
Building a Linked List
 Starting from an empty list, new nodes may be created and
inserted into the linked list
data NULL
head

 A null pointer is assigned the pointer variable of the last node


to indicate the end of the linked list
 Similarly, nodes may be removed from the linked list and
destroyed
 Both node insertion and removal may take place at the head
or any other position of the linked list

5
Inserting a Node at the Head
 To insert a node at the head
1. Create a new node using the new operator
data data data data NULL
head

2. Fill in the members (data) of this node using the arrow operator
data data data data NULL
head
data

6
Inserting a Node at the Head
 To insert a node at the head (continue)
3. Copy the head pointer to the next member of this new node
data data data data NULL
head
data

4. Replace the head pointer by a pointer to this new node


data data data data NULL
head
data

7
Inserting a Node at the Head
 Example:
NodePtr head_insert(NodePtr &head, string title, int year)
{
NodePtr pNode = new Node;

pNode->title = title;
pNode->year = year;

pNode->next = head;
head = pNode;

return head;
}

8
Searching a Linked List
 To search a linked list
 Copy the head pointer to a pointer variable current
 While current is not a null pointer do
 Check the data pointed to by current against the input value
 If the input value is found return current

 otherwise replace the current pointer by the next member pointed to by


current
 If the input value is not found after traversing the whole list, return a
null pointer

9
Searching a Linked List
 Example:
NodePtr search_title(NodePtr head, string title)
{
NodePtr current = head;
while (current != NULL)
{
if (current->title == title)
return current;
else
current = current->next;
}

return NULL;
}

10
Inserting a Node
 To insert a node after a node pointed to by a pointer p
1. Create a node using the new operator
2. Fill in the members (data) of this node using the arrow operator
3. Copy the next member pointed to by p to the next member of this
new node

data data data data NULL


head
data
p
4. Replace the next member pointed to by p by a pointer to this new
node
data data data data NULL
head
data
p
11
Inserting a Node
 Example:
NodePtr insert(NodePtr prev, string title, int year)
{
NodePtr pNode = new Node;

pNode->title = title;
pNode->year = year;

pNode->next = prev->next;
prev->next = pNode;

return pNode;
}

12
Removing a Node
 To remove a node pointed to by a pointer q
1. Obtain a pointer p pointing to the node before the node to be
removed (i.e., p->next should be equal to q)
data data data data NULL
head

p q

2. Copy the next member pointed to by q to the next member pointed


to by p
data data data data NULL
head

p q
3. Destroy the node pointed to by q using delete operator
13
Removing a Node
 Example:
bool remove(NodePtr p, NodePtr &q)
{
if (p->next != q)
return false;

p->next = q->next;
delete q;
q = NULL;

return true;
}

14
Removing a Node at the Head
 To remove a node pointed to by the head pointer
1. Copy the head pointer to a temporary pointer variable q
data data data data NULL
head

2. Replace the head pointer by the next member pointed to by the


head pointer

data data data data NULL


head

q
3. Destroy the node pointed to by q using delete operator

15
Removing a Node at the Head
 Example:
bool head_remove(NodePtr &head)
{
if (head == NULL)
return false;

NodePtr q = head;
head = head->next;
delete q;

return true;
}

16
A Simple Linked List Example
 Example:
#include <iostream> int main()
using namespace std; {
int n = 0;
struct Node { cout << "Please input the number of data: " << endl;
int v; cin >> n;
Node* next; NodePtr head = NULL;
}; for(int i = 0; i < n; i++)
typedef Node* NodePtr; {
Node tmp = {0, NULL};
NodePtr print(NodePtr head); cout << "Data[" << i <<"]:";
NodePtr insertion_sort(NodePtr &head); cin >> tmp.v;
NodePtr find_prev(NodePtr head, NodePtr pNode); head_insert(head, &tmp);
NodePtr head_insert(NodePtr &head, }
const NodePtr pNode); print(head);
NodePtr insert(NodePtr prev, NodePtr pNode); insertion_sort(head);
NodePtr remove_all(NodePtr &head); print(head);
return 0;
}

17
A Simple Linked List Example
NodePtr print(NodePtr head) NodePtr insert(NodePtr prev, NodePtr pNode)
{ {
cout << "Content of the linked list: " << endl; NodePtr pNewNode = new Node;
NodePtr current = head; *pNewNode = *pNode;
while (current != NULL) pNewNode->next = prev->next;
{ prev->next = pNewNode;
cout << current->v << " "; return pNewNode;
current = current-> next; }
}
cout << endl << endl; NodePtr remove_all(NodePtr &head)
return head; {
} NodePtr current = head;
while (current != NULL)
NodePtr head_insert(NodePtr &head, {
const NodePtr pNode) NodePtr pNode = current;
{ current = current->next;
NodePtr pNewNode = new Node; delete pNode;
*pNewNode = *pNode; }
pNewNode->next = head; head = NULL;
head = pNewNode; return head;
return head; }
}

18
A Simple Linked List Example
NodePtr find_prev(NodePtr head, NodePtr pNode) NodePtr insertion_sort(NodePtr &head)
{ {
NodePtr current = head; NodePtr sorted_head = NULL;
NodePtr prev = NULL; NodePtr current = head;
while (current != NULL) while (current != NULL)
{ // matching criterion {
if (current->v < pNode->v) NodePtr prev = find_prev(sorted_head, current);
{ if (prev == NULL)
prev = current; head_insert(sorted_head, current);
current = current->next; else
} insert(prev, current);
else current = current->next;
break; }
} remove_all(head);
return prev; head = sorted_head;
} sorted_head = NULL;
return head;
}

19
Other Types of Linked Lists
 Doubly-Linked List

× data data data data ×

 Circularly-Linked List

data data data data

20

You might also like