APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to...

35
APS105 Lists

Transcript of APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to...

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Insert into an Ordered List.