Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.

Slides:



Advertisements
Similar presentations
COMP171 Fall 2005 Lists.
Advertisements

Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
Lists: An internal look
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Data Structures Data Structures Topic #7. Today’s Agenda How to measure the efficiency of algorithms? Discuss program #3 in detail Review for the midterm.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Data Structures Data Structures Topic #5. Today’s Agenda Other types of linked lists –discuss algorithms to manage circular and doubly linked lists –should.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
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.
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.
Chapter 3: Arrays, Linked Lists, and Recursion
1 Data Structures Topic #1 Welcome !. 2 Today’s Agenda Introduction...what to expect!?! Talk about our Goals and Objectives Textbook is highly recommended.
Data Structures Using C++ 2E
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
C++ Classes and Data Structures Jeffrey S. Childs
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
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.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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 © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Linked List by Chapter 5 Linked List by
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Data Structure By Amee Trivedi.
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists
LINKED LISTS CSCD Linked Lists.
Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Introduction to C++ Linear Linked Lists
Programming II (CS300) Chapter 07: Linked Lists
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Presentation transcript:

Data Structures Topic #3

Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated by “position” Use of Data Structures for Ordered Lists –arrays (statically & dynamically allocated) –linear linked lists

Ordered Lists Now that we have seen a simple list example, and started to examine how we can use different data structures to solve the same problem We will move on to examine an ordered list ADT –implemented using a linear linked list, circular linked list, linked list of linked lists, doubly linked lists, etc.

Ordered Lists So, what is an ordered list? Immediately, a sorted list comes to mind But...this isn’t the only definition! –think of lists ordered by time, –grocery lists? In the order you think about it –to-do lists? In priority order... In fact, typically an ordered list is: –ordered by “position” –whereas a “sorted” list is a “value oriented list”, this is a “position oriented list”

Ordered Lists But, an ordered list has many many different interpretations We will discuss two: –absolute lists –relative lists Why? Well, there are a variety of interpretations of where data is inserted and whether or not the list has “holes”

Absolute Ordered Lists An absolute ordered list –may have holes –which means if the first data inserted is at position 12, there are 11 holes (1-11) prior –may “replace” data if inserted at the same position (another insert at position 12 could replace the previously inserted data at that position...it never shifts the data) –similar to “forms” such as a tax form

Relative Ordered Lists A relative ordered list –may not have holes –which means if the first data inserted is at position 12, it would actually be inserted at the first position! –may “shift” data if inserted at the same position (another insert at position 1 would shift what had been at position 1 -- now to be at position 2) –similar to “editors” such as vi

Absolute List Operations For an absolute ordered list, what are the operations? –insert, retrieve, remove, create, destroy, display –insert, retrieve, and remove would all require the client program to supply a position number –notice we are not inserting in sorted order, or retrieving by a “value” –instead we insert at an absolute position, retrieve the data at that position, remove data at a given position --- not affecting the rest of the list!

Relative List Operations For a relative ordered list, what are the operations?(the same!) –insert, retrieve, remove, create, destroy, display –insert, retrieve, and remove would all require the client program to supply a position number –instead we insert at a relative position, retrieve the data at that position, remove data at a given position --- this time affecting the rest of the list! –A remove at position 1 would require every other piece of data to shift down (logically)

Absolute/Relative Operations Notice what was interesting about the last two slides The operations for a relative and absolute list are the same One exception is that a relative list might also have an “append” function And, an absolute list might restrict insert from “replacing”, and add another function to specifically “replace”

Client Interface Therefore, the client interface for these two interpretations might be identical! –so...how would the application writer know which type of list the ADT supports? –Documentation! Critical whenever you implement a list –What does it mean to insert? Where? –What implications does insert and remove have on the rest of the data in the list?

