cscs-200 data structure and algorithms lecture 9-10-11

117
CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Upload: eric-isaac-hopkins

Post on 19-Jan-2018

227 views

Category:

Documents


1 download

DESCRIPTION

Lists  A list is collection of items that are all of the same type (grocery items, integers, names)  The items, or elements of the list, are stored in some particular order  It is possible to insert new elements into various positions in the list and remove any element of the list

TRANSCRIPT

Page 1: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

CSCS-200 Data Structure and Algorithms

Lecture 9-10-11

Page 2: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

The LIST Data Structure The List is among the most generic of data structures.

Real life:

a. shopping list, b. groceries list, c. list of people to invite to dinnerd. List of presents to get

Page 3: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Lists A list is collection of items that are all of the same

type (grocery items, integers, names)

The items, or elements of the list, are stored in some particular order

It is possible to insert new elements into various positions in the list and remove any element of the list

Page 4: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Lists List is a set of elements in a linear order.

For example, data values a1, a2, a3, a4 can be arranged in a list:

(a3, a1, a2, a4)

In this list, a3, is the first element, a1 is the second element, and so on

The order is important here; this is not just a random collection of elements, it is an ordered collection

Page 5: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

List OperationsUseful operations• createList(): create a new list (presumably empty)• copy(): set one list to be a copy of another• clear(); clear a list (remove all elments)• insert(X, ?): Insert element X at a particular position

in the list• remove(?): Remove element at some position in

the list• get(?): Get element at a given position• update(X, ?): replace the element at a given position with X• find(X): determine if the element X is in the list• length(): return the length of the list.

Page 6: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

List Operations We need to decide what is meant by “particular position”; we

have used “?” for this.

There are two possibilities:

1. Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays

2. Use a “current” marker or pointer to refer to a particular position in the list.

Page 7: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

List Operations If we use the “current” marker, the following four

methods would be useful:

start(): moves to “current” pointer to the very first element.

tail(): moves to “current” pointer to the very last element.

next(): move the current position forward one element

back(): move the current position backward one element

Page 8: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Implementing Lists We have designed the interface for the List; we now must

consider how to implement that interface.

Page 9: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Implementing Lists We have designed the interface for the List; we now

must consider how to implement that interface. Implementing Lists using an array: for example, the

list of integers (2, 6, 8, 7, 1) could be represented as:

A 6 8 7 11 2 3 4 5

2current

3size

5

Page 10: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

List Implementation add(9); current position is 3. The new list would thus be: (2,

6, 8, 9, 7, 1) We will need to shift everything to the right of 8 one place to

the right to make place for the new element ‘9’.

current3

size5

step 1: A 6 8 7 11 2 3 4 5

26

current4

size6

step 2: A 6 8 7 11 2 3 4 5

26

9

notice: current pointsto new element

Page 11: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Implementing Lists next():

current4

size6

A 6 8 7 11 2 3 4 5

26

95

Page 12: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Implementing Lists next():

current4

size6

A 6 8 7 11 2 3 4 5

26

95

There are special cases for positioning the current pointer:

a. past the last array cell b. before the first cell

Page 13: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Implementing Lists next():

current4

size6

A 6 8 7 11 2 3 4 5

26

95

There are special cases for positioning the current pointer:

a. past the last array cell b. before the first cell

We will have to worry about these when we write the actual code.

Page 14: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Implementing Lists remove(): removes the element at the current

index

current5

size6

A 6 8 11 2 3 4 5

26

9

5

Step 1:

current5

size5

A 6 8 11 2 3 4 5

2 9Step 2:

Page 15: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Implementing Lists remove(): removes the element at the current

index

We fill the blank spot left by the removal of 7 by

shifting the values to the right of position 5 over to the left one space.

current5

size5

A 6 8 11 2 3 4 5

2 9Step 2:

current5

size6

A 6 8 11 2 3 4 5

26

9

5

Step 1:

Page 16: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Implementing Listsfind(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}

Page 17: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Implementing Lists Other operations:

get() return A[current];update(X) A[current] = X;length() return size;back() current--;start() current = 1;end() current = size;

Page 18: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Analysis of Array Lists 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

