computer notes - Data Structures - 3

Page 1

Class No.03 Data Structures http://ecomputernotes.com


Linked List 

Actual picture in memory:

current

head

1051

6

1052

1063

1053

1063

1054

2

1055

1051

1056

2

6

8

7

1

current

1057

7

1058

1060

1059

head

1060

1

1061

0

1062

1054

1063

8

1064

1057

http://ecomputernotes.com1065


Linked List Operations 

add(9): Create a new node in memory to hold ‘9’ Node* newNode = new Node(9);

newNode

http://ecomputernotes.com

9


Linked List Operations 

add(9): Create a new node in memory to hold ‘9’ Node* newNode = new Node(9);

newNode

Link the new node into the list head 2

6

8

current

7 2

1

size=5 6

1

3

9 newNode

http://ecomputernotes.com

9


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


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


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


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


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


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


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


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


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


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


C++ Code for Linked List #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; http://ecomputernotes.com


C++ Code for Linked List ď‚´

#include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; http://ecomputernotes.com


C++ Code for Linked List #include <stdlib.h> ď‚´ #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; http://ecomputernotes.com


C++ Code for Linked List #include <stdlib.h> #include "Node.cpp" ď‚´

class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; http://ecomputernotes.com


C++ Code for Linked List #include <stdlib.h> #include "Node.cpp" class List { ď‚´ public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; http://ecomputernotes.com


C++ Code for Linked List #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { ď‚´ headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; http://ecomputernotes.com


C++ Code for Linked List #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { ď‚´ headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; http://ecomputernotes.com


C++ Code for Linked List #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); ď‚´ headNode->setNext(NULL); currentNode = NULL; size = 0; }; http://ecomputernotes.com


C++ Code for Linked List #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); ď‚´ currentNode = NULL; size = 0; }; http://ecomputernotes.com


C++ Code for Linked List #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; ď‚´ size = 0; }; http://ecomputernotes.com


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


Building a Linked List List list;

headNode

size=0

http://ecomputernotes.com


Building a Linked List List list;

headNode

size=0 currentNode

list.add(2);

headNode

2

lastcurrentNode

http://ecomputernotes.com

size=1


Building a Linked List List list;

headNode

size=0 currentNode

list.add(2);

headNode

2

size=1

lastcurrentNode

currentNode

list.add(6);

headNode

2

6

size=2

lastcurrentNode

http://ecomputernotes.com


Building a Linked List List.add(8); list.add(7); list.add(1);

currentNode headNode

2

6

8

7

1

size=5

lastcurrentNode

http://ecomputernotes.com


C++ Code for Linked List int get() { if (currentNode != NULL) return currentNode->get(); };

http://ecomputernotes.com


C++ Code for Linked List bool next() { if (currentNode == NULL) return false; lastCurrentNode = currentNode; currentNode = currentNode->getNext(); if (currentNode == NULL || size == 0) return false; else return true; };

http://ecomputernotes.com


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.