Download presentation

Presentation is loading. Please wait.

Published bySydni Dollard Modified over 2 years ago

1
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications

2
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.)

3
Abstract Data Type (ADT) oList ADT insert, find, delete (primary operations) successor, merge, reverse, … (secondary) oVariations of linked lists oCircular lists oDoubly linked lists Review of ADT

4
Some list functions Consider the standard singly-linked list class: class list { private: Node* first; list(int k) { first = new Node(k);} void insert(int k, int posn); // code in Lecture 5 Node* find(int k); // return first node containing k void delete(int k); // remove first node containing k 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........ } Some additional functions: Reverse the list Remove all the negative items from the list

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

6
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; }

7
Remove negative items from the list Example: List: -3, 4, 5, -2, 11 becomes 4, 5, 11 We will write this one recursively.

8
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; }

9
A problem similar to Project # 1 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.

10
Data structure used Array or vector of lists: Each member of the vector is a pointer to a list containing one of the subsets. null 2 42 4 A 01230123

11
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 build(A, 2) Returns: null 2 42 4

13
build (A, k) calls build(A, k – 1). Let temp be the result from the call. It creates a copy of temp, say temp1. It inserts A[k-1] into each list of temp1. It merges temp and temp1 and returns the merged array of lists. 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}.

14
Concept of a static function class List{ private: Node* first; public: List ( ); // constructor ~List( ); // destructor void insert(int k); // insert k as the first item void print(); // print the list // some other functions } Suppose you are to the implementing an operation on the list to make all the terms positive. Thus, if L is:, L.make_positive() would change L into

15
Code for make_positive will look like this: void make_positive() { Node * temp = first; while (first != null) temp->key = change(temp->key); } Code for change is: int change(int x) { if (x >= 0) return x; else return –x; } We want to put change in the class List. But change is not applied to the List object. In fact, it does not have anything to do with the List objects. Such functions are called static.

16
Static function – syntax for calling Suppose C is a class, d is a data item (field) of C, f is a function that belongs to class C: C x; // declare x as an object of type C y = x.d // access the field d of x z = x.f() // calling the function f Suppose C is a class, d is a static item of C, and f is a static function of C: Inside C: y = d // just refer to d without a qualifier object preceding it. Z = f() //same for a function call.

17
Outside C: y = C::d //Use the class name for scope resolution. Z = C::f() //same for a function call.

18
Code for build static set build(int a[], int s) { if (s == 0) { set x(1); // set constructor shown in the next slude x.mems[0] = new List(); // list constructor also shown next x.size = 1; return x; } else { set L1 = build(a,s-1); set L2 = L1.copy(); int s = L2.size; for (int k = 0; k < s; ++k) L2.mems[k]->insert(a[s-1]); set L = merge(L1, L2); // merge is also static return L; } };

19
Constructors for set and List set(int n) { size = n; for (int j=0; j < n; ++j) mems[j] = null; } public: List() { first = 0; }

20
Main function for subsets construction int main() { int s; cout << "Enter the size of the set." << endl; cin >> s; int a[s]; cout << "Enter the elements of the set." << endl; for (int j=0; j < s; ++j) cin >> a[j]; set temp = set::build(a, s); temp.print(); } Full code can be accessed from the lab web page.

21
Algorithm for permutation problem build(input array A, int n, int k) { if (n == 1) // same as for combination else { temp = build(A, n-1, k); temp1 = build(A, n-1, k-1); tempf = copy(temp1); for j from 1 to k-1 do { temp2 = copy(temp1); insert A[k-1] at position j of each list in temp2; tempf = merge(tempf, temp2); } return tempf; }

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google