list l = x 0 x 1 x 2 x 3 … x n-1 n = # elements if a list is ordered than the key of x i-1

23
List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1 <= the key of x i for all i where 0 < i < n. The sort symbol <= can be replaced by >= or any other function that determines ordering in the keys. An unordered list does not have this restriction. Functions: access(L, i) returns x i length(L) returns n concat(L 1 , L 2 ) returns a new list with L 2 concatenated on to L 1 createEmptyList() returns a newly created empty list isEmptyList(L) returns true if L is empty and false if it is not searchFor(L, key) returns i where the key of x i = key remove(L, i) returns a list with x removed; the old x is Lists

Upload: nigel-paddy

Post on 15-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

List L = x0 x1 x2 x3 … xn-1 n = # elements

If a list is ordered than the key of xi-1 <= the key of xi for all i where 0 < i < n.

The sort symbol <= can be replaced by >= or any other function that determines ordering in the keys.An unordered list does not have this restriction. Functions:access(L, i) returns xi

length(L) returns nconcat(L1, L2) returns a new list with L2 concatenated on to L1

createEmptyList() returns a newly created empty listisEmptyList(L) returns true if L is empty and false if it is notsearchFor(L, key) returns i where the key of xi = key

remove(L, i) returns a list with xi removed; the old xi+1 is now xi, etc.

inserti(L, i, x) returns a list with x inserted as xi; the old xi is now xi+1, etc.

insert(L, x) returns a list with x added to Lsort(L) returns the list in sorted order

Lists

Page 2: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

A Node With Pointer

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

Page 3: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

Homework 1

• Describe how to use a set of nodes with pointers (as described in class) to implement a variable length unordered list.

• Determine how to do the following functions: access, length, concat, createEmptyList, isEmptyList, searchFor, remove, inserti, and insert.

• How would any of these functions change if the list was to be ordered?

Page 4: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

Linked Listhead

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

is a node pointer which is part of the node, we’ll call it next

when we pass a list in as a parameter we pass in the head

a pointer pointing to null indicates the end of the list

Page 5: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

Linked List with Tailhead

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

tail

Page 6: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

Linked Listhead

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

is a node pointer which is part of the node, we’ll call it next

when we pass a list in as a parameter we pass in the head

a pointer pointing to null indicates the end of the list

Page 7: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

createEmptyList()

Declare a pointer to type node called head

n = 0head

null

Page 8: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

isEmptyList(L)

if head == nullreturn true

elsereturn false

Page 9: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

isEmptyList(L)

return head == null

Page 10: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

access(L, i)

declare a pointer temp

if i < 0 or i >= nreturn null

temp = head

for j = 0; j < i; j++temp = temp.next

return temp

Page 11: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

length(L)

int count = 0temp = head

while temp != nullcount = count + 1temp = temp.next

return count

Page 12: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

length(L)

return n

Page 13: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

searchFor(L, key)

int count = 0temp = head

while temp != nullif temp.key = key

return countcount = count + 1temp = temp.next

return -1

Page 14: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

insert(L, x)

loop until temp.next == null

then temp.next = x

n = n + 1

Page 15: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

insert(L, x)

x.next = head

head = x

n = n + 1

Page 16: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

concat(L1, L2)

if head1 == null

head1 = head2

elsetemp = head1

while temp.next != nulltemp = temp.next

temp.next = head2

n1 = n1 + n2

return head1

Page 17: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

remove(L, i)

head

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

p2p1

head

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

p2p1

p1.next = p2.nextP2.next = null

Page 18: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

inserti(L, i, x)

head

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

p2p1

head

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

p2p1

p2.next = p1.next

p1.next = p2

Page 19: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

search-remove

head

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

p

Page 20: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

Doubly Linked List

head

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

ptail

null

Two pointers per node: next prev

Page 21: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

Circular Linked List

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

p

Page 22: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

Stacks

Stack S = x0 x1 x2 x3 … xn-1 n = # elements

A stack is a list but the nodes are only accessed last-in-first-out (LIFO).

 

Functions:

createEmptyStack() returns a newly created empty stack

top(S) returns the last node of S

pop(S) returns and removes the last node of S

push(S, x) returns a S with x added as the last element

isEmptyStack(S) returns true if S is empty and false if it is not

Page 23: List L = x 0 x 1 x 2 x 3 … x n-1 n = # elements If a list is ordered than the key of x i-1

Homework 2

• Describe how to implement a stack using an array (assume it will never have more than 100 elements).

• Do the five stack functions.• Describe how to implement a stack using an

set of nodes (this stack will have no number of element limit).

• Do the five stack functions.