Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 APS105 Lists

2 Structures

3 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 { } ;

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 ; 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? 53162670 9 53162670 X 5316267380 9

14 Flexible List: “Linked List” Insert an item into the middle of the list: Delete an item from the middle of the list: 9 5316267 5316267 X 5316267

15 Implementing a Linked List of int s.

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 info: 5 link: myList 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 info: 5 link: myList info: 3 link: info: 1 link:

27 Function to Delete Last Element.

28 Inserting into an Ordered List info: 1 link: myList 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.


Download ppt "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."

Similar presentations


Ads by Google