Professional Documents
Culture Documents
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;
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
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
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
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
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
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
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
Circularly-Linked List
20