Computer notes - Linked List

1,929 views

Published on

Create a new node in memory to hold ‘9’. Link the new node into the list. C++ Code for Linked List

Published in: Education, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,929
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
126
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • End of lecture 3 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • Computer notes - Linked List

    1. 1. Class No.03 Data Structures http://ecomputernotes.com
    2. 2. Linked List <ul><li>Actual picture in memory: </li></ul>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 1063 current 6 8 7 1 head current 1065 http://ecomputernotes.com 2
    3. 3. Linked List Operations <ul><li>add(9): Create a new node in memory to hold ‘9’ Node* newNode = new Node(9); </li></ul>http://ecomputernotes.com 9 newNode
    4. 4. Linked List Operations <ul><li>add(9): Create a new node in memory to hold ‘9’ Node* newNode = new Node(9); </li></ul><ul><li>Link the new node into the list </li></ul>http://ecomputernotes.com 9 newNode 2 6 8 7 1 head current size=5 6 9 newNode 1 3 2
    5. 5. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    6. 6. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    7. 7. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    8. 8. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    9. 9. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    10. 10. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    11. 11. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    12. 12. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    13. 13. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    14. 14. C++ Code for Linked List <ul><li>The Node class </li></ul><ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node *getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node *nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node *nextNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    15. 15. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com
    16. 16. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com 
    17. 17. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com 
    18. 18. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com 
    19. 19. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com 
    20. 20. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com 
    21. 21. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com 
    22. 22. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com 
    23. 23. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com 
    24. 24. <ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;Node.cpp&quot; </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>// Constructor </li></ul><ul><li>List() { </li></ul><ul><li>headNode = new Node(); </li></ul><ul><li>headNode->setNext(NULL); </li></ul><ul><li>currentNode = NULL; </li></ul><ul><li>size = 0; </li></ul><ul><li>}; </li></ul>C++ Code for Linked List http://ecomputernotes.com 
    25. 25. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    26. 26. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    27. 27. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    28. 28. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    29. 29. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    30. 30. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    31. 31. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    32. 32. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    33. 33. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    34. 34. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    35. 35. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    36. 36. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    37. 37. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    38. 38. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    39. 39. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    40. 40. C++ Code for Linked List <ul><li>void add(int addObject) { </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(addObject); </li></ul><ul><li>if( currentNode != NULL ){ </li></ul><ul><li> newNode->setNext(currentNode->getNext()); </li></ul><ul><li> currentNode->setNext( newNode ); </li></ul><ul><li> lastCurrentNode = currentNode; </li></ul><ul><li> currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li> newNode->setNext(NULL); </li></ul><ul><li> headNode->setNext(newNode); </li></ul><ul><li> lastCurrentNode = headNode; </li></ul><ul><li>currentNode = newNode; </li></ul><ul><li>} </li></ul><ul><li>size++; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com 
    41. 41. Building a Linked List headNode size=0 List list; http://ecomputernotes.com
    42. 42. Building a Linked List headNode 2 headNode currentNode size=1 lastcurrentNode size=0 List list; list.add(2); http://ecomputernotes.com
    43. 43. Building a Linked List headNode 2 headNode currentNode size=1 lastcurrentNode 2 6 headNode currentNode size=2 lastcurrentNode size=0 List list; list.add(2); list.add(6); http://ecomputernotes.com
    44. 44. Building a Linked List <ul><li>List.add(8); list.add(7); list.add(1); </li></ul>2 6 1 headNode currentNode size=5 lastcurrentNode 8 http://ecomputernotes.com 7
    45. 45. C++ Code for Linked List <ul><li>int get() { </li></ul><ul><li>if (currentNode != NULL) </li></ul><ul><li> return currentNode->get(); </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    46. 46. C++ Code for Linked List <ul><li>bool next() { </li></ul><ul><li>if (currentNode == NULL) return false; </li></ul><ul><li>lastCurrentNode = currentNode; </li></ul><ul><li>currentNode = currentNode->getNext(); </li></ul><ul><li>if (currentNode == NULL || size == 0) </li></ul><ul><li>return false; </li></ul><ul><li>else </li></ul><ul><li>return true; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com

    ×