1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing...

21
1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    1

Transcript of 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing...

Page 1: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

1

CS2303: Systems Programming Concepts

Class 14Data Structures:

DefiningUsing

ImplementingCopyright 2005-2008, Michael J. Ciaraldi

Page 2: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

2

Data Structures

You may have used some: Stack Queue Linked list Tree

We will learn how to make them.

Page 3: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

3

Data Structures

We will learn how to make them. Why? Not always available. Might have to implement. Better understanding.

Pick most appropriate.

Built-in not appropriate.E.g. kernel, time-critical.

Page 4: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

4

Defining and UsingData Structures

Page 5: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

5

StackGets it name from where?

And is it appropriate?Policy: LIFO

Where else is that used? Top

Operations: push() pop() peek() isEmpty()

Page 6: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

6

Conceptual Stack: push-down

One entry in stack

Push Push3 entries in stack

Page 7: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

7

Conceptual Stack: pop up

3 entries in stack

Pop PopPop causes empty stack

Page 8: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

8

Practical Stack

One entry in stack

Push Push Push

Sta

ck poin

ter

Page 9: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

9

Stack Options

Growth Up? Down?

Stack pointer points to: “Top” of stack? Next free cell?

Types of elements restricted/preserved?

Page 10: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

10

Stack Warnings

How to handle stack overflow?How to handle stack underflow?What is “handle”?

Detect Stack effect Return value Signal error condition

Page 11: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

11

Queue

Gets it name from where? And is it appropriate?

Policy: FIFO Where else is that used? Add at tail, remove at head.

Operations: enqueue() dequeue() peek() isEmpty()

Page 12: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

12

Conceptual Queue

One entry in queue

Enqueue Enqueue Enqueue

Page 13: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

13

Conceptual Queue

3 entries in queue

Dequeue DequeueDequeue causes

empty queue

Page 14: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

14

Practical Queue

Tail pointerHead pointer

Page 15: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

15

Queue Options

Fixed maximum size?Priority?Add/delete at both ends?

Deque = double-ended queue.Types of elements

restricted/preserved?

Page 16: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

16

Queue Warnings

How to handle wraparound?How to handle queue overflow?How to handle queue underflow?What is “handle”?

Detect Queue effect Return value Signal error condition

Page 17: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

17

Data StructureImplementation

Page 18: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

18

Implementation

Alternatives: Object-oriented Not

Page 19: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

19

Implementation

Object-oriented Each stack or queue is a separate

object. Fields: Stack data area, size, pointer. Methods: push(), pop(), etc.

Page 20: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

20

Implementation

Not Object-oriented Series of functions: create(), push(),

pop(), etc. Need identifier if more than one. Where to store specs? Risky, but done: just a stack pointer.

Page 21: 1 CS2303: Systems Programming Concepts Class 14 Data Structures: Defining Using Implementing Copyright 2005-2008, Michael J. Ciaraldi.

21

Next Time

More data structures: Implementation, list, tree…