Page 19: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Analysis of Array Lists 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.

Other operations are one-step.

Page 20: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

List Using Linked Memory Various cells of memory are not allocated consecutively

in memory.

Page 21: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

List Using Linked Memory Various cells of memory are not allocated consecutively

in memory. Not enough to store the elements of the list.

Page 22: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

List Using Linked Memory Various cells of memory are not allocated consecutively

in memory. Not enough to store the elements of the list. With arrays, the second element was right next to the

first element.

Page 23: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

List Using Linked Memory Various cells of memory are not allocated consecutively

in memory. Not enough to store the elements of the list. With arrays, the second element was right next to the

first element. Now the first element must explicitly tell us where to

look for the second element.

Page 24: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

List Using Linked Memory Various cells of memory are not allocated

consecutively in memory. Not enough to store the elements of the list. With arrays, the second element was right next

to the first element. Now the first element must explicitly tell us

where to look for the second element. Do this by holding the memory address of the

second element

Page 25: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Linked List Create a structure called a Node.

object next

The object field will hold the actual list element. The next field in the structure will hold the

starting location of the next node. Chain the nodes together to form a linked list.

Page 26: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Linked List Picture of our list (2, 6, 7, 8, 1) stored as a linked list:

2 6 8 7 1

head

current

size=5

Page 27: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Linked ListNote some features of the list: Need a head to point to the first node of the list.

Otherwise we won’t know where the start of the list is.

Page 28: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Linked ListNote some features of the list: Need a head to point to the first node of the list.

Otherwise we won’t know where the start of the list is. The current here is a pointer, not an index.

Page 29: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Linked ListNote some features of the list: Need a head to point to the first node of the list.

Otherwise we won’t know where the start of the list is. The current here is a pointer, not an index. The next field in the last node points to nothing. We will

place the memory address NULL which is guaranteed to be inaccessible.

Page 30: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Linked List Actual picture in memory:

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

1063current

2 6 8 7 1

head

current

1065

Page 31: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Linked List Operations add(9): Create a new node in memory to hold ‘9’

Node* newNode = new Node(9);

9newNode

Page 32: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Linked List Operations add(9): Create a new node in memory to hold ‘9’

Node* newNode = new Node(9);

Link the new node into the list

9newNode

2 6 8 7 1

head

current

size=5 6

9

newNode

1

3

2

Page 33: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

C++ Code for Linked ListThe 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;

};

Page 34: CSCS-200 Data Structure and Algorithms Lecture 9-10-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;

};

Page 35: CSCS-200 Data Structure and Algorithms Lecture 9-10-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;

};

Page 36: CSCS-200 Data Structure and Algorithms Lecture 9-10-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;

};

Page 37: CSCS-200 Data Structure and Algorithms Lecture 9-10-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;

};

Page 38: CSCS-200 Data Structure and Algorithms Lecture 9-10-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;

};

Page 39: CSCS-200 Data Structure and Algorithms Lecture 9-10-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;

};

Page 40: CSCS-200 Data Structure and Algorithms Lecture 9-10-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;

};

Page 41: CSCS-200 Data Structure and Algorithms Lecture 9-10-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;

};

Page 42: CSCS-200 Data Structure and Algorithms Lecture 9-10-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;

};

Page 43: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 44: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 45: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 46: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 47: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 48: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 49: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 50: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 51: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 52: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

#include <stdlib.h>#include "Node.cpp"

class List {public:

// ConstructorList() {

headNode = new Node();headNode->setNext(NULL);currentNode = NULL;size = 0;

};

C++ Code for Linked List

Page 53: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 54: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 55: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 56: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 57: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 58: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 59: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 60: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 61: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 62: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 63: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 64: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 65: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 66: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 67: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 68: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

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++; };

Page 69: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Building a Linked ListheadNode size=0List list;

Page 70: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Building a Linked ListheadNode

2headNode

currentNode

size=1

lastcurrentNode

size=0List list;

list.add(2);

Page 71: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Building a Linked ListheadNode

2headNode

currentNode

size=1

lastcurrentNode

2 6headNode

currentNode

size=2

lastcurrentNode

size=0List list;

list.add(2);

