CS 240: Data Structures Thursday, June 21 th Lists – Array based, Link based Dynamic vs Static.

Slides:



Advertisements
Similar presentations
Lists CS 3358.
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Foundation of Computing Systems Lecture 2 Linked Lists.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 261- Winter 2009 Dynamic Array Queue and Deque.
Linked Lists
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
CS 240: Data Structures Thursday, June 21 th Vector, Linked List.
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 Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Chapter 3: Arrays, Linked Lists, and Recursion
CS 240: Data Structures Thursday, July 12 th Lists, Templates, Vector, Algorithms.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
A quadratic equation is a second degree polynomial, usually written in general form: The a, b, and c terms are called the coefficients of the equation,
Grade 12 Computer Studies HG
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
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Linked Lists based on the original work of Dr. Roger deBry Version 1.0
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Data Structure & Algorithms
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
3/19/2016 5:40 PMLinked list1 Array-based List v.s. Linked List Yumei Huo Department of Computer Science College of Staten Island, CUNY Spring 2009.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
Unit – I Lists.
CSCI-255 LinkedList.
CS 1114: Implementing Search
Hashing Exercises.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Topic 11 Linked Lists -Joel Spolsky
Stacks, Queues, and Deques
Arrays and Linked Lists
Stacks, Queues, and Deques
Lesson Objectives Aims
Linked List Intro CSCE 121 J. Michael Moore.
Linked Lists.
Introduction to C++ Linear Linked Lists
Lists.
Linked List Intro CSCE 121.
Topic 11 Linked Lists -Joel Spolsky
Presentation transcript:

CS 240: Data Structures Thursday, June 21 th Lists – Array based, Link based Dynamic vs Static

Lists Here is a “random access” list, in array form Here is a “random access” list, in array form Address:ii+4i+8i+12i+16i+20i+24 Value:

Lists Here is a “traditional” list, in array form Here is a “traditional” list, in array form Wait…. Where do we start? Wait…. Where do we start? We have to maintain that data separately. We have to maintain that data separately. We usually refer to it as “first” We usually refer to it as “first” first = i+20 first = i+20 Address:ii+4i+8i+12i+16i+20i+24 Value: Next:i+12i+24i+16i+80i+4i

Implications How is the data being accessed? How is the data being accessed? ??? ??? These are perfect candidates to use with pointers. These are perfect candidates to use with pointers.

What is a list? A list is a container class A list is a container class Like an array Like an array However… However… The list includes: The list includes: 1) The address of the first piece of data 1) The address of the first piece of data 2) The data 2) The data 3) For each data, the address of the next piece. 3) For each data, the address of the next piece.

