CS162 - 4A
TABLE OF CONTENTS
Introduction - Singly Ordered Linked List
Last time, we learn about Singly Linked List
and how to implement it. In this lecture, we will extend that to Singly Ordered Linked List
.
A Singly Ordered Linked List
is a Singly Linked List
with it’s value sorted in ascending order. Given a singly ordered linked list, named listA
, you’re asked to implement the following functions:
Problem statements
- Insert a new Node
x
into the ordered list and still keep the list in order.void insertOrderedList(Node* &pHead, int x);
- Given anoter orderd linked list, named
listB
, merge the 2 lists into a newlistC
in whick its values are also sorted in order.void mergeOderedLists(Node* &pHA, Node* &pHB, Node* &pHC);
E.g.
Problem 1
Student’s code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void insertOrderedList(Node* &pHead, int x) {
Node* cur = pHead;
while(cur!=nullptr && cur->pNext!=nullptr) {
if(pHead->pNext->data > x) {
Node* tmp = new Node;
tmp->data = x;
tmp->pNext = cur->pNext;
cur->pNext = tmp;
return;
}
cur = cur->pNext;
}
Node* tmp = new Node;
tmp->data = x;
tmp->pNext = cur->pNext;
cur->pNext = tmp;
}
What’s wrong?
- In
line 4
he misusedpHead
withcur
. - What if we have to insert a node into the first position?
Fixed code
void insertOrderedList(Node* &pHead, int x) {
if(pHead == nullptr || pHead->data > x) {
Node* tmp = new Node;
tmp->data = x;
tmp->pNext = pHead;
pHead = tmp;
return;
}
Node* cur = pHead;
while(cur->pNext!=nullptr) {
if(cur->pNext->data > x) {
Node* tmp = new Node;
tmp->data = x;
tmp->pNext = cur->pNext;
cur->pNext = tmp;
return;
}
cur = cur->pNext;
}
Node* tmp = new Node;
tmp->data = x;
tmp->pNext = cur->pNext;
cur->pNext = tmp;
}
Problem 2
Student’s code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void mergeOderedLists(Node* &pHA, Node* &pHB, Node* &pHC) {
pHC = new Node;
Node *cur = pHC;
while(pHA && pHB) {
if(pHA->data < pHB->data) {
cur->pNext = pHA;
pHA = pHA->pNext;
} else {
cur->pNext = pHB;
pHB = pHB->pNext;
}
cur = cur->pNext;
}
if(pHA) {
cur->pNext = pHA;
} else {
cur->pNext = pHB;
}
pHC = pHC->pNext;
}
Discussion
- Didn’t ensure the nullity of
pHA
orpHB
in the if statement atline 16
. - Forgot to delete the dummy node.
Fixed code
void mergeOderedLists(Node* &pHA, Node* &pHB, Node* &pHC) {
pHC = new Node; //create a dummy node
Node *cur = pHC;
while(pHA && pHB) {
if(pHA->data < pHB->data) {
cur->pNext = pHA;
pHA = pHA->pNext;
} else {
cur->pNext = pHB;
pHB = pHB->pNext;
}
cur = cur->pNext;
}
if(pHA) {
cur->pNext = pHA;
pHA = nullptr;
} else {
cur->pNext = pHB;
pHB = nullptr;
}
Node *tmp = pHC;
pHC = pHC->pNext;
delete tmp; //delete the dummy
}
My comment: "Very eleganto!"
Double linked list
struct DNode {
int data;
DNode* pPrev, *pNext;
};
With the new member - pointer DNode
, the list created base on these nodes are called Doubly Linked List
. Its name explained a lots about how it’ll be used, it point to the previous node in the list.
Problem statements
- Insert
x
at the beginning of a DLL.void insertAtBeginning(DNode* &pHead, int x);
- Insert
x
beforek
in a DLL.void insertBeforeK(DNode* &pHead, int x, int k);
Problem 1
Teacher’s code
void insertAtBeginning(DNode* &pHead, int x) {
DNode* tmp = new DNode;
tmp->data = x;
tmp->pNext = pHead;
tmp->pPrev = nullptr;
if(pHead) pHead->pPrev = tmp;
pHead = tmp;
}