aps105 lists. structures arrays allow a collection of elements –all of the same type how to...
TRANSCRIPT
![Page 1: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/1.jpg)
APS105
Lists
![Page 2: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/2.jpg)
Structures
![Page 3: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/3.jpg)
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>;
![Page 4: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/4.jpg)
Example• A struct of stock items at a store:
.
![Page 5: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/5.jpg)
Separate Definition from Declaration
.
![Page 6: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/6.jpg)
Using Typedef
• Can define your own types using typedef!• General form:
typedef <type> <yourTypeName>;• Examples:
.
![Page 7: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/7.jpg)
Doing typedef and definition at once:
.
![Page 8: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/8.jpg)
Referencing Struct Members
.
![Page 9: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/9.jpg)
Structs and Pointers
StockItem x;StockItem *p;p = &x; // p points to x
// ways to set quantity to 500:.
![Page 10: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/10.jpg)
Malloc a Struct (StockItem)
.
![Page 11: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/11.jpg)
Lists
![Page 12: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/12.jpg)
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
![Page 13: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/13.jpg)
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?
5 3 1 6 2 6 7 0
9
5 3 1 6 2 6 7 0
X
5 3 1 6 2 6 7 3 8 0
9
![Page 14: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/14.jpg)
Flexible List: “Linked List”
• Insert an item into the middle of the list:
• Delete an item from the middle of the list:
9
5 3 1 6 2 6 7
5 3 1 6 2 6 7
X
5 3 1 6 2 6 7
![Page 15: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/15.jpg)
Implementing a Linked List of ints
.
![Page 16: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/16.jpg)
Allocating a new Node
• a function to allocate & initialize a node
.
![Page 17: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/17.jpg)
Creating a List: Adding to the end
.
![Page 18: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/18.jpg)
Creating a List: Adding to the front
.
![Page 19: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/19.jpg)
Function to Print a List
.
![Page 20: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/20.jpg)
Deleting the First Node of a List
info: 5link:
myListinfo: 3link:
info: 1link:
![Page 21: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/21.jpg)
Function to Delete first Node
.
![Page 22: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/22.jpg)
Delete first Node: return new head
.
![Page 23: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/23.jpg)
Function to return true if an item found
.
![Page 24: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/24.jpg)
Function to Delete Entire List
.
![Page 25: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/25.jpg)
Function to Add Element to End of List
.
![Page 26: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/26.jpg)
Deleting the Last Node of a List
info: 5link:
myListinfo: 3link:
info: 1link:
![Page 27: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/27.jpg)
Function to Delete Last Element
.
![Page 28: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/28.jpg)
Inserting into an Ordered List
info: 1link:
myListinfo: 3link:
info: 5link:
4
![Page 29: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/29.jpg)
Function to Insert into Ordered List.
![Page 30: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/30.jpg)
Using Recursion on Lists
![Page 31: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/31.jpg)
Printing a List using Recursion• can think of a list recursively as: a head node plus a list
.
![Page 32: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/32.jpg)
Deleting a List using Recursion.
![Page 33: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/33.jpg)
Finding an Item using Recursion.
![Page 34: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/34.jpg)
Compare 2 Lists: true if identical.
![Page 35: 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](https://reader035.vdocuments.us/reader035/viewer/2022062518/5697bf8c1a28abf838c8bc24/html5/thumbnails/35.jpg)
Insert into an Ordered List.