Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240: Data Structures Thursday, July 12 th Lists, Templates, Vector, Algorithms.

Similar presentations


Presentation on theme: "CS 240: Data Structures Thursday, July 12 th Lists, Templates, Vector, Algorithms."— Presentation transcript:

1 CS 240: Data Structures Thursday, July 12 th Lists, Templates, Vector, Algorithms

2 To do: We need to select groups for: Linked List Presentations. We need to select groups for: Linked List Presentations. I think I’m going to make this due later than what is on the website, probably the next Tuesday. I think I’m going to make this due later than what is on the website, probably the next Tuesday.

3 Back to lists This is a representation of our array-based linked list. This is a representation of our array-based linked list. We are going to go back to the code we were working on Tuesday. We are going to go back to the code we were working on Tuesday. Index:0123456789 Address:ii+4i+8i+12i+16i+20i+24i+28i+32i+36 Value:1020301050100??? Next:i+12i+24i+16i+80i+4i???

4 Back to lists Some changes: Some changes: Since we have an array based linked list, we can use indices to access our data. Since we have an array based linked list, we can use indices to access our data. We will use indices instead of memory addresses – a real linked list won’t use indices. We will use indices instead of memory addresses – a real linked list won’t use indices. Index:0123456789 Address:ii+4i+8i+12i+16i+20i+24i+28i+32i+36 Value:1020301050100??? Next:i+12i+24i+16i+80i+4i???

5 Indices In this case, NULL would be a valid next location, so we use -1 to indicate that we are at the end of the list. In this case, NULL would be a valid next location, so we use -1 to indicate that we are at the end of the list. first = 5 first = 5 Index:0123456789 Address:ii+4i+8i+12i+16i+20i+24i+28i+32i+36 Value:1020301050100??? Next:i+12i+24i+16i+80i+4i??? Next:364210???

6 New nodes? When we want to insert data we need to find an available node. When we want to insert data we need to find an available node. Special case: Special case: There are no nodes left: Resize the array! There are no nodes left: Resize the array! But, how do we find an empty node? But, how do we find an empty node? Index:0123456789 Value:1020301050100??? Next:364210???

7 Finding new nodes One possibility: One possibility: Free list: All unallocated nodes are part of a list Free list: All unallocated nodes are part of a list Therefore, we are maintaining two lists. Therefore, we are maintaining two lists. If we want a node, we take it from the Free list. If we want a node, we take it from the Free list. When we want to get rid of a node, we add it to the Free list. When we want to get rid of a node, we add it to the Free list. Index:01234567-Free89 Value:1020301050100??? Next:36421089

8 Finding new nodes Another possibility: Another possibility: Valid bits: All nodes have a bit that tells us if the node is in use. Valid bits: All nodes have a bit that tells us if the node is in use. This adds data to Node. This adds data to Node. We can scan the array for a Node with Node::Valid==0. We can scan the array for a Node with Node::Valid==0. We get rid of a node by setting its Valid bit to 0. We get rid of a node by setting its Valid bit to 0. An alternative is two create an array with the valid bits to avoid adding them to Node. An alternative is two create an array with the valid bits to avoid adding them to Node. Index:0123456789 Value:1020301050100??? Next:364210??? Valid:1111111000

9 Back to coding…. Back to coding…. Optional: Code insertion sort. Optional: Code insertion sort. Optional: Swapping of list elements Optional: Swapping of list elements

10 Linked List Differences Linked list is similar to the array-based list. But, there are differences: Linked list is similar to the array-based list. But, there are differences: Data allocation/deallocation is done on a per-node basis. Data allocation/deallocation is done on a per-node basis. Node pointers are our only method for operating on the list. Node pointers are our only method for operating on the list. On each insert, we need to create a new node and refer to it. On each insert, we need to create a new node and refer to it. On each remove, we need to delete a node. On each remove, we need to delete a node.

11 Differences We no longer need capacity. We no longer need capacity. Our destructor needs to delete each individual node. Our destructor needs to delete each individual node. Our copy constructor/assignment requires list traversal. Our copy constructor/assignment requires list traversal. We don’t need a method to get an empty node, nor resize. We don’t need a method to get an empty node, nor resize.

12 Queues Queues can be implemented from a linked list. Queues can be implemented from a linked list. We need to change insert to enqueue We need to change insert to enqueue remove -> dequeue remove -> dequeue No operator[], but we do get peek. No operator[], but we do get peek.