Client Interface class ordered_list { public: ordered_list(); ~ordered_list(); int insert(int, const data & ); int retrieve(int, data &); int display(); int remove(int);

Client Interface With the previous class public section –the constructor might be changed to have an integer argument if we were implementing this abstraction with an array –the int return types for each member function represent the “success/failure” situation; if more than two states are used (to represent the error- code) ints are a good choice; otherwise, select a bool return type

Data Structures Now let’s examine various data structures for an ordered list and discuss the efficiency tradeoffs, based on: –run-time performance –memory usage We will examine: –statically/dynamically allocated arrays –linked lists (linear, circular, doubly)

Data Structures Statically Allocated array... private: data array[SIZE]; int number_of_items; Absolute lists: –direct access (insert at pos12 :array[11] =... –remove only alters one element (resetting it?) –problem: memory limitations (fixed size) –problem: must “guess” at the SIZE at compile time

Data Structures Relative lists: (statically allocated arrays) –direct access for retrieve –problem: searching! Insert might cause the data to be inserted somewhere other than what the position specifies (i.e., if the position # is greater than the next “open” position) –problem: shifting! Remove, insert alters all subsequent data –problem: memory limitations (fixed size) –problem: must “guess” at the SIZE at compile time

Data Structures Dynamically Allocated array... private: data * array; int number_of_items; int size_of_array; Absolute lists: –direct access (insert at pos12 :array[11] =... –remove only alters one element (resetting it?) –problem: memory limitations (fixed size)

Data Structures Relative lists: (dynamically allocated arrays) –direct access for retrieve –problem: searching for the correct position for insert –problem: shifting with insert and remove –problem: memory limitations (fixed size)

Data Structures What this tells us is that a dynamically allocated list is better than a statically allocated list (one less problem) –if the cost of memory allocation for the array is manageable at run-time. –may not be reasonable if a large quantity of instances of an ordered_list are formed –is not required if the size of the data is known up-front at compile time (and is the same for each instance of the class)

Data Structures We also should have noticed from the previous discussion that... –absolute ordered list are well suited for array implementations, since they are truly direct access abstractions –relative ordered list are rather poorly suited for arrays, since they require that data be shifted therefore, hopefully the array consists of pointers to our data, so that at least when we shift we are only moving pointers rather than the actual data!!

Data Structures Linear Linked list... private: node * head; node * tail; //???helpful? Absolute lists: (a poor choice) –holes: how to deal with them? add a position number to the contents of each node....don’t really allocate nodes for each hole!!!! –insert, retrieve, removal requires traversal –how can a tail pointer help? if data is entered in order by position!

Data Structures Relative lists: (linear linked lists) –no holes -- so no extra position needed in each node –insert, retrieve, remove requires traversal –a tail pointer assists if appending at the end –no shifting!! So, while we still have to “search”, and the search may be more expensive than with an array -- this is greatly improved for a relative list, since there is not shifting!!

Data Structures Circular Linked list... private: node * head; node * tail; //???helpful? There is nothing in an ordered list that will benefit from the last node pointing to the first node A circular linked list will require additional code to manage, with no additional benefits

Data Structures Doubly Linked list... private:(each node has a node * prev) node * head; node * tail; //???helpful? Again, there is nothing in an ordered list that will benefit from each node having a pointer to the previous node. UNLESS, there were operations to backup or go forward from the “current” position. In this case a doubly linked list would be ideal

Data Structures What about a linked list of arrays –where every n items are stored in the first node, the next n items are stored in the second node, etc. –This still requires traversal to get to the right node, but then from there direct access can be used to insert, retrieve, remove the data –May be the best of both worlds for relative lists, limiting the amount of shifting to “n” items while at the same time keeping the traversal to a manageable level

Data Structures Are there other alternatives? –How about an array of linked lists? –How about “marking” data in a relative list as “empty” to avoid shifting with an array?! Given the data structures discussed, which is best and why for: –absolute ordered list –relative ordered list

Next Time... Now that we have applied data structures to an ordered list We will move on to examine stack and queue abstractions Again, we will examine them from the standpoint of arrays, linked list, circular linked list, linked list of linked lists, doubly linked lists, etc. beginning next time!

Data Structures Programming Assignment Discussion