Successfully reported this slideshow.
Upcoming SlideShare
×

# Data structures cs301 power point slides lecture 03

709 views

Published on

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Data structures cs301 power point slides lecture 03

1. 1. Lecture No.03 Data Structures Dr. Sohail Aslam
2. 2. Linked List  Actual picture in memory: 1051 1052 1053 1063 2 1055 head 1063 1054 current 6 1051 1056 2 6 8 7 1 1057 1058 current 7 1060 1059 1060 1061 head 1 0 1062 1054 1063 8 1064 1057 1065
3. 3. Linked List Operations  add(9): Create a new node in memory to hold ‘9’ Node* newNode = new Node(9); newNode 9
4. 4. 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 3 9 newNode 1 1 size=5 6 9
5. 5. 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; };
6. 6. 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; };
7. 7. 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; };
8. 8. 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; };
9. 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; };
10. 10. 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; };
11. 11. 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; };
12. 12. 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; };
13. 13. 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; };
14. 14. 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; };
15. 15. 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; };
16. 16. 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; };
17. 17. 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; };
18. 18. 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; };
19. 19. 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; };
20. 20. 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; };
21. 21. 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; };
22. 22. 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; };
23. 23. 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; };
24. 24. 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; };
25. 25. 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++; };
26. 26. 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++; };
27. 27. 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++; };
28. 28. 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++; };
29. 29. 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++; };
30. 30. 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++; };
31. 31. 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++; };
32. 32. 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++; };
33. 33. 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++; };
34. 34. 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++; };
35. 35. 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++; };
36. 36. 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++; };
37. 37. 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++; };
38. 38. 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++; };
39. 39. 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++; };
40. 40. 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++; };