ce 221 data structures and algorithms chapter 3: lists, stacks, and queues - iii text: read weiss,...

13
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1 Izmir University of Economics

Upload: ashlie-elliott

Post on 19-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

CE 221Data Structures and

Algorithms

Chapter 3: Lists, Stacks, and Queues - III

Text: Read Weiss, §3.7

1Izmir University of Economics

Page 2: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

The Queue ADT• Like stacks, queues are lists. With a queue,

however, insertion is done at one end, whereas deletion is performed at the other end.

• The basic operations on a queue (FIFO list) are enqueue, which inserts an element at the end of the list (called the rear), and dequeue, which deletes (and returns) the element at the start of the list (known as the front).

Izmir University of Economics 2

Page 3: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Array Implementation of Queues - I

• As with stacks, both array and linked list (trivial) implementations give O(1) running times. Let’s discuss circular array implementation.

• An array to store elements, Array, and the positions Front and Rear to represent the ends of the queue are kept.

• We also keep track of the number of elements: Size.

Izmir University of Economics 3

Page 4: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Izmir University of Economics 4

Array Implementation of Queues – Problem

5 2 7 1

Front Rear

• To enqueue an element X, increment Size and Rear and set Array[Rear]=X

• To dequeue an element, set the return value to Array[Front], decrement Size and then increment Front.

• Assume after several enqueue operations, the Rear is at the last index position. Assume also that some elements, in the meantime, have been dequeued to make up room. The next enqueue would fail, although there would be free slots.

Page 5: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

• The simple solution is whenever Front or Rear gets to the end of the Array, it is wrapped around to the beginning (hence the name circular array).

Izmir University of Economics 5

Array Implementation of Queues – Problem Solved

2 4

Front Rear

1 2 4

FrontRear

1 3 2 4

FrontRear

initially

After enqueue(1)

After enqueue(3)

Page 6: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Izmir University of Economics 6

Array Implementation of Queues – Example

1 3 2 4

FrontRear

After dequeue 2

1 3 2 4

Front Rear

After dequeue 4

1 3 2 4

Front Rear

After dequeue 1

1 3 2 4

FrontRear

After dequeue 3and queue is empty

• If Size is not kept, queue is empty when Rear=Front-1. In that case, queue is full when there are Capacity-1 items. Why? Only Capacity different sizes can be differentiated and one of these is 0.

Page 7: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Izmir University of Economics 7

Circular Array Implementation of Queues – Sample Code I

typedef int ElementType;#define MinQueueSize (5)

struct QueueRecord { int Capacity; int Front; int Rear; int Size; ElementType *Array;};

typedef struct QueueRecord *Queue;

int IsEmpty( Queue Q ) { return Q->Size == 0;}

int IsFull( Queue Q ) { return Q->Size == Q->Capacity;}

/* MakeEmpty is invoked *//* after CreateQueue *//* on the next slide *//* is called */void MakeEmpty(Queue Q) { Q->Size = 0; Q->Front = 1; Q->Rear = 0;}

void DisposeQueue(Queue Q) { if( Q != NULL ) { free( Q->Array ); free( Q ); }}

Page 8: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Izmir University of Economics 8

Circular Array Implementation of Queues – Sample Code II

Queue CreateQueue(int MaxElements) { Queue Q;

if(MaxElements<MinQueueSize) Error("Queue size is too small”);

Q = malloc(sizeof(struct QueueRecord)); if(Q == NULL) FatalError("Out of space!!!”);

Q->Array = malloc(sizeof(ElementType)*MaxElements); if( Q->Array == NULL ) FatalError("Out of space!!!”); Q->Capacity = MaxElements; MakeEmpty( Q );

return Q;}

Page 9: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Izmir University of Economics 9

Circular Array Implementation of Queues – Sample Code III

static int Succ(int Value, Queue Q) { if( ++Value == Q->Capacity ) Value = 0; return Value;}void Enqueue( ElementType X, Queue Q ) { if( IsFull( Q ) ) Error( "Full queue" ); else { Q->Size++; Q->Rear = Succ( Q->Rear, Q ); Q->Array[ Q->Rear ] = X; }}ElementType Front( Queue Q ) { if( !IsEmpty( Q ) ) return Q->Array[ Q->Front ]; Error( "Empty queue" ); return 0; /* Return value used to avoid warning */}

Page 10: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Izmir University of Economics 10

Circular Array Implementation of Queues – Sample Code IV

void Dequeue( Queue Q ) { if( IsEmpty( Q ) ) Error( "Empty queue" ); else { Q->Size--; Q->Front = Succ( Q->Front, Q ); }}ElementType FrontAndDequeue( Queue Q ) { ElementType X = 0;

if( IsEmpty( Q ) ) Error( "Empty queue" ); else { Q->Size--; X = Q->Array[ Q->Front ]; Q->Front = Succ( Q->Front, Q ); } return X;}

Page 11: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Applications of Queues - I

Izmir University of Economics 11

• When jobs are submitted to a printer, they are arranged in order of arrival.

• Virtually every real-life line is (supposed to be) a queue.

• Users on other machines are given access to a file server on a first-come first-served basis.

• Calls to large companies are generally placed on a queue when all operators are busy.

Page 12: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Izmir University of Economics 12

Applications of Queues - II• A whole branch of mathematics, known as

queueing theory, deals with computing, probabilistically, how long users expect to wait on a line, how long the line gets, and other such questions. The answer depends on how frequently users arrive to the line and how long it takes to process a user once the user is served. Both of these parameters are given as probability distribution functions.

Page 13: CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - III Text: Read Weiss, §3.7 1Izmir University of Economics

Homework Assignments

• 3.35

• You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me.

Izmir University of Economics 13