• No results found

DSA 03.ppt

N/A
N/A
Protected

Academic year: 2020

Share "DSA 03.ppt"

Copied!
52
0
0

Loading.... (view fulltext now)

Full text

(1)

Lectures 2 – 6

(2)

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 1

1 2 3 4 5

(3)

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’.

current 3

size 5

step 1: A 6 8 7 1

1 2 3 4 5

2 6 current 4 size 6

step 2: A 6 8 7 1

1 2 3 4 5

2

6 9

(4)

Implementing Lists

next():

current 4

size 6

A 6 8 7 1

1 2 3 4 5

2

6 9

(5)

Implementing Lists

remove(): removes the element at the current

index

current 5

size 6

A 6 8 1

1 2 3 4 5

2 6 9 5 Step 1: current 5 size 5

A 6 8 1

1 2 3 4 5

2 9

(6)

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.

current 5

size 5

A 6 8 1

1 2 3 4 5

2 9 Step 2: current 5 size 6

A 6 8 1

1 2 3 4 5

2

6 9

5

(7)

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

}

(8)

Implementing Lists

Other operations:

get()  return A[current];

update(X)  A[current] = X;

length()  return size;

back()  current--;

start()  current = 1;

(9)

Analysis of Array Lists

add

we have to move every element to the right of

current pointer 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.

 Best case is when the current is pointing to the last element of the list

Average-case: on average we may have to move

(10)

Analysis of Array Lists

remove

Worst-case: remove at the beginning, must

shift all remaining elements to the left.

Best case is when the current is pointing to

the last element of the list

Average-case: expect to move half of the

(11)

Analysis of Array Lists

Find ()

What is the worst case?

Best case?

(12)

Analysis of Array Lists

Find()

Worst-case: may have to search the entire

array. The desired elements is the last element of the list.

Best case is when the location of the desired

element is at the first is the list

(13)

Analysis of Array Lists

Which of the list operations are One-step

(14)

Analysis of Array Lists

How about the following operations?

get()  return A[current];

update(X)  A[current] = X;

length()  return size;

back()  current--;

start()  current = 1;

(15)

List

Using

Linked Memory

Various cells of memory are not allocated consecutively

in memory.

 An array may not be 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.

(16)

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.

(17)

Linked List

Picture of our list (2, 6, 7, 8, 1) stored as a

linked list:

2 6 8 7 1

head

current

(18)

Linked List

Note some features of the linked list:

Need a head pointer to point to the first node of

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

(19)

Linked List

Note some features of the linked list:

Need a head node to point to the first node of

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

start of the list.

(20)

Linked List

Note some features of the linked list:

Need a head node to point to the first node of

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

start of the list.

The current here is a pointer, not an index.

The next field in the last node points to nothing.

(21)

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 1063 current

2 6 8 7 1

head

current

(22)

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 1063 current

2 6 8 7 1

head

current

1065

What will be result of

(23)

Linked List Operations

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

(24)

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

9 newNode

2 6 8 7 1

head

current

size=5 6

9

newNode

1 3

(25)

Building a Linked List

headNode size=0

(26)

Building a Linked List

headNode

2 headNode

currentNode

size=1

lastcurrentNode

size=0

List list;

(27)

Building a Linked List

headNode

2 headNode

currentNode

size=1

lastcurrentNode

2 6 headNode

currentNode

size=2

lastcurrentNode

size=0

List list;

list.add(2);

(28)

Building a Linked List

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

2 6 7 1

headNode

currentNode

size=5

(29)

void remove() {

if( currentNode != NULL &&

currentNode != headNode) {

lastCurrentNode->setNext(currentNode->getNext());

delete currentNode;

currentNode = lastCurrentNode->getNext();

size--;

}

};

2 6 7 1

headNode

currentNode

size=5

lastcurrentNode

8

(30)

Removing a node from Linked list

void remove() {

if( currentNode != NULL &&

currentNode != headNode) {

lastCurrentNode->setNext(currentNode->getNext());

delete currentNode;

currentNode = lastCurrentNode->getNext();

size--;

}

};

