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.

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

Stacks, Queues, and Linked Lists
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
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.
Review Learn about linked lists
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
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.
Chapter 4 ADT Sorted List.
Chapter 14: Overloading and Templates
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.
Data Structures Data Structures Topic #5. Today’s Agenda Other types of linked lists –discuss algorithms to manage circular and doubly linked lists –should.
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.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
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 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
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
Doubly Linked Lists Deleting from the end of the list – Have to traverse the entire list to stop right in front of tail to delete it, so O(n) – With head.
Review of C++ Programming Part II Sheng-Fang Huang.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Programming Techniques Classes II Important Class Features Spring 2009.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
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.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
LINKED LISTS.
CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Linked Lists Chapter 6 Section 6.4 – 6.6
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 4 Linked Lists.
Introduction to C++ Linear Linked Lists
Linked Lists.
Data Structures & Algorithms
Data Structures & Algorithms
CS410 – Software Engineering Lecture #5: C++ Basics III
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Presentation transcript:

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 of an abstract data type using classes, constructors, member functions, data hiding, distinguishing the difference between what the client can do and what the ADT can do

3 List Abstraction Let’s begin by building a “list” abstraction Remember, the data structure used should be able to “plug and play” (i.e., be replaced without affecting the client program Operations will incloude to: –insert, remove, retrieve, and display –create, destroy

4 List Abstraction Once the operations are understood, we can begin to examine the data Let’s build a list to be used for a student roster –so, the data will include the student name, current grade % in the class, and psu id# –we can use a struct or a class to represent the underlying data –we will examine both of these approaches today

5 List Abstraction The list of students can be implemented using a variety of data structures Our choices are: –array (statically allocated), –array (dynamically allocated), –linear linked list –circular linked list –doubly linked list

6 List Abstraction struct data { char * name; char * psu_id; float grade; }; Options for placement: –before the class in the header file –in the implementation file –nested within the class

7 Placed in the.h file struct data { char * name; char * psu_id; float grade; }; class list { public:...

8 Placed in the.cpp file //.h file struct data; class list { public:... //.cpp file struct data { char * name; char * psu_id; float grade; };

9 Nested... class list { public:... private: struct data { char * name; char * psu_id; float grade; };

10 Structs vs. Classes class data { char * name; char * psu_id; float grade; friend class list; }; If a class had been used, the members would have been private by default requiring the list class to be declared as a friend...

11 Structs vs. Classes My personal preference is to use structures for the underlying data –this has the benefit of grouping various data types together –the data is accessible directly by the class that uses the underlying data –and there is little overhead to access the data members (of the name, psu id#, and grade)

12 Structs vs. Classes But, doesn’t this violate data hiding? no! –think about what code can access this data? You would have to have the ‘array’ of students, or the ‘head’ pointer to the first node containing a student to actually access the data –certainly, the client can “see” the structure, but they can’t access the data if properly hidden within the class!

13 Structs vs. Classes But, couldn’t I use a class instead and specify the members as public to avoid using friends? yes...but... –then what is the difference between a class and a struct? –personally, I consider that structs should be used when you simply want to group different data items and that classes be used when you want to create new data types, new abstractions, or are doing OOP

14 Defining the List Class class list { public: list(); ~list(); int insert(const data &); int retrieve(char *, data &); int display(); int remove (char *);

15 List class...continued... private: data s_array[SIZE]; int number_of_students; }; This provides for a statically allocated array of students Notice, none of the member functions uses an index or passes the array Why do we need the 2nd data member?

16 Or...List class...continued... private: data * d_array; int number_of_students; int size_of_array; }; This provides for a dynamically allocated array of students Notice, none of the member functions changes in their client interface Add:list (int size);

17 Or...List class...continued... By replacing the constructor with: list (int size=SIZE); //in prototype only We can have only one constructor act as either the default constructor (with the same size of an array as the previous example), or with a client- specified size The constructor would need to: d_array = new data [size]; size_of_array = size;

18 Or...List class...continued... private: node * head; }; This provides for a linked list (linear, circular, or doubly linked) Notice, none of the member functions changes in their client interface That’s right. Insert and retrieve have the same arguments as before!!!!

19 Defining the node structure But, what does the node look like? struct node { //linear/circular data student; node * next; }; Add, node * previous to the structure for a doubly linked list. Where do we place this? Should the “student” member be a pointer to a data instead?

20 Constructors The purpose of the constructor is to initialize the data members So, for the dynamic array it simply allocated the array’s memory and initialized the size. What else? Yes, it also needs to set the number of items currently stored in the list to zero. Why doesn’t it need to initialize each element of the array?

21 Constructors For the linked list implementation, the constructor would simply set the head pointer to null In some situations, you will also want a second list data member, called tail, to keep track of the end of the list This is important when you want to frequently add to the end of the list Why doesn’t a tail pointer help when removing the last item?

22 Destructors The purpose of a destructor is to deallocate all dynamic memory and close down any resources being used For the statically allocated array, the destructor had no purpose For the dynamically allocated array, it would be:delete [] d_array; Question: Do we need to set the other data members to zero? And, d_array to zero?

23 Destructors For a linked list, the purpose of the destructor is to deallocate all memory This requires traversing through the linked data structure, deallocating all items Will this do it? delete head; Of course not. This will deallocate one node unless the “node” has a destructor of its own...

24 Destructors OK, so what is wrong with this: while (head) { delete head; head = head->next; } delete head; Two things. We are accessing memory within the loop that has already been deallocated Second, we have an extra “delete”

25 Destructors OK, is this correct? while (head) { node * temp = head; delete head; head = temp; } Close...but why reallocate the memory for temp each time through the loop Think about the inefficiency of this!!! Therefore, remove the “underlined” portion

26 Other Member Functions For insert, we would need to define what it is that insert actually does Does it insert at the beginning, at the end, in sorted order? The approach should be well documented in the header file for any client program using the software Remember, insert should not prompt or read from the user but rather get its data through arguments from the client

27 Other Member Functions Let’s examine some various prototype statements for insert and discuss the pros/cons of each: int insert(const data &); bool insert(const data &); void insert(const data &); void insert(data &, bool); int insert(const node &);

28 Other Member Functions What about retrieve? Should it display the matching item or “return” it to the client program? Which prototype is best? int retrieve(char *, data &); data retrieve(char *, bool); data & retrieve (char *, bool);

29 Efficiency... Now that we have seen a partial example of a list abstraction, we should examine the efficiency of using an array versus a linked list for the data structure An array allows for direct access, which is only useful if you know the position of where the data is located If the array is sorted, then a binary search can be used to get to the correct position

30 Efficiency... If a linked list is used, we must traverse to the correct spot to insert into sorted order If the array is not sorted, and we are inserting at the beginning or the end, an array is extremely efficient...direct access for both arrays and linked lists why? Would you need a tail pointer now????

31 Efficiency... But, what about retrieval? An array, if sorted, will allow us to use the binary search A linked list - regardless of if it is sorted or not - will require traversal starting at the head Think of the overhead if you have a list of 10,000 items? How many comparisons would we have in the worst case?

32 Efficiency... With an array that is not sorted, the same is true. An array provides no additional advantage in this case! Ok, so what about memory efficiency? An array’s size must be determined prior to actually needing to use the memory –A statically allocated array must be decided upon at compile time –A dynamically allocated array’s size can be determined later, at run time

33 Next Time... 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 linked list, circular linked list, linked list of linked lists, doubly linked lists, etc. beginning next time! Our next ADT will be an “ordered list” adt!

34 Data Structures Programming Assignment Discussion