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

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
CHP-5 LinkedList.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Programming and Data Structure
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
CS 240: Data Structures Thursday, June 21 th Lists – Array based, Link based Dynamic vs Static.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CS 240: Data Structures Thursday, June 21 th Vector, Linked List.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Lists, Stacks, and Queues Abstract Data Types (ADTs) The List ADT The Stack ADT The Queue ADT.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Pointers OVERVIEW.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Stacks And Queues Chapter 18.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
 200 Total Points ◦ 75 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 35 Points Short Answer ◦ 30 Points Multiple Choice.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Cpt S 122 – Data Structures Abstract Data Types
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
CS 1114: Implementing Search
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stacks and Queues.
Programming Abstractions
Stack and Queue APURBO DATTA.
This pointer, Dynamic memory allocation, Constructors and Destructor
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Stacks, Queues, and Deques
Arrays and Linked Lists
Stacks, Queues, and Deques
Linked List (Part I) Data structure.
Further Data Structures
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Data Structures & Algorithms
Fundaments of Game Design
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
CS 2308 Final Exam Review.
Presentation transcript:

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

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.

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: Address:ii+4i+8i+12i+16i+20i+24i+28i+32i+36 Value: ??? Next:i+12i+24i+16i+80i+4i???

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: Address:ii+4i+8i+12i+16i+20i+24i+28i+32i+36 Value: ??? Next:i+12i+24i+16i+80i+4i???

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: Address:ii+4i+8i+12i+16i+20i+24i+28i+32i+36 Value: ??? Next:i+12i+24i+16i+80i+4i??? Next:364210???

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: Value: ??? Next:364210???

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: Free89 Value: ??? Next:

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: Value: ??? Next:364210??? Valid:

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

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.

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.

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.

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.

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

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)

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.

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);

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!

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

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?

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;

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;