+ All Categories
Home > Documents > CS 240: Data Structures Thursday, July 12 th Lists, Templates, Vector, Algorithms.

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

Date post: 22-Dec-2015
Category:
View: 214 times
Download: 0 times
Share this document with a friend
22
CS 240: Data CS 240: Data Structures Structures Thursday, July 12 Thursday, July 12 th th Lists, Templates, Vector, Lists, Templates, Vector, Algorithms Algorithms
Transcript

CS 240: Data StructuresCS 240: Data Structures

Thursday, July 12Thursday, July 12thth

Lists, Templates, Vector, Lists, Templates, Vector, AlgorithmsAlgorithms

To do:To do:

We need to select We need to select groups for: Linked List groups for: Linked List Presentations.Presentations.

I think I’m going to I think I’m going to make this due later make this due later than what is on the than what is on the website, probably the website, probably the next Tuesday.next Tuesday.

Back to listsBack to lists

This is a representation of our array-based This is a representation of our array-based linked list.linked list.

We are going to go back to the code we were We are going to go back to the code we were working on Tuesday.working on Tuesday.

Index:Index: 00 11 22 33 44 55 66 77 88 99

Address:Address: i i i+4i+4 i+8i+8 i+12i+12 i+16i+16 i+20i+20 i+24i+24 i+28i+28 i+32i+32 i+36i+36

Value:Value: 1010 2020 3030 1010 5050 1010 00 ?? ?? ??

Next:Next: i+12i+12 i+24i+24 i+16i+16 i+8i+8 00 i+4i+4 ii ?? ?? ??

Back to listsBack to lists

Some changes:Some changes: Since we have an array based linked list, we can use Since we have an array based linked list, we can use

indices to access our data. indices to access our data. We will use indices instead of memory addresses – We will use indices instead of memory addresses –

a real linked list won’t use indices.a real linked list won’t use indices.

Index:Index: 00 11 22 33 44 55 66 77 88 99

Address:Address: i i i+4i+4 i+8i+8 i+12i+12 i+16i+16 i+20i+20 i+24i+24 i+28i+28 i+32i+32 i+36i+36

Value:Value: 1010 2020 3030 1010 5050 1010 00 ?? ?? ??

Next:Next: i+12i+12 i+24i+24 i+16i+16 i+8i+8 00 i+4i+4 ii ?? ?? ??

IndicesIndices

In this case, NULL would be a valid next In this case, NULL would be a valid next location, so we use -1 to indicate that we are at location, so we use -1 to indicate that we are at the end of the list.the end of the list.

first = 5first = 5

Index:Index: 00 11 22 33 44 55 66 77 88 99

Address:Address: i i i+4i+4 i+8i+8 i+12i+12 i+16i+16 i+20i+20 i+24i+24 i+28i+28 i+32i+32 i+36i+36

Value:Value: 1010 2020 3030 1010 5050 1010 00 ?? ?? ??

Next:Next: i+12i+12 i+24i+24 i+16i+16 i+8i+8 00 i+4i+4 ii ?? ?? ??

Next:Next: 33 66 44 22 -1-1 11 00 ?? ?? ??

New nodes?New nodes?

When we want to insert data we need to find an When we want to insert data we need to find an available node.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:Index: 00 11 22 33 44 55 66 77 88 99

Value:Value: 1010 2020 3030 1010 5050 1010 00 ?? ?? ??

Next:Next: 33 66 44 22 -1-1 11 00 ?? ?? ??

Finding new nodesFinding new nodes

One possibility:One possibility: Free list: All unallocated nodes are part of a listFree 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 When we want to get rid of a node, we add it to the

Free list.Free list.

Index:Index: 00 11 22 33 44 55 66 7-Free7-Free 88 99

Value:Value: 1010 2020 3030 1010 5050 1010 00 ?? ?? ??

Next:Next: 33 66 44 22 -1-1 11 00 88 99 -1-1

Finding new nodesFinding 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 An alternative is two create an array with the valid bits to avoid

adding them to Node.adding them to Node.

Index:Index: 00 11 22 33 44 55 66 77 88 99

Value:Value: 1010 2020 3030 1010 5050 1010 00 ?? ?? ??

Next:Next: 33 66 44 22 -1-1 11 00 ?? ?? ??

Valid:Valid: 11 11 11 11 11 11 11 00 00 00

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

Linked List DifferencesLinked List Differences

Linked list is Linked list is similar to the similar to the array-based list. array-based list. But, there are But, there are differences:differences:

Data allocation/deallocation is Data allocation/deallocation is done on a per-node basis.done on a per-node basis.

