Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 240: Data Structures Thursday, June 21 th Lists – Array based, Link based Dynamic vs Static."— Presentation transcript:

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

2 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:1020301050100

3 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:1020301050100 Next:i+12i+24i+16i+80i+4i

4 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.

5 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.

6 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.

7 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!

8 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).

9 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.

10 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; };

11 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!

12 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

13 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

14 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

15 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

16 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!

17 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!

18 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!

19 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….

20 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!

21 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

22 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.

23 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.

24 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

25 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: 5660728090 Start Null Now, lets remove 80. Ok, let’s traverse the list! That’s not good.

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


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

Similar presentations


Ads by Google