Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 2 Queue.

Similar presentations


Presentation on theme: "Computer Science 2 Queue."— Presentation transcript:

1 Computer Science 2 Queue

2 Learning Objectives Improve reading a program that uses linked lists.
Be able to model a line using a linked list.

3 program DryRunPointers; type ptrtype = ^rectype; rectype = record
score:integer; next:ptrtype; end; var front, back:ptrtype; count:integer; procedure a(var front, back:ptrtype;count:integer); temp:ptrtype; begin new(temp); temp^.score:=count; temp^.next:=nil; if front = nil then front:=temp; back:= temp; end else back^.next:= temp; back:=temp; procedure b(front:ptrtype); begin while front<> nil do writeln(front^.score); front:=front^.next; end; procedure c(var front, back:ptrtype); var temp:ptrtype; if front=nil then writeln('The list is empty') else temp:=front; dispose(temp); begin {Of the main body}; front:=nil; back:=nil; for count:= 1 to 4 do a(front,back,3*count); b(front); for count := 1 to 5 do c(front,back); end.

4 Queue Definition: A data structure where you the first one on the list is the first one off of the list.

5 Quick Check What does a stack look like after the following?
Push: 10, 20, 5, 16 Pop: Two Values Push: 8 Pop: 2 Values Push: 47, 18 What does a Queue look like after the following? Enqueue (Add): 5, 12, 27 Dequeue (Remove): 2 values Enqueue: 51, 12, 92, 26 Dequeue: 2 values 12 92 26

6 Uses: Modeling a line of people. Modeling a bank, fast food place, …
Setting up a printer queue. This is where several computers print using one printer. A computer is set up as the print server and processes who gets to print and when. Modeling traffic pattern at a signal.

7 Example: Initializing the queue: Setting the front and back to nil.
Adding to a queue: Enqueue Empty Queue Non-empty Queue Picture Code Showing all. Taking off of a queue: Dequeue

8 From Idea to Code Initialize the Queue Add to an empty queue
Front := nil; Back:= nil; Add to an empty queue If front = nil then Begin New(temp); Readln(temp^.name); Temp^.next:= back; Back:= temp; Front:= temp; End;

9 Idea to Code Add to non-empty queue If front <> nil then begin
New(temp); Temp^.next:= nil; Back^.next:= temp; Back:= temp; end

10 Idea to Code: Show all Temp:= front; While temp<> nil do Begin
Writeln(Temp^.name); Temp:= temp^.next; End;

11 Remove One: Dequeue Temp := front; If front<> nil then Begin
Front:= front^.next; Writeln(temp^.name , ‘ is being removed.’); Dispose(temp); end;

12 Program options: Write a program that will model a line in a bank. Using a menu and a while loop. Add person to the line. Get their name and put them in the line. Show all of the people in the line. Serve a person. Say the name of the person being served. Close the bank. (When this is chosen, everyone left in the line is dismissed personally. Ex. Sorry we are closed, please come back tomorrow, John, Jan , Jane, Jal) Push: Time how long each person waits in the line. Add a provision for a person cutting in line. Create a screen saver that uses a queue help modeling the motion. Have the lines around the screen saver where the lines are added to one end of the series and the line in the back of the series is removed. Note: You need to incorporate the pointers into this option.


Download ppt "Computer Science 2 Queue."

Similar presentations


Ads by Google