list.add(6);

Page 72: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Building a Linked List

List.add(8); list.add(7); list.add(1);

2 6 7 1headNode

currentNode

size=5

lastcurrentNode

8

Page 73: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

C++ Code for Linked List

int get() { if (currentNode != NULL) return currentNode->get();

};

Page 74: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

C++ Code for Linked Listbool next() {

if (currentNode == NULL) return false;

lastCurrentNode = currentNode;currentNode = currentNode->getNext();if (currentNode == NULL || size == 0)

return false;else

return true;};

Page 75: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

C++ Code for Linked List// position current before the first// list elementvoid start() {

lastCurrentNode = headNode;currentNode = headNode;

};

Page 76: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

C++ Code for Linked Listvoid remove() { if( currentNode != NULL && currentNode != headNode) {

lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--;

}};

2 6 7 1

headNodecurrentNode

size=5

lastcurrentNode

8

Page 77: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

C++ Code for Linked Listvoid remove() { if( currentNode != NULL && currentNode != headNode) {

lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--;

}};

2 6 7 1

headNodecurrentNode

size=5

lastcurrentNode

8

1

1

Page 78: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

C++ Code for Linked Listvoid remove() { if( currentNode != NULL && currentNode != headNode) {

lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--;

}};

2 7 1

headNodecurrentNode

size=5

lastcurrentNode

8

1

1

2

2

Page 79: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

C++ Code for Linked Listvoid remove() { if( currentNode != NULL && currentNode != headNode) {

lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--;

}};

2 7 1

headNodecurrentNode

size=4

lastcurrentNode

8

1

1

2

2

3

3

4

4

Page 80: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

C++ Code for Linked Listint length() {

return size; };

private:int size;Node *headNode;Node *currentNode, *lastCurrentNode;

Page 81: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Example of List Usage#include <iostream>#include <stdlib.h>#include "List.cpp"

int main(int argc, char *argv[]){

List list;

list.add(5); list.add(13); list.add(4);list.add(8); list.add(24); list.add(48); list.add(12);list.start();while (list.next())

cout << "List Element: "<< list.get()<<endl;

}

Page 82: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Analysis of Linked List add

• we simply insert the new node after the current node. So add is a one-step operation.

Page 83: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Analysis of Linked List add

• we simply insert the new node after the current node. So add is a one-step operation.

remove remove is also a one-step operation

Page 84: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Analysis of Linked List add

• we simply insert the new node after the current node. So add is a one-step operation.

remove remove is also a one-step operation

find worst-case: may have to search the entire list

Page 85: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Analysis of Linked List add

• we simply insert the new node after the current node. So add is a one-step operation.

remove remove is also a one-step operation

find worst-case: may have to search the entire list

back moving the current pointer back one node requires

traversing the list from the start until the node whose next pointer points to current node.

Page 86: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-linked List Moving forward in a singly-linked list is easy;

moving backwards is not so easy.

Page 87: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-linked List Moving forward in a singly-linked list is easy;

moving backwards is not so easy. To move back one node, we have to start at the

head of the singly-linked list and move forward until the node before the current.

Page 88: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-linked List Moving forward in a singly-linked list is easy;

moving backwards is not so easy. To move back one node, we have to start at the

head of the singly-linked list and move forward until the node before the current.

To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node:

element nextprev

Page 89: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-Linked List Nodeclass Node {public:

int get() { return object; };void set(int object) { this->object = object; };

Node* getNext() { return nextNode; };void setNext(Node* nextNode)

{ this->nextNode = nextNode; };Node* getPrev() { return prevNode; };void setPrev(Node* prevNode)

{ this->prevNode = prevNode; };private:

int object;Node* nextNode;Node* prevNode;

};

Page 90: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-linked List Need to be more careful when adding or

removing a node. Consider add: the order in which pointers are

reorganized is important:

size=52 6 8 7 1head

current

Page 91: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-linked List 1. newNode->setNext( current->getNext() );

size=52 6 8 7head

current

1

9newNode 1

Page 92: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-linked List 1. newNode->setNext( current->getNext() );2. newNode->setprev( current );

size=52 6 8 7head

current

1

9newNode 1

