Xiaoyan Li, 2007 1 CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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.
Data Structures: A Pseudocode Approach with C
@ Zhigang Zhu, CSC212 Data Structure - Section RS Lectures 6/7 Pointers and Dynamic Arrays Instructor: Zhigang Zhu Department of Computer Science.
Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department.
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
1 Linked Lists It’s a conspiracy!. 2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
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.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Edgardo Molina, CSC212 Data Structure - Section AB Lectures 6/7 Pointers and Dynamic Arrays Instructor: Edgardo Molina Department of Computer Science.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Edgardo Molina, CSC212 Data Structure - Section AB Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Edgardo Molina Department.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
Xiaoyan Li, CSC211 Data Structures Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Prof. Xiaoyan Li Department of Computer Science.
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structure & Algorithms
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.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Prof. I. J. Chung Data Structure #4 Professor I. J. Chung.
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.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
Pointers and Dynamic Arrays
Unit – I Lists.
C++ Programming:. Program Design Including
CSC212 Data Structure - Section BC
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 4 Linked Lists.
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
Chapter 4 Linked Lists
The Bag and Sequence Classes with Linked Lists
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 4 Linked Lists.
Arrays and Linked Lists
Chapter 18: Linked Lists.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
CSC212 Data Structure - Section RS
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
Presentation transcript:

Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Xiaoyan Li, Reviews: Node and Linked List p Node p a class with a pointer to an object of the node class p core structure for the linked list p two versions of the “link” functions p why and how? p

Xiaoyan Li, The Complete node Class Definition p The node class is fundamental to linked lists p The private member variables p data_field p link_field p The member functions include: p A constructor p Set data and set link p Retrieve data and retrieve link class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node( const value_type& init_data = value_type( ), node* init_link = NULL ) { data = init_data; link = init_link; } // Member functions to set the data and link fields: void set_data(const value_type& new_data) { data = new_data; } void set_link(node* new_link) { link = new_link; } // Constant member function to retrieve the current data: value_type data( ) const { return data; } // Two slightly different member functions to retrieve // the current link: const node* link( ) const { return link; } node* link( ) { return link;} private: value_type data; node* link; }; default argument given by the value_type default constructor Why TWO? p

Xiaoyan Li, Reviews: Node and Linked List p Linked Lists Traverse p How to access the next node by using link pointer of the current node p the special for loop size_t list_length(const node* head_ptr) { const node *cursor; size_t count = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) count++; return count; }

Xiaoyan Li, Reviews: Node and Linked List p Insert p Insert at the head p set the head_ptr and the link of the new node correctly p Insert at any location p cursor pointing to the current node p need a pre-cursor to point to the node before the current node (two approaches) p the third approach: doubly linked list

Xiaoyan Li, Reviews: Node and Linked List p Delete p Delete at the head p set the head_ptr correctly p release the memory of the deleted node p Delete at any location p cursor pointing to the current node p need a pre-cursor to point to the node before the current node (two approaches) p the third approach: doubly linked list

Xiaoyan Li, Key points you need to know p Linked List Toolkit uses the node class which has p set and retrieve functions p The functions in the Toolkit are not member functions of the node class p length, insert(2), remove(2), search, locate, copy,... p compare their Big-Os with similar functions for an array p They can be used in various container classes, such as bag, sequence, etc. Toolkit Code

Xiaoyan Li, Container Classes using Linked Lists p Bag Class with a Linked List p Specification p Class definition p Implementation p Testing and Debugging p Sequence Class with a Linked List p Design suggestion – difference from bag p Arrays or Linked Lists: which approach is better? p Dynamic Arrays p Linked Lists p Doubly Linked Lists

Xiaoyan Li, Our Third Bag - Specification p The documentation p nearly identical to our previous bag p The programmer uses the bag do not need to know know about linked lists. p The difference p No worries about capacity therefore p no default capacity p no reserve function p because our new bag with linked list can grow or shrink easily! // static const size_type CAPACITY = _____ // bag::CAPACITY is the maximum number of items (in Bag 1) // that a bag can hold. (in Bag 1) // static const size_type DEFAULT_CAPACITY = _____ (in Bag 2) // bag::DEFAULT_CAPACITY is the initial capatity of a bag //that is created by the default constructor. (in Bag 2)

Xiaoyan Li, Our Third Bag – Class Definition p The invariant of the 3 rd bag class p the items in the bag are stored in a linked list (which is dynamically allocated)  the head pointer of the list is stored in the member variable head_ptr of the class bag  The total number of items in the list is stored in the member variable many_nodes. p The Header File & implementation file (code) (bag1, bag2, bag3) code

Xiaoyan Li, Our Third Bag – Class Definition p How to match bag::value_type with node::value_type p Following the rules for dynamic memory usage p Allocate and release dynamic memory p The law of the Big-Three class bag { public: public: typedef node::value_type value type;......}

Xiaoyan Li, Our Third Bag - Implementation p The Constructors p default constructor p copy constructor p Overloading the Assignment Operator p release and re-allocate dynamic memory p self-assignment check p The Destructor p return all the dynamic memory to the heap p Other functions and the code code

