computer notes - c++ code for linked list
DESCRIPTION
Add -we simply insert the new node after the current node. So add is a one-step operation. Remove - remove is also a one-step operationTRANSCRIPT
![Page 2: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/2.jpg)
C++ Code for Linked List
// position current before the first// list elementvoid start() {
lastCurrentNode = headNode;currentNode = headNode;
};
http://ecomputernotes.com
![Page 3: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/3.jpg)
C++ Code for Linked Listvoid remove() { if( currentNode != NULL && currentNode != headNode) {
lastCurrentNode->setNext(currentNode->getNext());
delete currentNode; currentNode = lastCurrentNode->getNext(); size--;
}};
2 6 7 1
headNode
currentNode
size=5
lastcurrentNode
8
http://ecomputernotes.com
![Page 4: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/4.jpg)
C++ Code for Linked Listvoid remove() { if( currentNode != NULL && currentNode != headNode) {
lastCurrentNode->setNext(currentNode->getNext());
delete currentNode; currentNode = lastCurrentNode->getNext(); size--;
}};
2 6 7 1
headNode
currentNode
size=5
lastcurrentNode
8
1
1
http://ecomputernotes.com
![Page 5: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/5.jpg)
C++ Code for Linked Listvoid remove() { if( currentNode != NULL && currentNode != headNode) {
lastCurrentNode->setNext(currentNode->getNext());
delete currentNode; currentNode = lastCurrentNode->getNext(); size--;
}};
2 7 1
headNode
currentNode
size=5
lastcurrentNode
8
1
1
2
2
http://ecomputernotes.com
![Page 6: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/6.jpg)
C++ Code for Linked Listvoid remove() { if( currentNode != NULL && currentNode != headNode) {
lastCurrentNode->setNext(currentNode->getNext());
delete currentNode; currentNode = lastCurrentNode->getNext(); size--;
}};
2 7 1
headNode
currentNode
size=4
lastcurrentNode
8
1
1
2
2
3
3
4
4
http://ecomputernotes.com
![Page 7: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/7.jpg)
C++ Code for Linked List
int length() {
return size; };
private:int size;Node *headNode;Node *currentNode, *lastCurrentNode;
http://ecomputernotes.com
![Page 8: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/8.jpg)
Example of List Usage
#include <iostream>#include <stdlib.h>#include "List.cpp"
int main(int argc, char *argv[]){
List list;
list.add(5); list.add(13); list.add(4);list.add(8); list.add(24); list.add(48); list.add(12);list.start();while (list.next())
cout << "List Element: "<< list.get()<<endl;
} http://ecomputernotes.com
![Page 9: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/9.jpg)
Analysis of Linked List
add• we simply insert the new node after the current
node. So add is a one-step operation.
http://ecomputernotes.com
![Page 10: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/10.jpg)
Analysis of Linked List
add• we simply insert the new node after the current
node. So add is a one-step operation.
remove remove is also a one-step operation
http://ecomputernotes.com
![Page 11: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/11.jpg)
Analysis of Linked List
add• we simply insert the new node after the current
node. So add is a one-step operation.
remove remove is also a one-step operation
find worst-case: may have to search the entire list
http://ecomputernotes.com
![Page 12: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/12.jpg)
Analysis of Linked List
add• we simply insert the new node after the current
node. So add is a one-step operation.
remove remove is also a one-step operation
find worst-case: may have to search the entire list
back moving the current pointer back one node requires
traversing the list from the start until the node whose next pointer points to current node.
![Page 13: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/13.jpg)
Doubly-linked List Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
http://ecomputernotes.com
![Page 14: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/14.jpg)
Doubly-linked List Moving forward in a singly-linked list is easy;
moving backwards is not so easy. To move back one node, we have to start at
the head of the singly-linked list and move forward until the node before the current.
http://ecomputernotes.com
![Page 15: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/15.jpg)
Doubly-linked List Moving forward in a singly-linked list is easy;
moving backwards is not so easy. To move back one node, we have to start at
the head of the singly-linked list and move forward until the node before the current.
To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node:
element nextprev
http://ecomputernotes.com
![Page 16: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/16.jpg)
Doubly-Linked List Nodeclass Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node* getNext() { return nextNode; };void setNext(Node* nextNode)
{ this->nextNode = nextNode; };Node* getPrev() { return prevNode; };void setPrev(Node* prevNode)
{ this->prevNode = prevNode; };private:
int object;Node* nextNode;Node* prevNode;
};
http://ecomputernotes.com
![Page 17: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/17.jpg)
Doubly-linked List
Need to be more careful when adding or removing a node.
Consider add: the order in which pointers are reorganized is important:
size=52 6 8 7 1head
current
http://ecomputernotes.com
![Page 18: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/18.jpg)
Doubly-linked List 1. newNode->setNext( current->getNext() );
size=52 6 8 7head
current
1
9newNode 1
http://ecomputernotes.com
![Page 19: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/19.jpg)
Doubly-linked List 1. newNode->setNext( current->getNext() );2. newNode->setprev( current );
size=52 6 8 7head
current
1
9newNode 1
2
http://ecomputernotes.com
![Page 20: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/20.jpg)
Doubly-linked List 1. newNode->setNext( current->getNext() );2. newNode->setprev( current );3. (current->getNext())->setPrev(newNode);
size=52 6 8 7head
current
1
9newNode 1
2 3
http://ecomputernotes.com
![Page 21: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/21.jpg)
Doubly-linked List 1. newNode->setNext( current->getNext() );2. newNode->setprev( current );3. (current->getNext())->setPrev(newNode);4. current->setNext( newNode );
size=52 6 8 7head
current
1
9newNode 1
2 34
http://ecomputernotes.com
![Page 22: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/22.jpg)
Doubly-linked List 1. newNode->setNext( current->getNext() );2. newNode->setprev( current );3. (current->getNext())->setPrev(newNode);4. current->setNext( newNode );5. current = newNode;6. size++;
size=62 6 8 7head
current
1
9newNode 1
2 34
http://ecomputernotes.com
![Page 23: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/23.jpg)
Circularly-linked lists
The next field in the last node in a singly-linked list is set to NULL.
Moving along a singly-linked list has to be done in a watchful manner.
Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node.
A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list.
http://ecomputernotes.com
![Page 24: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/24.jpg)
Circularly-linked lists
The next field in the last node in a singly-linked list is set to NULL.
Moving along a singly-linked list has to be done in a watchful manner.
Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node.
A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list.
http://ecomputernotes.com
![Page 25: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/25.jpg)
Circularly-linked lists
The next field in the last node in a singly-linked list is set to NULL.
Moving along a singly-linked list has to be done in a watchful manner.
Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node.
A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list.
http://ecomputernotes.com
![Page 26: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/26.jpg)
Circularly-linked lists
The next field in the last node in a singly-linked list is set to NULL.
Moving along a singly-linked list has to be done in a watchful manner.
Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node.
A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list.
http://ecomputernotes.com
![Page 27: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/27.jpg)
Cicularly Linked List
Two views of a circularly linked list:
2 6 8 7 1head
current
size=5
2
8
7
1
head
current
size=5
6
http://ecomputernotes.com
![Page 28: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/28.jpg)
Josephus Problem
A case where circularly linked list comes in handy is the solution of the Josephus Problem.
http://ecomputernotes.com
![Page 29: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/29.jpg)
Josephus Problem
A case where circularly linked list comes in handy is the solution of the Josephus Problem.
Consider there are 10 persons. They would like to choose a leader.
http://ecomputernotes.com
![Page 30: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/30.jpg)
Josephus Problem
A case where circularly linked list comes in handy is the solution of the Josephus Problem.
Consider there are 10 persons. They would like to choose a leader.
The way they decide is that all 10 sit in a circle.
http://ecomputernotes.com
![Page 31: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/31.jpg)
Josephus Problem
A case where circularly linked list comes in handy is the solution of the Josephus Problem.
Consider there are 10 persons. They would like to choose a leader.
The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in
clockwise direction and skip 3. Person 4 reached is eliminated.
http://ecomputernotes.com
![Page 32: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/32.jpg)
Josephus Problem
A case where circularly linked list comes in handy is the solution of the Josephus Problem.
Consider there are 10 persons. They would like to choose a leader.
The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in
clockwise direction and skip 3. Person 4 reached is eliminated.
The count starts with the fifth and the next person to go is the fourth in count.
http://ecomputernotes.com
![Page 33: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/33.jpg)
Josephus Problem
A case where circularly linked list comes in handy is the solution of the Josephus Problem.
Consider there are 10 persons. They would like to choose a leader.
The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in
clockwise direction and skip 3. Person 4 reached is eliminated.
The count starts with the fifth and the next person to go is the fourth in count.
Eventually, a single person remains.
http://ecomputernotes.com
![Page 34: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/34.jpg)
Josephus Problem
A case where circularly linked list comes in handy is the solution of the Josephus Problem.
Consider there are 10 persons. They would like to choose a leader.
The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in
clockwise direction and skip 3. Person 4 reached is eliminated.
The count starts with the fifth and the next person to go is the fourth in count.
Eventually, a single person remains.
![Page 35: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/35.jpg)
Josephus Problem N=10, M=3
98
7
6
54
3
2
1
10
http://ecomputernotes.com
![Page 36: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/36.jpg)
Josephus Problem N=10, M=3
98
7
6
54
3
2
1
10
eliminated
http://ecomputernotes.com
![Page 37: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/37.jpg)
Josephus Problem N=10, M=3
9
8
7
6
54
3
2
1
10
eliminated
http://ecomputernotes.com
![Page 38: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/38.jpg)
Josephus Problem N=10, M=3
9
8
7
6
54
3
2
1
10
eliminated
http://ecomputernotes.com
![Page 39: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/39.jpg)
Josephus Problem N=10, M=3
9
8
7
6
54
3
2
1
10
eliminated
http://ecomputernotes.com
![Page 40: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/40.jpg)
Josephus Problem N=10, M=3
9
8
7
6
54
3
2
1
10
eliminated
http://ecomputernotes.com
![Page 41: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/41.jpg)
Josephus Problem N=10, M=3
9
8
7
6
54
3
2
1
10
eliminated
http://ecomputernotes.com
![Page 42: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/42.jpg)
Josephus Problem N=10, M=3
9
8
7
6
54
3
2
1
10
eliminated
http://ecomputernotes.com
![Page 43: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/43.jpg)
Josephus Problem N=10, M=3
9
8
7
6
54
3
2
1
10
eliminated
http://ecomputernotes.com
![Page 44: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/44.jpg)
Josephus Problem N=10, M=3
9
8
7
6
54
3
2
1
10
eliminated
http://ecomputernotes.com
![Page 45: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/45.jpg)
Josephus Problem#include "CList.cpp"void main(int argc, char *argv[]){
CList list;int i, N=10, M=3;for(i=1; i <= N; i++ ) list.add(i);
list.start();while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl; list.remove();
}cout << "leader is: " << list.get() << endl;
}http://ecomputernotes.com
![Page 46: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/46.jpg)
Josephus Problem#include "CList.cpp"void main(int argc, char *argv[]){
CList list;int i, N=10, M=3;for(i=1; i <= N; i++ ) list.add(i);
list.start();while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl; list.remove();
}cout << "leader is: " << list.get() << endl;
}http://ecomputernotes.com
![Page 47: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/47.jpg)
Josephus Problem#include "CList.cpp"void main(int argc, char *argv[]){
CList list;int i, N=10, M=3;for(i=1; i <= N; i++ ) list.add(i);
list.start();while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl; list.remove();
}cout << "leader is: " << list.get() << endl;
}http://ecomputernotes.com
![Page 48: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/48.jpg)
Josephus Problem#include "CList.cpp"void main(int argc, char *argv[]){
CList list;int i, N=10, M=3;for(i=1; i <= N; i++ ) list.add(i);
list.start();while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl; list.remove();
}cout << "leader is: " << list.get() << endl;
}http://ecomputernotes.com
![Page 49: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/49.jpg)
Josephus Problem#include "CList.cpp"void main(int argc, char *argv[]){
CList list;int i, N=10, M=3;for(i=1; i <= N; i++ ) list.add(i);
list.start();while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl; list.remove();
}cout << "leader is: " << list.get() << endl;
}http://ecomputernotes.com
![Page 50: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/50.jpg)
Josephus Problem#include "CList.cpp"void main(int argc, char *argv[]){
CList list;int i, N=10, M=3;for(i=1; i <= N; i++ ) list.add(i);
list.start();while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl; list.remove();
}cout << "leader is: " << list.get() << endl;
}http://ecomputernotes.com
![Page 51: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/51.jpg)
Josephus Problem#include "CList.cpp"void main(int argc, char *argv[]){
CList list;int i, N=10, M=3;for(i=1; i <= N; i++ ) list.add(i);
list.start();while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl; list.remove();
}cout << "leader is: " << list.get() << endl;
}
![Page 52: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/52.jpg)
Josephus Problem#include "CList.cpp"void main(int argc, char *argv[]){
CList list;int i, N=10, M=3;for(i=1; i <= N; i++ ) list.add(i);
list.start();while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl; list.remove();
}cout << "leader is: " << list.get() << endl;
}
![Page 53: Computer notes - C++ Code for Linked List](https://reader033.vdocuments.net/reader033/viewer/2022061214/549b0d3fb4795933098b468d/html5/thumbnails/53.jpg)
Josephus Problem#include "CList.cpp"void main(int argc, char *argv[]){
CList list;int i, N=10, M=3;for(i=1; i <= N; i++ ) list.add(i);
list.start();while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl; list.remove();
}cout << "leader is: " << list.get() << endl;
}