Node pointers are our only Node pointers are our only method for operating on the list.method for operating on the list.

On each insert, we need to On each insert, we need to create a new node and refer to create a new node and refer to it.it.

On each remove, we need to On each remove, we need to delete a node.delete a node.

DifferencesDifferences

We no longer need We no longer need capacity. capacity.

Our destructor needs to Our destructor needs to delete each individual delete each individual node.node.

Our copy Our copy constructor/assignment constructor/assignment requires list traversal.requires list traversal.

We don’t need a method to We don’t need a method to get an empty node, nor get an empty node, nor resize.resize.

QueuesQueues

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

StacksStacks

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

VectorVector

Vector is an STL provided sequential Vector is an STL provided sequential container.container.

It provides us with similar abilities as does It provides us with similar abilities as does our templated mycontainer (lab 5).our templated mycontainer (lab 5).

VectorVector

We declare a vector just like we do a templated We declare a vector just like we do a templated mycontainer:mycontainer: vector<T> testvector;vector<T> 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)

VectorVector

We can access Vector data as follows:We can access Vector data as follows: front() //gets first elementfront() //gets first elementback() //gets last elementback() //gets last elementoperator [unsigned int] //gets element at operator [unsigned int] //gets element at

specified location.specified location.

VectorVector Instead of currentvalue, Vector uses iterators:Instead of currentvalue, Vector uses iterators:

vector<T>::iterator myiterator; //T must match the vector<T>::iterator myiterator; //T must match the vector you want to use this iterator with.vector you want to use this iterator with.

myiterator = testvector.begin();myiterator = testvector.begin(); myiterator = testvector.end();myiterator = testvector.end(); myiterator++;myiterator++; //equivalent to mycontainer::next()//equivalent to mycontainer::next() myiterator--;myiterator--; //equivalent to mycontainer::previous()//equivalent to mycontainer::previous() *myiterator;*myiterator; //equivalent to mycontainer::current()//equivalent to mycontainer::current() testvector.erase(myiterator); //equivalent to testvector.erase(myiterator); //equivalent to

mycontainer::removeHere();mycontainer::removeHere(); testvector.insert(myiterator, T); //equivalent to testvector.insert(myiterator, T); //equivalent to

mycontainer::insertHere(T);mycontainer::insertHere(T);

Algorithm EfficiencyAlgorithm Efficiency

What determines if an algorithm is What determines if an algorithm is efficient?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 We usually worry about time when we discuss efficiency – however, space discuss efficiency – however, space issues are also important!issues are also important!

Time efficiencyTime efficiency

The time an algorithm takes has many The time an algorithm takes has many variables:variables:Size of data setSize of data setProcessing speedProcessing speedCompiler optimizations, effective codingCompiler optimizations, effective coding

Time EvaluationTime Evaluation

We could count how many instructions are We could count how many instructions are executed.executed.

Let T(n) represent the time it takes for an Let T(n) represent the time it takes for an algorithm to handle a data size of size n.algorithm to handle a data size of size n.

How long does insert() take?How long does insert() take?

Time EvaluationTime Evaluation

What about taking an What about taking an average?average?

How does this vary based How does this vary based on SIZE?on SIZE?

SIZE has a direct effect SIZE has a direct effect on the performance of on the performance of this algorithm!this algorithm!

//float array[SIZE] is filled //float array[SIZE] is filled with datawith data

float sum = 0;float sum = 0;

for(int i=0;i<SIZE;i++)for(int i=0;i<SIZE;i++)

{{

sum += array[i];sum += array[i];

}}

float average = sum/SIZE;float average = sum/SIZE;

Time EvaluationTime Evaluation We refer to this as an We refer to this as an

“order of magnitude” -> “order of magnitude” -> Big Oh, or O()Big Oh, or O()

In this case, the algorithm In this case, the algorithm is O(N), where N is the is O(N), where N is the input size.input size.

Math:Math: We say that T(N) has an We say that T(N) has an

order of magnitude of order of magnitude of O(f(X)) where,O(f(X)) where,

T(N) <= Cf(X), for some int T(N) <= Cf(X), for some int constant C, a sufficiently constant C, a sufficiently large N and f(X) in terms of large N and f(X) in terms of N and minimized.N and minimized.

f(X) = N, C >= 2f(X) = N, C >= 2

//float array[SIZE] is filled //float array[SIZE] is filled with datawith data

float sum = 0;float sum = 0;for(int i=0;i<SIZE;i++)for(int i=0;i<SIZE;i++){{

sum += array[i];sum += array[i];}}float average = sum/SIZE;float average = sum/SIZE;


Recommended