Xiaoyan Li, void bag::operator =(const bag& source) // FUNCTIONS for the linked list toolkit std::size_t list_length(const node* head_ptr); void list_head_insert(node*& head_ptr, const node::value_type& entry); void list_insert(node* previous_ptr, const node::value_type& entry); node* list_search(node* head_ptr, const node::value_type& target); const node* list_search (const node* head_ptr, const node::value_type& target); node* list_locate(node* head_ptr, std::size_t position); const node* list_locate(const node* head_ptr, std::size_t position); void list_head_remove(node*& head_ptr); void list_remove(node* previous_ptr); void list_clear(node*& head_ptr); void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);

Xiaoyan Li, void bag::operator =(const bag& source) { node *tail_ptr; node *tail_ptr; list_clear(head_ptr); list_clear(head_ptr); many_nodes = 0; many_nodes = 0; list_copy(source.head_ptr, head_ptr, tail_ptr); list_copy(source.head_ptr, head_ptr, tail_ptr); many_nodes = source.many_nodes; many_nodes = source.many_nodes;}

Xiaoyan Li, void bag::operator =(const bag& source) { node *tail_ptr; // needed for argument to list_copy node *tail_ptr; // needed for argument to list_copy if (this == &source) // check for self-assignment if (this == &source) // check for self-assignment return; return; list_clear(head_ptr); list_clear(head_ptr); many_nodes = 0; many_nodes = 0; list_copy(source.head_ptr, head_ptr, tail_ptr); list_copy(source.head_ptr, head_ptr, tail_ptr); many_nodes = source.many_nodes; many_nodes = source.many_nodes;} How to implement the copy constructor? How to implement the destructor?

Xiaoyan Li, Sequence Class with Linked List p Compare three implementations p using a fixed size array (assignment 2) p using a dynamic array (assignment 3) p using a linked list (assignment 4) p What are the differences? p member variables p value semantics p Performance (time and space)

Xiaoyan Li, Sequence – Design Suggestions p Private member variables  many_nodes :  head_ptr  tail_ptr : p Why tail_ptr  cursor : pointer to the current item (or NULL)  precursor : pointer to the item before the current item p Why precursor? p Don’t forget p the dynamic allocation/release p the value semantics and p the Law of the Big-Three

Xiaoyan Li, Sequence – Value Semantics p Goal of assignment and copy constructor p make one sequence equals to a new copy of another  Can we just use list_copy in the Toolkit?  void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr); p

Xiaoyan Li, list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr); 1. Set head_ptr and tail_ptr to NULL 2. If (souce_ptr == NULL), then teturn with no futher work 3. Allocate a new node for the head node of the new list that we are creating. Make both head_ptr and tail_prt point to this new node, and copy data from the head node of the source list. 4. Make source_ptr point to the second node, third node, and so on. At each node that source_ptr points to, add one new node to the tail of the new list, let tail_ptr point to the newly added node.

Xiaoyan Li, list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr); { head_ptr = NULL; head_ptr = NULL; tail_ptr = NULL; tail_ptr = NULL; // Handle the case of the empty list. // Handle the case of the empty list. if (source_ptr == NULL) return; if (source_ptr == NULL) return; // Make the head node for the newly created list, and put data in it. // Make the head node for the newly created list, and put data in it. list_head_insert(head_ptr, source_ptr->data( )); list_head_insert(head_ptr, source_ptr->data( )); tail_ptr = head_ptr; tail_ptr = head_ptr; // Copy the rest of the nodes one at a time, adding at the tail of new list source_ptr = source_ptr->link( ); source_ptr = source_ptr->link( ); while (source_ptr != NULL) while (source_ptr != NULL) { list_insert(tail_ptr, { list_insert(tail_ptr, source_ptr->data( )); source_ptr->data( )); tail_ptr = tail_ptr->link( ); tail_ptr = tail_ptr->link( ); source_ptr = source_ptr->link( ); source_ptr = source_ptr->link( ); }}

Xiaoyan Li, Sequence – Value Semantics p Goal of assignment and copy constructor p make one sequence equals to a new copy of another  Can we just use list_copy in the Toolkit?  list_copy(source.head_ptr, head_ptr, tail_ptr); p Problems ( deep copy – new memory allocation) p many_nodes OKAY p head_ptr and tail_ptr OKAY p How to set cursor and precursor ? (you need to handle this in the fourth assignment)

Xiaoyan Li, Choose a dynamic array, a linked list, or a doubly linked list? Frequent random access operations Operations occur at a cursor Operations occur at a two- way cursor Frequent resizing may be needed

Xiaoyan Li, Choose a dynamic array, a linked list, or a doubly linked list? Frequent random access operations Use a dynamic array Operations occur at a cursor Use a linked list Operations occur at a two- way cursor Use a doubly linked list Frequent resizing may be needed Use a linked list

Xiaoyan Li, Dynamic Arrays vs Linked Lists p Arrays are better at random access p O (1) vs. O(n) p Linked lists are better at insertions/ deletions at a cursor p O(1) vs O(n) p Doubly linked lists are better for a two-way cursor p for example for insert O(1) vs. O(n) p Resizing can be Inefficient for a Dynamic Array p re-allocation, copy, release

Xiaoyan Li, Reading and Programming Assignments p Reading after Class p Chapter 6 —Templates and Iterators p Programming Assignment 4 p Detailed guidelines online already! p Breakdown of points: p (70 points) Basis points (passes the seq_ex3 test) p (5 points) invariant of the class p (10 points) running time analysis p (15 points) Other implementation details