Top Banner
Lists Lists CSE1303 Part A Data Structures and Algorithms
25
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lists CSE1303 Part A Data Structures and Algorithms.

ListsLists

CSE1303 Part A

Data Structures and Algorithms

Page 2: Lists CSE1303 Part A Data Structures and Algorithms.

Basic Data TypesBasic Data Types

StackLast-In, First-Out (LIFO)initialize, push, pop, status

QueueFirst-In, First-Out (FIFO)initialize, append, serve, status

• List

Page 3: Lists CSE1303 Part A Data Structures and Algorithms.

OverviewOverview

• Lists.

• Operations.

• Abstract Data Types (ADT).

• Programming Styles.

• Implementations of Lists.

Page 4: Lists CSE1303 Part A Data Structures and Algorithms.

ListsLists

SEALFOXDEERAPE EMU0 1 3 42

Page 5: Lists CSE1303 Part A Data Structures and Algorithms.

InsertionInsertion

SEALFOXDEERAPE0 1 32

EMU Inserted at position 2

SEALFOXDEERAPE0 1 3 4

EMU2

Before:

After:

Page 6: Lists CSE1303 Part A Data Structures and Algorithms.

DeletionDeletion

SEALFOXDEERAPE0 1 3 4

EMU2

Delete item at position 3

SEALDEERAPE0 1 3

EMU2

Before:

After:

Page 7: Lists CSE1303 Part A Data Structures and Algorithms.

List OperationsList Operations

• Initialize the list.

• Determine whether the list is empty.

• Determine whether the list is full.

• Find the size of the list.

• Insert an item anywhere in the list.

• Delete an item anywhere in a list.

• Go to a particular position in a list.

Page 8: Lists CSE1303 Part A Data Structures and Algorithms.

A List ADTA List ADT

• Initialize the list.• Determine whether the list is empty.• Determine whether the list is full.• Find the size of the list.• Insert an item anywhere in the list.• Delete an item anywhere in a list.• Go to a particular position in a list.

A sequence of elements together with these operations:

Page 9: Lists CSE1303 Part A Data Structures and Algorithms.

Abstract Data Type (ADT)Abstract Data Type (ADT)

• A Data Structure together with operations defined on it.

• Useful to consider what operations are required before starting implementation.

• Led to the development of object oriented programming.

Page 10: Lists CSE1303 Part A Data Structures and Algorithms.

A Stack ADTA Stack ADT

• Initialize the stack.• Determine whether the stack is empty.• Determine whether the stack is full.• Push an item onto the top of the stack. • Pop an item off the top of the stack.

A sequence of elements together with these operations:

Page 11: Lists CSE1303 Part A Data Structures and Algorithms.

A Queue ADTA Queue ADT

• Initialize the queue.• Determine whether the queue is empty.• Determine whether the queue is full.• Find the size of the queue.• Append an item to the rear of the queue.• Serve an item at the front of the queue.

A sequence of elements together with these operations:

Page 12: Lists CSE1303 Part A Data Structures and Algorithms.

ComparisonComparison

• Stacks– Insert at the top of the stack (push)– Delete at the top of the stack (pop)

• Queues– Insert at the rear of the queue (append)– Delete at the front of the queue (serve)

• Lists– Insert at any position in the list.– Delete at any position in the list.

Page 13: Lists CSE1303 Part A Data Structures and Algorithms.

A Rational Number ADTA Rational Number ADT

• Initialize the rational number.• Get the numerator.• Get the denominator.• Simplify a rational number.• Add two rational numbers.• Determine whether two rational numbers are

equal.• etc.

Two integers together with these operations:

Page 14: Lists CSE1303 Part A Data Structures and Algorithms.

A String ADTA String ADT

• Initialize the string.• Copy a string.• Read in a line of input.• Concatenate two strings.• Compare two strings.• Find a length of a string.• etc.

A array of characters together with these operations:

Page 15: Lists CSE1303 Part A Data Structures and Algorithms.

Programming StylesProgramming Styles• Procedural Programming

Decide which procedures you want: use the best algorithms you can find.

• Modular ProgrammingDecide which modules you want: partition the program so

that data is hidden in modules.

• Data AbstractionDecide which types you want: provide a full set of

operations for each type.

• Object-Oriented ProgrammingDecide which classes you want: provide a full set of operations for each class; make commonality explicit

by using inheritance.

Page 16: Lists CSE1303 Part A Data Structures and Algorithms.

Simple List ImplementationSimple List Implementation

0 1 2 3 4 5

APE DEER FOX SEAL

EMU

Page 17: Lists CSE1303 Part A Data Structures and Algorithms.

Simple List ImplementationSimple List Implementation

0 1 2 3 4 5

APE DEER FOX SEAL

EMU

Page 18: Lists CSE1303 Part A Data Structures and Algorithms.

Simple List ImplementationSimple List Implementation

0 1 2 3 4 5

APE DEER FOX SEALEMU

Page 19: Lists CSE1303 Part A Data Structures and Algorithms.

Linked List Implementation:Linked List Implementation:Using Array IndexUsing Array Index

DEER FOX APE SEAL

0 1 2 3 4 5

1 3 0 -1

data

marks last item

start

link to next item

Page 20: Lists CSE1303 Part A Data Structures and Algorithms.

Linked List Implementation:Linked List Implementation:Using Array IndexUsing Array Index

DEER FOX APE SEAL

0 1 2 3 4 5

3 0 -1

startinsert: EMU

EMU

11

Page 21: Lists CSE1303 Part A Data Structures and Algorithms.

Linked List Implementation:Linked List Implementation:Using Array IndexUsing Array Index

DEER FOX APE SEAL

0 1 2 3 4 5

4 3 0 -1

startinsert: EMU

EMU

1

Page 22: Lists CSE1303 Part A Data Structures and Algorithms.

Linked List Implementation:Linked List Implementation:Using PointersUsing Pointers

DEER FOX APE SEAL

0x2000 0x2008 0x2010 0x2018 0x2020 0x2018

0x2020 0x2018 0x2000 NULL

start

EMU

0x2008

Page 23: Lists CSE1303 Part A Data Structures and Algorithms.

Linked List Implementation:Linked List Implementation:Using PointersUsing Pointers

DEER FOX APE SEAL

0x2000 0x2008 0x2010 0x2018 0x2020 0x2018

0x2020 0x2018 0x2000 NULL

start

EMU

0x2008

special pointerconstant

Page 24: Lists CSE1303 Part A Data Structures and Algorithms.

RevisionRevision

• List– initialize, insert, delete, position, status– implementations

• Difference between Stacks, Queues, and Lists.

• ADT.

Page 25: Lists CSE1303 Part A Data Structures and Algorithms.

Next LectureNext Lecture

• Dynamic Memory

• Allocate/Deallocate Memory

PreparationPreparation

• Read 4.5 in Kruse et al.

• Read 12.3 in Deitel and Deitel.