List Data Since each data is attached to the location of the next piece we can represent them as an ADT: We generally refer to this as a Node class Node { T thedata;//Remember, T can be any type memory_address next_data; Node * next_data; }; Ok, Node has our data. What about “memory_address”? Well, Node tells us where the next Node is…. Therefore, memory_address is really a Node pointer.

Floating Nodes Now we have a node (instead of separate data): Now we have a node (instead of separate data): Type: Unknown (T) Value (X bits): ? Address: ? ADT: Node Size: X+32 bits thedata: (X bits) -> T next_node (32 bits) -> Node * Type: Node * Value (32 bits): ? Address: ? Bonus: Instead of having to keep track of two pieces of data, we keep track of one! If we know where the node is, we know where the data and next_node pointer are!

Floating Nodes Address: 0xABCD ADT: Node Size: X+32 bits thedata: (X bits) -> T next_node (32 bits) -> Node * Ok, we can create a node now. Ok, we can create a node now. First, we should decide what kind of data node will hold. First, we should decide what kind of data node will hold. Later, we will make it possible for node to hold anything (a couple of weeks from now). Later, we will make it possible for node to hold anything (a couple of weeks from now).

Floating Nodes Address: 0xABCD ADT: Node Size: X+32 bits thedata: (32 bits) -> String next_node (32 bits) -> Node * Strings sound good. Strings sound good. For ease, we will directly access the elements of Node in these slides. We can also write methods to place the data and change the pointers. For ease, we will directly access the elements of Node in these slides. We can also write methods to place the data and change the pointers.

Floating Nodes Address: 0xABCD ADT: Node Size: X+32 bits thedata: (32 bits) -> String next_node (32 bits) -> Node * class Node {public: string thedata; Node * next_data; };

Creating a Node first - Address: 0x50F4 ADT: Node Size: X+32 bits thedata: (32 bits) -> String next_node (32 bits) -> Node * Remember, we need to know where the first node in our list is. Remember, we need to know where the first node in our list is. Therefore: “Node * first = new Node();” Therefore: “Node * first = new Node();” Remember to delete it when you are done! Remember to delete it when you are done!

Storing Data first - Address: 0x50F4 ADT: Node Size: X+32 bits thedata: (32 bits) -> String next_node (32 bits) -> Node * For some string we’ll call “userinput” with value “apple” For some string we’ll call “userinput” with value “apple” first->thedata = userinput; first->thedata = userinput; first->next_node = NULL; first->next_node = NULL; ADT: Node Size: X+32 bits thedata: (32 bits) -> String “apple” next_node (32 bits) -> Node * NULL

Adding Data first - Address: 0x50F4 How do we add data? How do we add data? Well, we have to find an empty location in our list. Well, we have to find an empty location in our list. Access the list and search for an empty location!. Access the list and search for an empty location!. ADT: Node Size: X+32 bits thedata: (32 bits) -> String “apple” next_node (32 bits) -> Node * NULL

Adding Data first - Address: 0x50F4 Let’s add “donut”. Let’s add “donut”. first->next_node = new Node(); first->next_node = new Node(); Node * accessptr = first->next_node; Node * accessptr = first->next_node; ADT: Node Size: X+32 bits thedata: (32 bits) -> String “apple” next_node (32 bits) -> Node * NULL ??? - Address: 0x846c ADT: Node Size: X+32 bits thedata: (32 bits) -> String Uninitialized next_node (32 bits) -> Node * Uninitialized ADT: Node Size: X+32 bits thedata: (32 bits) -> String “apple” next_node (32 bits) -> Node * 0x846c

Adding Data first - Address: 0x50F4 Node * accessptr = first->next_node; Node * accessptr = first->next_node; accessptr->thedata = “donut”; accessptr->thedata = “donut”; accessptr->next_node = NULL; accessptr->next_node = NULL; ??? - Address: 0x846c ADT: Node Size: X+32 bits thedata: (32 bits) -> String Uninitialized next_node (32 bits) -> Node * Uninitialized ADT: Node Size: X+32 bits thedata: (32 bits) -> String “apple” next_node (32 bits) -> Node * 0x846c ADT: Node Size: X+32 bits thedata: (32 bits) -> String “donut” next_node (32 bits) -> Node * NULL

This can get much larger first: 0x50F4 0x846c ADT: Node Size: X+32 bits thedata: “apple” next_node: 0x846c ADT: Node Size: X+32 bits thedata: “donut” next_node: 0x3120 0x3120 ADT: Node Size: X+32 bits thedata: “cashew” next_node: 0x4278 0x4278 ADT: Node Size: X+32 bits thedata: “tomato” next_node: 0x5610 0x5610 ADT: Node Size: X+32 bits thedata: “banana” next_node: 0x8458 0x8458 ADT: Node Size: X+32 bits thedata: “hat” next_node: NULL Organizing this will allow us to make something useful!

Why lists? So far, all we have done is manage items using contiguous memory. So far, all we have done is manage items using contiguous memory. If we needed more room, we would ask for it and copy all of our data into the larger space. If we needed more room, we would ask for it and copy all of our data into the larger space. Movin on up!

Contiguous Memory So, what’s the big deal? So, what’s the big deal? Isn’t moving up good? Isn’t moving up good? Of course! But it is expensive! Of course! But it is expensive! We don’t want to try to hold everything! It gets messy! We don’t want to try to hold everything! It gets messy!

The Collector Using our “mycontainer”, our CDs wouldn’t be very organized. Using our “mycontainer”, our CDs wouldn’t be very organized. If we organize when we insert…. If we organize when we insert….

Insertion Issues When can insertion be a problem? When can insertion be a problem? Inserting at the end of the mycontainer is easy! Inserting at the end of the mycontainer is easy! Inserting in the middle isn’t too bad! Inserting in the middle isn’t too bad! Inserting at the front…. Inserting at the front…. With a list, we can insert easily! With a list, we can insert easily!

Let us represent this list: first: 0x50F4 0x846c ADT: Node Size: X+32 bits thedata: “apple” next_node: 0x846c ADT: Node Size: X+32 bits thedata: “donut” next_node: 0x3120 0x3120 ADT: Node Size: X+32 bits thedata: “cashew” next_node: 0x4278 0x4278 ADT: Node Size: X+32 bits thedata: “tomato” next_node: 0x5610 0x5610 ADT: Node Size: X+32 bits thedata: “banana” next_node: 0x8458 0x8458 ADT: Node Size: X+32 bits thedata: “hat” next_node: NULL

Nodes A node’s pointer nodes to 1 of 2 places: A node’s pointer nodes to 1 of 2 places: To another node To another node To Null To Null We can additional pointers in our Node and maintain additional data for various reasons. We can additional pointers in our Node and maintain additional data for various reasons.

List Construction To create a list: To create a list: We need to create a node pointer (which points to Null) We need to create a node pointer (which points to Null) Is that it? Is that it? Well, we need functions to act on the list. Well, we need functions to act on the list. Some things – like size – may be easier to manage as part of the list. Some things – like size – may be easier to manage as part of the list.

List Insertion Inserting into the list has two cases: Inserting into the list has two cases: The list was empty: The list was empty: The list was not empty: The list was not empty: However, our insertion policy may make this more difficult: However, our insertion policy may make this more difficult: Just insert Just insert Insert in order Insert in order This technically doesn’t add another case This technically doesn’t add another case

List Removal If the value is in the list, removing a value requires that we ensure the list is still valid: If the value is in the list, removing a value requires that we ensure the list is still valid: Start Null Now, lets remove 80. Ok, let’s traverse the list! That’s not good.

List Removal Let ’ s try this again. Let ’ s try this again Start Null Now, lets remove 80. Ok, let’s traverse the list! He made it!