Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Lecture No.03
Data Structures
Dr. Sohail Aslam
Linked List


Actual picture in memory:
1051
1052
1053

1063
2

1055

head

1063

1054

current

6

1051

1056

2

6

8

...
Linked List Operations


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

newNode

9
Linked List Operations


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



newNode

Link t...
C++ Code for Linked List
The Node class
class Node {
public:
int get() { return object; };
void set(int object) { this->ob...
C++ Code for Linked List
The Node class


class Node {
public:
int get() { return object; };
void set(int object) { this-...
C++ Code for Linked List
The Node class


class Node {
public:
int get() { return object; };
void set(int object) { this-...
C++ Code for Linked List
The Node class



class Node {
public:
int get() { return object; };
void set(int object) { this...
C++ Code for Linked List
The Node class



class Node {
public:
int get() { return object; };
void set(int object) { this...
C++ Code for Linked List
The Node class
class Node {
public:
int get() { return object; };
void set(int object) { this->ob...
C++ Code for Linked List
The Node class
class Node {
public:
int get() { return object; };
void set(int object) { this->ob...
C++ Code for Linked List
The Node class
class Node {
public:
int get() { return object; };
void set(int object) { this->ob...
C++ Code for Linked List
The Node class
class Node {
public:
int get() { return object; };
void set(int object) { this->ob...
C++ Code for Linked List
The Node class
class Node {
public:
int get() { return object; };
void set(int object) { this->ob...
C++ Code for Linked List
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = n...
C++ Code for Linked List


#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode ...
C++ Code for Linked List
#include <stdlib.h>
 #include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode =...
C++ Code for Linked List
#include <stdlib.h>
#include "Node.cpp"


class List {
public:
// Constructor
List() {
headNode ...
C++ Code for Linked List
#include <stdlib.h>
#include "Node.cpp"
class List {
 public:
// Constructor
List() {
headNode =...
C++ Code for Linked List
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {

headNode =...
C++ Code for Linked List
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {

headNode =...
C++ Code for Linked List
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = n...
C++ Code for Linked List
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = n...
C++ Code for Linked List
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = n...
C++ Code for Linked List
void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNode !=...
C++ Code for Linked List


void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNode...
C++ Code for Linked List


void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNode...
C++ Code for Linked List


void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNode...
C++ Code for Linked List


void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNode...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
C++ Code for Linked List



void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNod...
Building a Linked List
List list;

headNode

size=0
Building a Linked List
List list;

headNode

size=0
currentNode

list.add(2);

headNode
lastcurrentNode

2

size=1
Building a Linked List
List list;

headNode

size=0
currentNode

list.add(2);

headNode

2

size=1

lastcurrentNode

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

currentNode
headNode

2

6

8

7

lastcurrentNode

1

size=...
C++ Code for Linked List
int get() {
if (currentNode != NULL)
return currentNode->get();
};
C++ Code for Linked List
bool next() {
if (currentNode == NULL) return false;
lastCurrentNode = currentNode;
currentNode =...
Upcoming SlideShare
Loading in …5
×

Data structures cs301 power point slides lecture 03

709 views

Published on

Published in: Technology
  • 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++; };
  41. 41. Building a Linked List List list; headNode size=0
  42. 42. Building a Linked List List list; headNode size=0 currentNode list.add(2); headNode lastcurrentNode 2 size=1
  43. 43. 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 lastcurrentNode size=2
  44. 44. Building a Linked List List.add(8); list.add(7); list.add(1); currentNode headNode 2 6 8 7 lastcurrentNode 1 size=5
  45. 45. C++ Code for Linked List int get() { if (currentNode != NULL) return currentNode->get(); };
  46. 46. 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; };

×