9
Array A data structure that contains a list of elements accessed by an index Array: a set of pairs (index and value) For each index, there is a value associated with that index. Arrays are generally a fixed size, which means you must specify the maximum size when the array is created Increasing the size of an array normally involves creating a new array of the desired size and copying the values from the old array to the new array

5 data structures-arraysandlinkedlist

Embed Size (px)

Citation preview

Page 1: 5 data structures-arraysandlinkedlist

Array

• A data structure that contains a list of elements accessed by an index– Array: a set of pairs (index and value)

– For each index, there is a value associated with that index.

• Arrays are generally a fixed size, which means you must specify the maximum size when the array is created

• Increasing the size of an array normally involves creating a new array of the desired size and copying the values from the old array to the new array

Page 2: 5 data structures-arraysandlinkedlist

Array

• An array of size 5

String arr = new String[5];

Int nextIndex = 0;

• Set some of the values

arr[nextIndex ] = “Hi”

++nextIndex;

arr[nextIndex ] = “there”

++nextIndex;

arr[nextIndex ] = “Bob”

++nextIndex;

arr[nextIndex ] = “.”

++nextIndex;

Hi there Bob .

Page 3: 5 data structures-arraysandlinkedlist

Array• Insert into the middle of a array

– Insert “Billy” between “there” and “Bob”

• Push “Bob” and “.” toward the end of the array to make space for “Bob” in element 2

insertIndex = 2;

for ( int i = nextIndex; i > = insertIndex; --i ) {

arr[i] = arr[i-1];

}

++nextIndex;

• Set the new value for element 2

arr[insertIndex] = “Billy”;

Hi there Bob .

Hi there Bob Bob .

Hi there Billy Bob .

Page 4: 5 data structures-arraysandlinkedlist

Questions

• What is the complexity of “retrieve” in an array?

• What is the complexity of “store” in an array?

• What about insertion and deletion for ordered elements in an arrary?

Page 5: 5 data structures-arraysandlinkedlist

Linked List

• A linked list is a data structure that contains a set of nodes linked with pointers.

• Singly-linked list has links in one direction. To the next node in the list.

• A doubly-linked list had links in both directions. To the next and previous nodes in the list.

Data

Next

Variable

Variable Data

Next

Prev

Data

Next

Data

Next

Data

Next

Data

Next

Prev

Data

Next

Prev

Data

Next

Prev

Page 6: 5 data structures-arraysandlinkedlist

Linked List

• A Circularly-linked list has a complete cycles of links.

Variable Data

Next

Prev

Data

Next

Prev

Data

Next

Prev

Data

Next

Prev

Data

Next

Variable Data

Next

Data

Next

Data

Next

Page 7: 5 data structures-arraysandlinkedlist

Linked List

• Insert at the end of a linked list

• Create new node

• Change the links to include the new node

Billy

Next

Hi

Next

Sentence there

Next

Bob

Next

.

Next

Page 8: 5 data structures-arraysandlinkedlist

Linked List

• Insert into the middle of a linked list

• Create new node

• Change the links to include the new node

Hi

Next

Sentence there

Next

Bob

Next

.

Next

Billy

Next

Page 9: 5 data structures-arraysandlinkedlist

Array versus Linked List

• Some operations are faster with one versus the other– Find element at position X – Array faster

– Find element Y at unknown position – Tie

– Insert element in the middle – Linked list faster

– Insert element at the end – Tie, if doubly-linked and circular, Array otherwise

– Insert element at the beginning – Linked list

• Arrays are (typically) a fixed size, linked lists are variable length

• Arrays are defined as holding a specific number of elements whether or not that many elements are currently in the array. Doing that wastes space