2 6 7 1

headNode

currentNode

size=5

lastcurrentNode

(31)

void remove() {

if( currentNode != NULL &&

currentNode != headNode) {

lastCurrentNode->setNext(currentNode->getNext());

delete currentNode;

currentNode = lastCurrentNode->getNext();

size--;

}

};

2 7 1

headNode

currentNode

size=5

lastcurrentNode

8

(32)

void remove() {

if( currentNode != NULL &&

currentNode != headNode) {

lastCurrentNode->setNext(currentNode->getNext());

delete currentNode;

currentNode = lastCurrentNode->getNext();

size--;

}

};

2 7 1

headNode

currentNode

size=4

lastcurrentNode

8

(33)

Analysis of Linked List

add

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

(34)

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

(35)

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

(36)

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

(37)

Doubly-linked List

Moving forward in a singly-linked list is easy;

(38)

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

(39)

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 points to next node and another to points to the previous node:

(40)

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=5

2 6 8 7 1

head

current

What will be result of

(41)

Doubly-linked List

1. newNode->setNext( current->getNext() );

size=5

2 6 8 7

head

current

1

9

(42)

Doubly-linked List

1. newNode->setNext( current->getNext() );

2. newNode->setprev( current );

size=5

2 6 8 7

head

current

1

9

(43)

Doubly-linked List

1. newNode->setNext( current->getNext() );

2. newNode->setprev( current );

3. (current->getNext())->setPrev(newNode);

size=5

2 6 8 7

head

current

1

9

(44)

Doubly-linked List

1. newNode->setNext( current->getNext() );

2. newNode->setprev( current );

3. (current->getNext())->setPrev(newNode);

4. current->setNext( newNode );

size=5

2 6 8 7

head

current

1

9

(45)

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=6

2 6 8 7

head

current

1

9

(46)

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

(47)

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

(48)

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

(49)

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

(50)

Circularly Linked List

Two views of a circularly linked list:

2 6 8 7 1

head

current

size=5

2

8

7

1

head

current

size=5

(51)

Advantages of Linked List

 Linked lists are a dynamic data structure, which can

grow and be pruned, allocating and deallocating memory while the program is running.

 Insertion and deletion node operations are easily

implemented in a linked list.

 Dynamic data structures such as stacks and queues

can be implemented using a linked list.

 There is no need to define an initial size for a Linked

list.

 Items can be added or removed from the middle of list.

(52)

Disadvantages of Linked

List

 They use more memory than arrays because of the

storage used by their pointers.

 Nodes in a linked list must be read in order from the

beginning as linked lists are inherently sequential access.

 Nodes are stored incontiguously, greatly increasing the

time required to access individual elements within the list, especially with a CPU cache.

 Difficulties arise in linked lists when it comes to reverse

traversing. For instance, singly linked lists are

cumbersome to navigate backwards and while doubly linked lists are somewhat easier to read, memory is

References

Related documents

A doubly linear linked list contains two pointers and a data part where one of the pointer points to the preceding node and the other pointer will point to the subsequent node..

Including tokyo city as osaka guide looks great side trip to find the local and from.. Clearly demonstrates the time and transfer to set up

Finally, this theory of other hemisphere recruitment also sheds light on the frequent neuroimaging finding after TBI showing greater involvement of the right hemisphere

Dalam hal ini perjanjian internasional yang menjadi objek kajian adalah Mutual Commitments antara Pemerintah Liberia dan United Nations Peacebuilding Commision dalam

A doubly linked list is like a linked list, but each node also has a previous field, which points to the nodes predecessor..

Start Loop and run until Next points to NULL 'Check each Node until Next pointer points to NULL If value in Data of current Node is equal to Value then 'If we find

[8] A doubly linked list contains nodes which, in addition to a pointer storing the address of the next node, ha ve a pointer which stores the address of the previous node in

A doubly linked list node contains, minimally a data field and two pointers, one to next node and other to previous node.. The following struct can be used to define a