Professional Documents
Culture Documents
Lecture No.02
Implementing Lists
1
14-Sep-19
Implementing Lists
current size
A 2 6 8 7 1
3 5
1 2 3 4 5
List Implementation
current size
step 1: A 2 6 8 7 1
3 5
1 2 3 4 5 6
current size
step 2: A 2 6 8 9 7 1
4 6
1 2 3 4 5 6
2
14-Sep-19
Implementing Lists
next():
current size
A 2 6 8 9 7 1
4 6
1 2 3 4 5 6 5
Implementing Lists
3
14-Sep-19
Implementing Lists
Implementing Lists
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
4
14-Sep-19
Implementing Lists
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
Implementing Lists
find(X): traverse the array until X is located.
int find(int X)
{
int j;
for(j=1; j < size+1; j++ )
if( A[j] == X ) break;
if( j < size+1 ) { // found X
current = j; // current points to where X found
return 1; // 1 for true
}
return 0; // 0 (false) indicates not found
}
5
14-Sep-19
Implementing Lists
Other operations:
add
we have to move every element to the right of
current to make space for the new element.
Worst-case is when we insert at the beginning; we
have to move every element right one place.
Average-case: on average we may have to move
half of the elements
6
14-Sep-19
remove
Worst-case: remove at the beginning, must shift all
remaining elements to the left.
Average-case: expect to move half of the elements.
find
Worst-case: may have to search the entire array
Average-case: search at most half the array.
7
14-Sep-19
8
14-Sep-19
9
14-Sep-19
Linked List
object next
Linked List
head
2 6 8 7 1 size=5
current
10
14-Sep-19
Linked List
Linked List
11
14-Sep-19
Linked List
Linked List
Actual picture in memory:
1051 6
1052 1063
current 1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
12
14-Sep-19
head
2 6 8 7 1 size=5 6
current 2 1
3
9
newNod
e
13
14-Sep-19
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; };
14
14-Sep-19
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; };
15
14-Sep-19
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; };
16
14-Sep-19
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; };
17
14-Sep-19
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; };
18
14-Sep-19
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
19
14-Sep-19
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
20
14-Sep-19
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
21
14-Sep-19
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
22
14-Sep-19
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
#include <stdlib.h>
#include "Node.cpp"
class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
23
14-Sep-19
24
14-Sep-19
25
14-Sep-19
26
14-Sep-19
27
14-Sep-19
28
14-Sep-19
29
14-Sep-19
30
14-Sep-19
31
14-Sep-19
currentNode
lastcurrentNode
32
14-Sep-19
currentNode
lastcurrentNode
currentNode
lastcurrentNode
currentNode
headNode 2 6 8 7 1 size=5
lastcurrentNode
33
14-Sep-19
int get() {
if (currentNode != NULL)
return currentNode->get();
};
lastCurrentNode = currentNode;
currentNode = currentNode->getNext();
if (currentNode == NULL || size == 0)
return false;
else
return true;
};
34