Lecture No.03 Data Structures: Dr. Sohail Aslam
Lecture No.03 Data Structures: Dr. Sohail Aslam
Lecture No.03 Data Structures: Dr. Sohail Aslam
03
Data Structures
2 6 8 7 1 size=5 6
current 2 1
3
9
newNod
e
C++ Code for Linked List
The Node class
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
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;
};
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;
};
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;
};
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;
};
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;
};
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;
};
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;
};
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;
};
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;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
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++;
};
Building a Linked List
List list; headNode size=0
Building a Linked List
List list; headNode size=0
currentNode
lastcurrentNode
Building a Linked List
List list; headNode size=0
currentNode
lastcurrentNode
currentNode
lastcurrentNode
Building a Linked List
currentNode
headNode 2 6 8 7 1 size=5
lastcurrentNode
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 = currentNode->getNext();
if (currentNode == NULL || size == 0)
return false;
else
return true;
};