Lecture 04: Sequences structures

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists.
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.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Lecture 7 : Intro. to STL (Standard Template Library)
List Interface and Linked List Mrs. Furman March 25, 2010.
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Standard Template Library
STACKS & QUEUES for CLASS XII ( C++).
Lecture 6 of Computer Science II
Unit – I Lists.
Week 4 - Monday CS221.
Pointers and Linked Lists
Pointers and Linked Lists
Copy Constructor / Destructors Stacks and Queues
4. Linked Lists.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
UNIT – I Linked Lists.
CE 221 Data Structures and Algorithms
Data Structure and Algorithms
Lists CS 3358.
John Hurley Cal State LA
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Problems with Linked List (as we’ve seen so far…)
Data Structures Interview / VIVA Questions and Answers
Collections Intro What is the STL? Templates, collections, & iterators
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Lists.
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Linked List Intro CSCE 121 J. Michael Moore.
Linked Lists.
Given the code to the left:
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Programming Abstractions
CS2013 Lecture 4 John Hurley Cal State LA.
Indirection.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
Lecture 8 : Intro. to STL (Standard Template Library)
Arrays an array of 5 ints is filled with 3,2,4,1,7
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Collections Intro What is the STL? Templates, collections, & iterators
Linked List Intro CSCE 121.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 3 – Data collection List ADT
Presentation transcript:

Lecture 04: Sequences structures Topics: Problems with arrays Linked Lists basics Linked List operations STL variants of the above Other variants (stacks and queues)

Part I: Array problems Goal or arrays and linked structures: Arrays: Represent an collection of data Arrays: Advantage: quick addressing Recall the pointer math involved Disadvantage: must be contiguous This applies regardless of whether it's on stack or heap Disadvantage: size is fixed Static arrays: fixed at compile time Dynamic arrays: fixed upon new call. We can re-allocate the array, but there is some overhead Linked Structures (for now, linked lists) are the reverse.

Part II: Linked structures Goal: Represent an collection of data Comparisons to arrays: Arrays have this same goal Disadvantage: can’t “jump” to a specific element Instead, we need to traverse the list Advantage: needn’t be contiguous Each element can go to a different spot (on the heap) Advantage: can grow until we run out of memory Once we have the correct spot to insert a new element, adds are very fast. Disadvantage: can lead to heap-fragmentation (the way we’ll implement it)

Linked List data structures class LLNode { // Put the data here (I call it the payload) double mData; // sample payload LLNode * mNext; // Put any delegated (recursive) calls here. }; class LList LLNode * mStart; // put methods here to add, remove, search, // etc. within the linked list. The interface

Manually creating a linked list Do the memory and logical diagrams on the board. int main() { LLNode * temp; // mPayload is an double. LList L; temp = new LLNode(); L.mStart = temp; temp->mPayload = 5; temp->mNext = new LLNode(); temp->mNext->mPayload = 10; temp = temp->mNext; // "Advances" temp temp->mNext = new LLNode() temp->mNext->mPayload = 15; temp = temp->mNext; temp->mNext = NULL; // Traverse the linked list temp = L.mStart; while (temp != NULL) cout << temp->mPayload << endl; }

Part III: Linked List operations Most applications of linked lists require that we are able to: Add new elements to a linked list At the end, beginning, middle, or "in-place" Remove elements of a linked list Find (by value or position) an element Size Two (at least…) approaches to each: Top-level: Let the LList class do the work, treat LLNode as a structure Often a bit faster, but less elegant Delegated: Let the LList class call a method of LLNode, which is where most of the work is done. A good example of recursion I’ll do some of both (just for practice)

IIIa: Add to Beginning (push_front) [Top-level approach] Assumption: We're given a payload value (Dynamically) create a new LLNode and set payload. Two cases: This is the first element The new element should be pointed to by mStart. It isn't. The mNext field of the new element should point to the old last element.

IIIb: Add to End (push_end) [Delegated approach] Allocate a new node as with push_front. Recursively calls this on the following node until we reach the end. Two cases at the top level: This is the first element in the linked list The new element should be pointed to by mStart. It isn't. We tell the existing first node to add the new node after itself (recursion) Two cases at the LLNode level: We have a node following us: tell them to add this new node after themselves. We don’t: attach it after us

IIIf. Find [Top-Level] Goal: Find a LLNode * Two approaches: Find by value. Find by index. [Add operator overloading] Bot work the same way: traverse the list until you find the requested “thing”. If you walk off the list, return NULL (or some error code)

IIIc. Insert [Delegated, non-recursive] Often used together with find. Find gets us a LLNode pointer We tell that LLNode to insert the given value after itself.

IIId. In-order [Top-Level] Set up a traversal pattern: Make a temporary pointer, copy the value of mStart to it. Look at this node and the one following. Two possibilities: We have no following node or the new node should go between the current and next node: attach the new node there. All other cases: move on to the next node.

IIIe. Remove [Top-Level] Indicate the “target” by value. Traverse to get the node before the one we want to remove. Why the one before? Re-route the pointers and call delete on the target node.

IIIg. [Hidden] Cleanup Since we’re using dynamic allocation, we must ensure delete is called. We want to trigger this in the Llist class’s destructor. Use a delegated approach to set up a cascading delete. Top level delete’s mStart. mStart deletes the thing after it. That deletes the thing after it. …

IIIh. Size [Delegated] If this node has a successor, return their result + 1. Otherwise, just return 1.

Part IV: STL lists I want you to do Lab4 “manually” But…the standard template library has a very nice linked list class. Works in many ways like vector’s, but internally it is a linked list (vectors are an array internally) Uses an iterator nested-class, which is basically a pointer to a LLNode structure.

STL List example #include <iostream> #include <list> #include <algorithm> // For the find function using namespace std; int main() { list<float> flist; list<string> slist; flist.push_back(14.3f); flist.push_front(7.9f); flist.push_back(15.6f); slist.push_back("ABC");

STL List Example, cont. //cout << flist[0] << endl; // Error! list<float>::iterator fiter; list<string>::iterator siter; for (fiter = flist.begin(); fiter != flist.end(); fiter++) cout << *fiter; // Find an element fiter = std::find(flist.begin(), flist.end(), 14.3f); // Insert an element after fiter flist.insert(fiter, -1.3f);

STL List Example, cont. // Remove all elements from flist that are // between 5.0 and 10.0 for (fiter = flist.begin(); fiter != flist.end(); ) { if (*fiter > 5.0 && *fiter < 10.0) fiter = flist.erase(fiter); } // Remove all elements from flist. flist.clear();

Part V: Other linked structures Many other data structures are based on [linked] lists Queues Stacks Doubly-Linked Lists [Talk about operations of each and some app’s]