Linked List
1. Overview
Today’s topic is Linked List. The code structure would be: class Node, class LinkedList, definitions of functions of LinkedList, and Main() function.
Pointer was mentioned at the first blog. Pointer as a feature of C++, it reflects a programmer’s profession. While the linked list is greatly relevant to the pointer, interviewer often ask relevant questions to the job-hunter. For example, reverse a linked list, sort a linked list, remove an element from linked list etc.
2. What Node looks like
Usually, node of linked list has value, and next pointer. If it is doubly linked list, it will also contains the previous pointer. This time, we discuss the singly linked list.
class Node{
public:
int value;
Node * next;
// default constructor
Node() { next = nullptr; }
// constructor
Node(int i ) {
value = i;
next = nullptr;
}
};
3. LinkedList
As we defined the node of linked list, linked list will have a head of that. In this class, besides the fundamental function(makeList, printList), it also includes reverse(), sort(), removeOne().
class LinkedList {
public:
Node* head;
LinkedList(){
// In order c++ version, some may use NULL
// But it's not recommended.
head = nullptr;
};
// create a linked list of m nodes with values randomly distributed in 0, ..., n-1
void makeList(int m, int n);
void printList();
void reverse();
void sort();
void removeOne(int k);
};
4. makeList(), printList()
// define the function outside the class
// To make complier know whose function is being defined,
// note to add the class name before the function name
// class name:: function name
void LinkedList::makeList(int m, int n){
for(int i = 0; i < m; i++){
Node* p = new Node(rand() % n);
p->next = head;
head = p;
}
}
void LinkedList:: printList(){
Node *p = head;
cout << endl;
while(p){
cout << p->value << " ";
p = p->next;
}
}
5. Reverse a Linked List
// classic algorithm problem, and job interview question
void LinkedList:: reverse(){
// return if it has only 0 or 1 node
if(!head || !head->next)
return;
Node* p1 = head, * p2, * p3;
p2 = p1->next;
while(p2){
p3 = p2->next;
p2->next = p1;
if(p1 == head)
p1->next = nullptr;
p1 = p2;
p2 = p3;
}
head = p1;
}
6. Sort the Linked List
// selection sort
void LinkedList:: sort(){
// if 0 or 1 nodem return;
if(!head || !head->next)
return;
Node * p1 = head, *p2;
int min;
Node* p_min;
while(p1){
min = p1->value;
p_min = p1;
p2 = p1->next;
while(p2) {
if(p2->value < min){
min = p2->value;
p_min = p2;
}
p2 = p2->next;
}
p_min->value = p1->value;
p1->value = min;
p1 = p1->next;
}
}
7. Remove a Target Element From Linked List
void LinkedList:: removeOne(int k){
// if 0 or 1 nodem return;
if(!head || !head->next)
return;
Node *p1 = head, * p2;
if(head->value == k){
head = head->next;
// avoid memory leak, important
delete p1;
return;
}
p2 = p1->next;
while(p2){
if(p2->value == k){
p1->next = p2->next;
delete p2;
return;
}
p1 = p2;
p2 = p2->next;
}
}
8. Complete Code:
#include<iostream>
using namespace std;
class Node{
public:
int value;
Node * next;
// default constructor
Node() { next = nullptr; }
// constructor
Node(int i ) {
value = i;
next = nullptr;
}
};
class LinkedList {
public:
Node* head;
LinkedList(){
// In order c++ version, some may use NULL
// But it's not recommended.
head = nullptr;
};
// create a linked list of m nodes with values randomly distributed in 0, ..., n-1
void makeList(int m, int n);
void printList();
void reverse();
void sort();
void removeOne(int k);
};
// define the function outside the class
// To make complier know whose function is being defined,
// note to add the class name before the function name
// class name:: function name
void LinkedList::makeList(int m, int n){
for(int i = 0; i < m; i++){
Node* p = new Node(rand() % n);
p->next = head;
head = p;
}
}
void LinkedList:: printList(){
Node *p = head;
cout << endl;
while(p){
cout << p->value << " ";
p = p->next;
}
}
// classic algorithm problem, and job interview question
void LinkedList:: reverse(){
// return if it has only 0 or 1 node
if(!head || !head->next)
return;
Node* p1 = head, * p2, * p3;
p2 = p1->next;
while(p2){
p3 = p2->next;
p2->next = p1;
if(p1 == head)
p1->next = nullptr;
p1 = p2;
p2 = p3;
}
head = p1;
}
// selection sort
void LinkedList:: sort(){
// if 0 or 1 nodem return;
if(!head || !head->next)
return;
Node * p1 = head, *p2;
int min;
Node* p_min;
while(p1){
min = p1->value;
p_min = p1;
p2 = p1->next;
while(p2) {
if(p2->value < min){
min = p2->value;
p_min = p2;
}
p2 = p2->next;
}
p_min->value = p1->value;
p1->value = min;
p1 = p1->next;
}
}
void LinkedList:: removeOne(int k){
// if 0 or 1 nodem return;
if(!head || !head->next)
return;
Node *p1 = head, * p2;
if(head->value == k){
head = head->next;
// avoid memory leak, important
delete p1;
return;
}
p2 = p1->next;
while(p2){
if(p2->value == k){
p1->next = p2->next;
delete p2;
return;
}
p1 = p2;
p2 = p2->next;
}
}
int main(){
// *, ., ->
// Three * p = new Three
// (*p).a = 5 ===== p->5
LinkedList L1;
L1.makeList(10,15);
L1.printList();
L1.reverse();
L1.printList();
L1.sort();
L1.printList();
return 0;
}