computer science: a structured programming approach using c1 15-3 stacks a stack is a linear list in...

39
Computer Science: A Structured Programming Approach Using C 1 15-3 Stacks A A stack stack is a linear list in which all additions and is a linear list in which all additions and deletions are restricted to one end, called the deletions are restricted to one end, called the top top . Stacks are known as the last in–first out . Stacks are known as the last in–first out (LIFO) data structure. (LIFO) data structure. Stack Structures Stack Algorithms Stack Demonstration Topics discussed in this section: Topics discussed in this section:

Upload: gary-buck

Post on 01-Apr-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 1

15-3 Stacks

A A stackstack is a linear list in which all additions and is a linear list in which all additions and deletions are restricted to one end, called the deletions are restricted to one end, called the toptop. . Stacks are known as the last in–first out (LIFO) data Stacks are known as the last in–first out (LIFO) data structure.structure.

Stack StructuresStack AlgorithmsStack Demonstration

Topics discussed in this section:Topics discussed in this section:

Page 2: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 2

A stack is a last in–first out (LIFO) data structure in which all insertions and deletions are restricted

to one end, called the top.

NoteNote

Page 3: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 3

FIGURE 15-17 Stack

Page 4: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 4

FIGURE 15-18 Conceptual and Physical Stack Implementations

Page 5: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 5

FIGURE 15-19 Stack Data Structure

Page 6: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 6

FIGURE 15-20 Streams

Page 7: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 7

PROGRAM 15-9 Push Stack

Page 8: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 8

PROGRAM 15-9 Push Stack

Page 9: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 9

FIGURE 15-21 Pop Stack Example

Page 10: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 10

PROGRAM 15-10 Pop Stack

Page 11: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 11

PROGRAM 15-10 Pop Stack

Page 12: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 12

FIGURE 15-22 Design for Basic Stack Program

Page 13: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 13

PROGRAM 15-11 Simple Stack Application Program

Page 14: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 14

PROGRAM 15-11 Simple Stack Application Program

Page 15: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 15

PROGRAM 15-11 Simple Stack Application Program

Page 16: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 16

PROGRAM 15-12 Insert Data

Page 17: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 17

PROGRAM 15-12 Insert Data

Page 18: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 18

PROGRAM 15-13 Print Stack

Page 19: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 19

15-4 Queues

A A queuequeue is a linear list in which data can be inserted is a linear list in which data can be inserted only at one end, called the only at one end, called the rearrear, and deleted from the , and deleted from the other end, called the other end, called the frontfront..

Queue OperationsQueue Linked List DesignQueue FunctionsQueue Demonstration

Topics discussed in this section:Topics discussed in this section:

Page 20: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 20

A queue is a linear list in which data can be inserted at one end, called the rear, and deleted from the other end,

called the front. It is a first in–first out(FIFO) restricted data structure.

NoteNote

Page 21: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 21

FIGURE 15-23 Queue Concept

Page 22: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 22

Enqueue inserts an element at the rear of the queue.

NoteNote

Page 23: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 23

FIGURE 15-24 Enqueue

Page 24: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 24

Dequeue deletes an element at the front of the queue.

NoteNote

Page 25: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 25

FIGURE 15-25 Dequeue

Page 26: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 26

FIGURE 15-26 Conceptual and Physical Queue Implementations

Page 27: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 27

FIGURE 15-27 Queue Data Structure

Page 28: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 28

FIGURE 15-28 Enqueue Example

Page 29: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 15-14 Enqueue

Page 30: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 15-14 Enqueue

Page 31: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 31

FIGURE 15-29 Dequeue Examples

Page 32: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 15-15 Dequeue

Page 33: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 33

PROGRAM 15-15 Dequeue

Page 34: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 34

PROGRAM 15-16 Simple Queue Demonstration

Page 35: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 35

PROGRAM 15-16 Simple Queue Demonstration

Page 36: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 36

PROGRAM 15-16 Simple Queue Demonstration

Page 37: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 37

PROGRAM 15-17 Insert Data

Page 38: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 38

PROGRAM 15-17 Insert Data

Page 39: Computer Science: A Structured Programming Approach Using C1 15-3 Stacks A stack is a linear list in which all additions and deletions are restricted to

Computer Science: A Structured Programming Approach Using C 39

PROGRAM 15-18 Print Queue