17
Lecture 6 Feb 8, 2012 Goals: • Linked list (Chapter 3) • list class in STL (section 3.3) • implementing with linked lists

Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Embed Size (px)

Citation preview

Page 1: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Lecture 6 Feb 8, 2012

Goals:

• Linked list (Chapter 3)

• list class in STL (section 3.3)

• implementing with linked lists

Page 2: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Overview of list vs. array

• list can be incrementally grown. (dynamic array resizing is expensive.)

• inserting next to a given node takes constant time. (In arrays, this takes O(n) time where n = size of the array.)

• searching for a given key even in a sorted list takes O(n) time. (in sorted array, it takes O(log n) time by binary search.)

• accessing the k-th node takes O(k) time. (in array, this takes O(1) time.)

Page 3: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Abstract Data Type (ADT)o List ADT insert, find, delete (primary operations) successor, merge, reverse, …

(secondary)

o Variations of linked listso Circular listso Doubly linked lists

Review of ADT

Page 4: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Some list functionsConsider the standard singly-linked list class:

class list { private: Node* first; public: list(int k) { first = new Node(k);} void insert(int k, int posn); // insert k at posn Node* find(int k); // return first node containing k void delete(int k); // remove first node containing k int length(); // return the length of the list void delete_after(Node* n);// remove node after n, if it // exists void get_key(int posn); // return key in a given position void print_list(); // print the list . . . . . . . .}

Etc.

Other functions: delete a key in a position.

Page 5: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Implementing some of the functions

void insert(int k, int posn) { // insert k at a given position // write recursively.

}

Page 6: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Implementing some of the functions

void insert(int k, int posn) { // insert k at a given position // write recursively. if (posn < 1 || posn > length()) return; // wrong value of posn; ignore if (posn == 1) { list temp = new list(k); temp->next = first; first = temp; } else first->next = first->next.insert(k, posn-1);

}

Page 7: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Reverse the list

We want to reverse the list using the existing nodes of the list, i.e., without creating new nodes.

Page 8: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Reverse the list

We want to reverse the list using the existing node of the list, i.e., without creating new nodes.

void reverse() { if (head == NULL || head -> next == NULL) return; Node* p = head; Node* q = p-> next; p->next = NULL; while (q != NULL) { Node* temp = q -> next; q->next = p; p = q; q = temp; } head = p; }

Page 9: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Remove negative items from the list

Example: List: -3, 4, 5, -2, 11 becomes 4, 5, 11

We will write this one recursively.

Page 10: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Remove negative items from the list

Example: List: -3, 4, 5, -2, 11 becomes 4, 5, 11

We will write this one recursively.

void remove_negative() {// removes all the negative items from a list// Example input: -4 5 6 -2 8; output: 5 6 8 if (head == NULL) return; else if (head->key >= 0) {

List nList = List(head->next);nList.remove_negative();

head->next = nList.head; } else {

List nList = List(head->next); nList.remove_negative(); head = nList.head; } }

Page 11: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Generating all subsets of a given list

Generate all the subsets of a given set of numbers.

Thus, if the input is {1, 2, 4} the output is:

{}{1}{2}{4}{1, 2}{1, 4}{2, 4}{1, 2, 4}

Our program treats all the input symbols as distinct so a pre-condition is: the input array elements are distinct.

Page 12: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Data structure used

Array or vector of lists:

Each member of the vector is a pointer to a list containing one of the subsets.

null2

4 2

4

A 0

1

2

3

Page 13: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

build(A, j) will generate all the subsets of the set {A[0], A[1], … , A[j – 1]}.

Thus, if A = [2, 4, 6, 1], then build(A, 1) will generate all the subsets of {2}, build(A, 2) will generate all the subsets of {2, 4} etc.

build(A, 1) returns:

null

2 null

build(A, 2)Returns:

null2

4 2

4

Page 14: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

set(A,k) calls set(A, k – 1).

Make a copy of set(A,k-1). Call this temp. Insert A[k-1] into each set in temp.

Then merge set(A,k-1) and temp and make it the current collection.

Example: A = {1, 3, 2}. Suppose k = 2.

Build(A,2) returns the collection of lists [ ], [1], [3], [1, 3]. Now inserting 2 into each of the lists gives [2], [2,1], [2,3], [2,1,3].

Merging the two lists we get:

[ ], [1], [3], [1, 3], [2], [2,1], [2,3], [2,1,3].This is the set of all subsets of {1, 3, 2}.

Page 15: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Constructor written recursively

set(vector<int> A, int k) {if (k == 0) { List temp = new List(); A.push_back(temp); }else {set(vector<int> A, k-1); set temp = copy(B);merge(temp); }}

Page 16: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Constructors for set and List

set(int n) { size = n; for (int j=0; j < n; ++j) mems[j] = null;}

public:List() { first = 0;}

Page 17: Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Main function for subsets construction

int main() { int s; cout << "Enter the size of the set." << endl; cin >> s; vector<int> a(s); cout << "Enter the elements of the set." << endl; for (int j=0; j < s; ++j) cin >> a[j]; set(a,s).print();}