Rossella Lau Lecture 4, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 4: C++ and list  Usage of Vector and List  C++

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

CSE Lecture 12 – Linked Lists …
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Data Structures: A Pseudocode Approach with C
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Rossella Lau Lecture 5, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque.
Rossella Lau Lecture 6, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 6: Algorithms and performance analysis  Algorithms.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
Rossella Lau Lecture 9, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 9: More on BST  Removal of a BST  Some advanced.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
Rossella Lau Lecture 2, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Rossella Lau Lecture 11, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 11: Queue & Priority Queue  Basic operations.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Data Structures Using C++ 2E
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Generic Programming Using the C++ Standard Template Library.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
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(
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure and Algorithms
Standard Template Library (STL)
Chapter 4 Linked Lists.
Linked lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Dynamic Memory Allocation
Chapter 18: Linked Lists.
Indirection.
Chapter 17: Linked Lists.
Standard Template Library
Linked lists.
Presentation transcript:

Rossella Lau Lecture 4, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 4: C++ and list  Usage of Vector and List  C++ reference  STL’s list and its usage -- By Rossella Lau

Rossella Lau Lecture 4, DCO20105, Semester A, vs (or array) Vector (or array)List Adding/removing elements fast when appending while there is still room or deleting the last element fast when adding/deleting an element in any position if the position is set Accessingcan be sorted and accessed by efficient method such as binary search no random access, should be linear search Memory allocation/de- allocation Actions are only required when the array needs to be re-sized Actions are required for each add/delete operation on a list

Rossella Lau Lecture 4, DCO20105, Semester A, Notes on memory allocation  In theory, list is efficient for applications in which storage is variant and insert/remove operations are required  However, memory allocation or de-allocation is a very expensive operation  It involves lots of steps to look for storage, computing positions of occupied or free memory  In the following discussion, we assume that we have an efficient memory allocation method

Rossella Lau Lecture 4, DCO20105, Semester A, Application considerations  In bookShop  Sales: only append, no update, no search  List  Catalog: one load, no update, search frequently  Sorted vector with binary search  Order: volume is variant, search is frequently  List: append easily, but can only apply linear search Vector: ordered or random? may apply binary search but needs shift operations for “insert”

Rossella Lau Lecture 4, DCO20105, Semester A, C++ pointer considerations on list Indirect identification of a part  Each part of a node must be identified through a node, and usually a pointer: e.g.,  ptr  item  ptr  next itemnext ptr

Rossella Lau Lecture 4, DCO20105, Semester A, ptr  next  Consider it in an assignment operation  When it is on the left hand side, it is going to be assigned a value to point to another node E.g., ptr->next = node2; changes ptr->next pointing to node2  When it is on the right hand side, it represents the next nodes on the linked list; e.g., ptr=ptr  next changes ptr pointing to node1 itemnext ptr item next node2 item next node1

Rossella Lau Lecture 4, DCO20105, Semester A, C++ reference  Using C++ reference can make an alias for ptr  next  It allows for a simpler coding for insert_before() and insert_after() with find()

Rossella Lau Lecture 4, DCO20105, Semester A, Reference re-visit  To simplify pointer operation in C, C++ supports one more form to represent an object: reference  To define a reference; e.g., int & refA = a; or Product & refP = p; where a and p are ordinary variables in a program  A reference must reference to another object and can not reference to null value

Rossella Lau Lecture 4, DCO20105, Semester A, Use of reference  A reference internally is an address, same as a pointer, but it can be used as the way of a real object/datum; i.e., it does not require dereferencing; e.g., refA = b; // assign b to a refP.getCode();  It can also give a “name” to a dynamic area: e.g., Node & refNode = * (new Node);

Rossella Lau Lecture 4, DCO20105, Semester A, Example of reference  What is the output?  int a, b;  int & refA = a;  Node& aNode = * (new Node);  Cin >> b; //input 15  a = 25;  refA = b;  aNode.item = refA;  cout << aNode.item;

Rossella Lau Lecture 4, DCO20105, Semester A, Reference of ptr->next  Node * & target = ptr  next;  target = node2;  ptr = target itemnext ptr item next node2 item next node1 target

Rossella Lau Lecture 4, DCO20105, Semester A, Interesting reference  Since a function can return a reference, a function call can be on the left hand side of an assignment:  int & min(int &lhs, &rhs) { return a < b? a : b; }  ……  int a, b;  cin >> a >> b;  min(a, b) += 1;

Rossella Lau Lecture 4, DCO20105, Semester A,  The declaration of Node * & is a reference of a pointer which can name a part of a node; e.g., Node *& target = nodePtr->next;  Since the part can be directly referred, find(), in List.h, allows for the found part to have an alias name  The name itself can be assigned a value, i.e., pointing to another node; i.e., link to another node or linked list  The name itself also represents a value which points to the next node  With find(), insert() can be as easy as: target = new Node ( item, target); Node *& find() item next targetV

Rossella Lau Lecture 4, DCO20105, Semester A, C++ notes on destructor  When designing destructor for friend classes, it should be careful to determine which destructor is responsible for memory de-allocation.  If the de-allocation places the destructor in Node, chain effects may accidentally happen  Because de-allocating a node would cause the destructor of another node (next) to be executed!

Rossella Lau Lecture 4, DCO20105, Semester A, STL’s list  It is a doubly linked list Ford’s slide 6:3  Operations on a doubly linked list: Ford’s slide: 9:12-16

Rossella Lau Lecture 4, DCO20105, Semester A, Methods of STL’s list  Method names of different containers in the STL are identical push_front(), push_back() front(), back() pop_front(), pop_front() list ::iterator is provided (forward/backward only) list ::const_iterator is for traversal on constant objects size() Syntax and other methods: Ford: 6: 6-13 Operator overload operations: 6:14  Header file is needed

Rossella Lau Lecture 4, DCO20105, Semester A, Sample usage of STL’s list  Ford’s slide: 6:15 insert()   Ford’s slide 6:16 erase()   Ford’s review exercises: 1,2,5,6,7 front List object (after) front rear List object (before) rear iter ??

Rossella Lau Lecture 4, DCO20105, Semester A, Ordered list  Ford: 6:17  The list has an order  Each insert operation should maintain the order  Linear search can be faster to stop for items not found on the list  Sample programs: d_listl.h and prg6_3.cpp

Rossella Lau Lecture 4, DCO20105, Semester A, Application considerations re-visit  For the container “order”  Will it use an ordered list better?

Rossella Lau Lecture 4, DCO20105, Semester A, Summary  Efficiency of operations on linked list and array depends  Reference is a more convenient way to use address than pointer  STL’s list is a doubly linked list and the operations names (method) are the same as what they are in the vector

Rossella Lau Lecture 4, DCO20105, Semester A, Reference  Ford: 6.2-3, 9.1-3,5  STL online references    Example programs: List.h, TestList.cpp, d_listl.h, and prg6_3.cpp -- END --