The Bag and Sequence Classes with Linked Lists

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

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.
Introduction to Programming Lecture 39. Copy Constructor.
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
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.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
Prof. I. J. Chung Data Structure #4 Professor I. J. Chung.
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.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
Link-Based Implementations
Memory Management.
Chapter 16: Linked Lists.
Pointers and Dynamic Arrays
Lecture 6 of Computer Science II
Unit – I Lists.
C++ Programming:. Program Design Including
COMP 53 – Week Eight Linked Lists.
Pointers and Linked Lists
CSC212 Data Structure - Section BC
Pointers and Linked Lists
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
UNIT-3 LINKED LIST.
Data Structures and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
CSCE 210 Data Structures and Algorithms
Chapter 4 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 16-2 Linked Structures
Linked Lists.
Chapter 4 Link Based Implementations
Chapter 4 Linked Lists.
Arrays and Linked Lists
Chapter 18: Linked Lists.
Pointers and 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.
Indirection.
Data Structures and Algorithms
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
CSC212 Data Structure - Section RS
Data Structures & Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
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
Data Structures & Programming
Presentation transcript:

The Bag and Sequence Classes with Linked Lists CSC212 Data Structure Lecture 8 The Bag and Sequence Classes with Linked Lists Instructor: George Wolberg Department of Computer Science City College of New York Assignment 4 here

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

The Complete node Class Definition 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 The node class is fundamental to linked lists The private member variables data_field link_field The member functions include: A constructor Set data and set link Retrieve data and retrieve link Please read Section 5.1. for details about value_type() and two versions of the function link() Why TWO? p. 213-4

Reviews: Node and Linked List Linked Lists Traverse How to access the next node by using link pointer of the current node 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; }

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

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

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

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

Our Third Bag - Specification The documentation nearly identical to our previous bag The programmer uses the bag do not need to know know about linked lists. The difference No worries about capacity therefore no default capacity no reserve function because our new bag with linked list can grow or shrink easily!

Our Third Bag – Class Definition The invariant of the 3rd bag class 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. The Header File (code)

Our Third Bag – Class Definition How to match bag::value_type with node::value_type Following the rules for dynamic memory usage Allocate and release dynamic memory The law of the Big-Three class bag { public: typedef node::value_type value type; ...... } Whenever you define or change the definition of value_type in node, the value_type of bag will be the same For example #include <string> class node { public: typedef std::string value_type } When you use the value-type of the bag, always use bag::value_type

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

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

Sequence – Design Suggestions Five private member variables many_nodes: number of nodes in the list head_ptr and tail_ptr : the head and tail pointers of the linked list why tail_ptr - for attach when no current item cursor : pointer to the current item (or NULL) precursor: pointer to the item before the current item for an easy insert (WHY) Don’t forget the dynamic allocation/release the value semantics and the Law of the Big-Three

Sequence – Value Semantics Goal of assignment and copy constructor 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); Problems ( deep copy – new memory allocation) many_nodes OKAY head_ptr and tail_ptr OKAY How to set cursor and precursor ?

Dynamic Arrays vs Linked Lists Arrays are better at random access O (1) vs. O(n) Linked lists are better at insertions/ deletions at a cursor O(1) vs O(n) Doubly linked lists are better for a two-way cursor for example for insert O(1) vs. O(n) Resizing can be Inefficient for a Dynamic Array re-allocation, copy, release class dnote { public: typedef int value_type; ... private: value_type data; dnode *next; dnode *last; };

Reading and Programming Assignments Reading after Class Chapter 6 Programming Assignment 4 Detailed guidelines online!