13 Stacks Stacks can also be implemented from a linked list. Stacks can also be implemented from a linked list. We need to change insert to push We need to change insert to push remove -> pop remove -> pop No operator[], but we do get peek. No operator[], but we do get peek.

14 Vector Vector is an STL provided sequential container. Vector is an STL provided sequential container. It provides us with similar abilities as does our templated mycontainer (lab 5). It provides us with similar abilities as does our templated mycontainer (lab 5).

15 Vector We declare a vector just like we do a templated mycontainer: We declare a vector just like we do a templated mycontainer: vector testvector; vector testvector; Many methods are built in: Many methods are built in: Constructor, destructor, operator = Constructor, destructor, operator = size(), capacity(), size(), capacity(), clear() //equivalent to mycontainer::empty() clear() //equivalent to mycontainer::empty() push_back(T) //equivalent to mycontainer::insert(T) push_back(T) //equivalent to mycontainer::insert(T) pop_back(T) //equivalent to mycontainer::remove(T) pop_back(T) //equivalent to mycontainer::remove(T)

16 Vector We can access Vector data as follows: We can access Vector data as follows: front() //gets first element front() //gets first element back() //gets last element back() //gets last element operator [unsigned int] //gets element at specified location. operator [unsigned int] //gets element at specified location.

17 Vector Instead of currentvalue, Vector uses iterators: Instead of currentvalue, Vector uses iterators: vector ::iterator myiterator; //T must match the vector you want to use this iterator with. vector ::iterator myiterator; //T must match the vector you want to use this iterator with. myiterator = testvector.begin(); myiterator = testvector.begin(); myiterator = testvector.end(); myiterator = testvector.end(); myiterator++;//equivalent to mycontainer::next() myiterator++;//equivalent to mycontainer::next() myiterator--;//equivalent to mycontainer::previous() myiterator--;//equivalent to mycontainer::previous() *myiterator;//equivalent to mycontainer::current() *myiterator;//equivalent to mycontainer::current() testvector.erase(myiterator); //equivalent to mycontainer::removeHere(); testvector.erase(myiterator); //equivalent to mycontainer::removeHere(); testvector.insert(myiterator, T); //equivalent to mycontainer::insertHere(T); testvector.insert(myiterator, T); //equivalent to mycontainer::insertHere(T);

18 Algorithm Efficiency What determines if an algorithm is efficient? What determines if an algorithm is efficient? How much space does it take up? How much space does it take up? How long does it take? How long does it take? We usually worry about time when we discuss efficiency – however, space issues are also important! We usually worry about time when we discuss efficiency – however, space issues are also important!

19 Time efficiency The time an algorithm takes has many variables: The time an algorithm takes has many variables: Size of data set Size of data set Processing speed Processing speed Compiler optimizations, effective coding Compiler optimizations, effective coding

20 Time Evaluation We could count how many instructions are executed. We could count how many instructions are executed. Let T(n) represent the time it takes for an algorithm to handle a data size of size n. Let T(n) represent the time it takes for an algorithm to handle a data size of size n. How long does insert() take? How long does insert() take?

21 Time Evaluation What about taking an average? What about taking an average? How does this vary based on SIZE? How does this vary based on SIZE? SIZE has a direct effect on the performance of this algorithm! SIZE has a direct effect on the performance of this algorithm! //float array[SIZE] is filled with data float sum = 0; for(int i=0;i<SIZE;i++) { sum += array[i]; } float average = sum/SIZE;

22 Time Evaluation We refer to this as an “order of magnitude” -> Big Oh, or O() We refer to this as an “order of magnitude” -> Big Oh, or O() In this case, the algorithm is O(N), where N is the input size. In this case, the algorithm is O(N), where N is the input size. Math: Math: We say that T(N) has an order of magnitude of O(f(X)) where, We say that T(N) has an order of magnitude of O(f(X)) where, T(N) <= Cf(X), for some int constant C, a sufficiently large N and f(X) in terms of N and minimized. T(N) <= Cf(X), for some int constant C, a sufficiently large N and f(X) in terms of N and minimized. f(X) = N, C >= 2 f(X) = N, C >= 2 //float array[SIZE] is filled with data float sum = 0; for(int i=0;i<SIZE;i++) { sum += array[i]; } float average = sum/SIZE;


Download ppt "CS 240: Data Structures Thursday, July 12 th Lists, Templates, Vector, Algorithms."

Similar presentations


Ads by Google