2

Page 93: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-linked List 1. newNode->setNext( current->getNext() );2. newNode->setprev( current );3. (current->getNext())->setPrev(newNode);

size=52 6 8 7head

current

1

9newNode 1

2 3

Page 94: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-linked List 1. newNode->setNext( current->getNext() );2. newNode->setprev( current );3. (current->getNext())->setPrev(newNode);4. current->setNext( newNode );

size=52 6 8 7head

current

1

9newNode 1

2 34

Page 95: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Doubly-linked List 1. newNode->setNext( current->getNext() );2. newNode->setprev( current );3. (current->getNext())->setPrev(newNode);4. current->setNext( newNode );5. current = newNode;6. size++;

size=62 6 8 7head

current

1

9newNode 1

2 34

Page 96: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Circularly-linked lists The next field in the last node in a singly-linked

list is set to NULL. Moving along a singly-linked list has to be done

in a watchful manner. Doubly-linked lists have two NULL pointers:

prev in the first node and next in the last node. A way around this potential hazard is to link the

last node with the first node in the list to create a circularly-linked list.

Page 97: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Circularly-linked lists The next field in the last node in a singly-linked

list is set to NULL. Moving along a singly-linked list has to be done

in a watchful manner. Doubly-linked lists have two NULL pointers:

prev in the first node and next in the last node. A way around this potential hazard is to link the

last node with the first node in the list to create a circularly-linked list.

Page 98: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Circularly-linked lists The next field in the last node in a singly-linked

list is set to NULL. Moving along a singly-linked list has to be done

in a watchful manner. Doubly-linked lists have two NULL pointers:

prev in the first node and next in the last node. A way around this potential hazard is to link the

last node with the first node in the list to create a circularly-linked list.

Page 99: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Circularly-linked lists The next field in the last node in a singly-linked

list is set to NULL. Moving along a singly-linked list has to be done

in a watchful manner. Doubly-linked lists have two NULL pointers:

prev in the first node and next in the last node. A way around this potential hazard is to link the

last node with the first node in the list to create a circularly-linked list.

Page 100: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Cicularly Linked List Two views of a circularly linked list:

2 6 8 7 1head

current

size=5

28

7

1

head

current

size=5

6

Page 101: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem A case where circularly linked list comes in

handy is the solution of the Josephus Problem.

Page 102: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem A case where circularly linked list comes in

handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like

to choose a leader.

Page 103: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem A case where circularly linked list comes in

handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like

to choose a leader. The way they decide is that all 10 sit in a circle.

Page 104: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem A case where circularly linked list comes in

handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like

to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in

clockwise direction and skip 3. Person 4 reached is eliminated.

Page 105: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem A case where circularly linked list comes in

handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like

to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in

clockwise direction and skip 3. Person 4 reached is eliminated.

The count starts with the fifth and the next person to go is the fourth in count.

Page 106: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem A case where circularly linked list comes in

handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like

to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in

clockwise direction and skip 3. Person 4 reached is eliminated.

The count starts with the fifth and the next person to go is the fourth in count.

Eventually, a single person remains.

Page 107: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem A case where circularly linked list comes in

handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like

to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in

clockwise direction and skip 3. Person 4 reached is eliminated.

The count starts with the fifth and the next person to go is the fourth in count.

Eventually, a single person remains.

Page 108: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

98

7

6

54

3

2

1

10

Page 109: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

98

7

6

54

3

2

1

10

eliminated

Page 110: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

9

8

7

6

54

3

2

1

10

eliminated

Page 111: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

9

8

7

6

54

3

2

1

10

eliminated

Page 112: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

9

8

7

6

54

3

2

1

10

eliminated

Page 113: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

9

8

7

6

54

3

2

1

10

eliminated

Page 114: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

9

8

7

6

54

3

2

1

10

eliminated

Page 115: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

9

8

7

6

54

3

2

1

10

eliminated

Page 116: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

9

8

7

6

54

3

2

1

10

eliminated

Page 117: CSCS-200 Data Structure and Algorithms Lecture 9-10-11

Josephus Problem N=10, M=3

9

8

7

6

54

3

2

1

10

eliminated