Download - Computer notes - Linked List
![Page 2: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/2.jpg)
Linked List Actual picture in memory:
1051
1052
1055
1059
1060
1061
1062
1063
1064
1056
1057
1058
1053
1054 2
6
8
7
1
1051
1063
1057
1060
0
head 1054
1063current
2 6 8 7 1
head
current
1065http://ecomputernotes.com
![Page 3: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/3.jpg)
Linked List Operations add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9);
9newNode
http://ecomputernotes.com
![Page 4: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/4.jpg)
Linked List Operations add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9);
Link the new node into the list
9newNode
2 6 8 7 1
head
current
size=5 6
9
newNode
1
3
2
http://ecomputernotes.com
![Page 5: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/5.jpg)
C++ Code for Linked ListThe Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 6: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/6.jpg)
C++ Code for Linked List
The Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 7: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/7.jpg)
C++ Code for Linked List
The Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 8: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/8.jpg)
C++ Code for Linked List
The Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 9: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/9.jpg)
C++ Code for Linked List
The Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 10: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/10.jpg)
C++ Code for Linked List
The Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 11: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/11.jpg)
C++ Code for Linked List
The Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 12: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/12.jpg)
C++ Code for Linked List
The Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 13: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/13.jpg)
C++ Code for Linked List
The Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 14: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/14.jpg)
C++ Code for Linked List
The Node class
class Node {public:
int get() { return object; };void set(int object) { this->object = object; };
Node *getNext() { return nextNode; };void setNext(Node *nextNode)
{ this->nextNode = nextNode; };private:
int object;Node *nextNode;
};
http://ecomputernotes.com
![Page 15: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/15.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 16: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/16.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 17: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/17.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 18: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/18.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 19: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/19.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 20: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/20.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 21: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/21.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 22: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/22.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 23: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/23.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 24: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/24.jpg)
#include <stdlib.h>#include "Node.cpp"
class List {public:
// ConstructorList() {
headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;
};
C++ Code for Linked List
http://ecomputernotes.com
![Page 25: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/25.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 26: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/26.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 27: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/27.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 28: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/28.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 29: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/29.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 30: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/30.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 31: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/31.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 32: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/32.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 33: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/33.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 34: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/34.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 35: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/35.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 36: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/36.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 37: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/37.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 38: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/38.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 39: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/39.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 40: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/40.jpg)
C++ Code for Linked List void add(int addObject) {
Node* newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;
} else {
newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode;
currentNode = newNode; } size++; }; http://ecomputernotes.com
![Page 41: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/41.jpg)
Building a Linked List
headNode size=0List list;
http://ecomputernotes.com
![Page 42: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/42.jpg)
Building a Linked List
headNode
2headNode
currentNode
size=1
lastcurrentNode
size=0List list;
list.add(2);
http://ecomputernotes.com
![Page 43: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/43.jpg)
Building a Linked List
headNode
2headNode
currentNode
size=1
lastcurrentNode
2 6headNode
currentNode
size=2
lastcurrentNode
size=0List list;
list.add(2);
list.add(6);
http://ecomputernotes.com
![Page 44: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/44.jpg)
Building a Linked List
List.add(8); list.add(7); list.add(1);
2 6 7 1headNode
currentNode
size=5
lastcurrentNode
8
http://ecomputernotes.com
![Page 45: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/45.jpg)
C++ Code for Linked List
int get() { if (currentNode != NULL) return currentNode->get();
};
http://ecomputernotes.com
![Page 46: Computer notes - Linked List](https://reader033.vdocuments.site/reader033/viewer/2022061212/5495435ab47959a1708b46d3/html5/thumbnails/46.jpg)
C++ Code for Linked Listbool next() {
if (currentNode == NULL) return false;
lastCurrentNode = currentNode;currentNode = currentNode->getNext();if (currentNode == NULL || size == 0)
return false;else
return true;};
http://ecomputernotes.com