1 / 35

APS105

APS105. Lists. Structures. Structures. Arrays allow a collection of elements All of the same type How to collect elements of different types? Structures; in C: struct General form: struct { <variable declarations> } <identifier list>;. Example.

vicki
Download Presentation

APS105

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. APS105 Lists

  2. Structures

  3. Structures • Arrays allow a collection of elements • All of the same type • How to collect elements of different types? • Structures; in C: struct • General form: struct { <variable declarations> } <identifier list>;

  4. Example • A struct of stock items at a store: .

  5. Separate Definition from Declaration .

  6. Using Typedef • Can define your own types using typedef! • General form: typedef <type> <yourTypeName>; • Examples: .

  7. Doing typedef and definition at once: .

  8. Referencing Struct Members .

  9. Structs and Pointers StockItem x; StockItem *p; p = &x; // p points to x // ways to set quantity to 500: .

  10. Malloc a Struct (StockItem) .

  11. Lists

  12. Intro • A list is a sequence of items • can implement lists different ways: • a group of variables • an array • using pointers (as we will see) • Typical list operations • start a new, empty list • insert a new element into the list • find an item with a certain value in the list • delete an item from the list • print the list • Abstract Data Type (ADT) • A list is an example of an ADT • An ADT is a concept • there are multiple ways to implement an ADT

  13. The Problem with Arrays • Insert an item into the middle of the list: • Delete an item from the middle of the list: • What if the array isn’t big enough? 9 X 9

  14. Flexible List: “Linked List” • Insert an item into the middle of the list: • Delete an item from the middle of the list: 5 3 1 6 2 6 7 5 3 1 6 2 6 7 9 5 3 1 6 2 6 7 X

  15. Implementing a Linked List of ints .

  16. Allocating a new Node • a function to allocate & initialize a node .

  17. Creating a List: Adding to the end .

  18. Creating a List: Adding to the front .

  19. Function to Print a List .

  20. Deleting the First Node of a List myList info: 5 link: info: 3 link: info: 1 link:

  21. Function to Delete first Node .

  22. Delete first Node: return new head .

  23. Function to return true if an item found .

  24. Function to Delete Entire List .

  25. Function to Add Element to End of List .

  26. Deleting the Last Node of a List myList info: 5 link: info: 3 link: info: 1 link:

  27. Function to Delete Last Element .

  28. Inserting into an Ordered List myList info: 1 link: info: 3 link: info: 5 link: 4

  29. Function to Insert into Ordered List .

  30. Using Recursion on Lists

  31. Printing a List using Recursion • can think of a list recursively as: a head node plus a list .

  32. Deleting a List using Recursion .

  33. Finding an Item using Recursion .

  34. Compare 2 Lists: true if identical .

  35. Insert into an Ordered List .

More Related