Download presentation
Presentation is loading. Please wait.
1
Lists 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 n concat(L1, L2) returns a new list with L2 concatenated on to L1 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 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 L sort(L) returns the list in sorted order
2
A Node With Pointer NameLast: Smart FirstName: Joe StudentNumber: 8
SSN: Grade: 95
3
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?
4
Linked List head NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 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 null is a node pointer which is part of the node, we’ll call it next
5
Linked List with Tail tail head null NameLast: Smart FirstName: Joe
StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null
6
Linked List head NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 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 null is a node pointer which is part of the node, we’ll call it next
7
createEmptyList() Declare a pointer to type node called head n = 0
null
8
isEmptyList(L) if head == null return true else return false
9
isEmptyList(L) return head == null
10
access(L, i) declare a pointer temp if i < 0 or i >= n
return null temp = head for j = 0; j < i; j++ temp = temp.next return temp
11
length(L) int count = 0 temp = head while temp != null
count = count + 1 temp = temp.next return count
12
length(L) return n
13
searchFor(L, key) int count = 0 temp = head while temp != null
if temp.key = key return count count = count + 1 temp = temp.next return -1
14
insert(L, x) loop until temp.next == null then temp.next = x n = n + 1
15
insert(L, x) x.next = head head = x n = n + 1
16
concat(L1, L2) if head1 == null head1 = head2 else temp = head1
while temp.next != null temp = temp.next temp.next = head2 n1 = n1 + n2 return head1
17
remove(L, i) p1.next = p2.next P2.next = null p1 p2 head null p1 p2
NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 head null p1 p2 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 head null p1.next = p2.next P2.next = null
18
inserti(L, i, x) p2.next = p1.next p1.next = p2 p1 p2 head null head
NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 head null head NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null p2 p1 p2.next = p1.next p1.next = p2
19
search-remove p head null NameLast: Smart FirstName: Joe
StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 head null
20
Doubly Linked List Two pointers per node: next prev tail p head null
NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 head null null Two pointers per node: next prev
21
Circular Linked List p NameLast: Smart FirstName: Joe StudentNumber: 8
SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95
22
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
23
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).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.