Download presentation
Presentation is loading. Please wait.
1
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A4 – Basic Data Structures – Continued (Queues)
2
2 Basic Data Structures Stacks Queues Lists
3
3 Overview What is a Queue? Queue Operations. Applications. Linear Implementation. Circular Implementation.
4
4Append Before FrontRear After FrontRear
5
5Serve Before FrontRear After Front Rear This comes off the queue
6
6 Operations Initialize the queue. Append an item to the rear of the queue. Serve an item from the front of the queue. Is the queue empty? Is the queue full? What size is the queue?
7
7 Applications In operating systems, e.g. printer queues, process queues, etc. Simulation programs. Algorithms.
8
8 Linear Implementation Front Rear 0 dogfishduckcatfinch 1234567
9
9 Append FrontRear 0 dogfishduckcatfinchsnake 1234567
10
10 Append FrontRear 0 dogfishduckcatfinchsnakeeel 1234567
11
11 Serve FrontRear 0 fishduckcatfinchsnakeeel 1234567 dog This comes off the queue
12
12 Serve Front Rear 0 duckcatfinchsnakeeel 1234567 fish This comes off the queue
13
13 Append FrontRear 0 duckcatfinchsnakeeeltiger 1234567
14
14 Append FrontRear 0 duckcatfinchsnakeeeltiger 1234567 ape NO ROOM HERE ROOM HERE
15
15 Circular Implementation 0 7 1 2 3 4 5 6
16
16 Append Front 0 duckcatfinchsnakeeeltiger 1234567 ape Rear ape Circular Implementation
17
17 #define MAXQUEUE 20 struct QueueRec { int count; int front; int rear; float entry[MAXQUEUE]; }; typedef struct QueueRec Queue; Queue:...... entry: count: front: rear:
18
18 #ifndef QUEUEH #define QUEUEH #include #define MAXQUEUE 20 struct QueueRec { int count; int front; int rear; float entry[MAXQUEUE]; }; typedef struct QueueRec Queue; void intializeQueue(Queue* queuePtr); bool queueEmpty(const Queue* queuePtr); bool queueFull(const Queue* queuePtr); void append(Queue* queuePtr, float item); float serve(Queue* queuePtr); #endif
19
#include #include “queue.h” void initializeQueue(Queue* queuePtr) { queuePtr -> count = 0; queuePtr -> front = 0; queuePtr -> rear = MAXQUEUE-1; } rear:...... entry: Queue: addr of Queue queuePtr: 19 count: 0 front: 0 19
20
bool queueEmpty(const Queue* queuePtr) { if (queuePtr->count <= 0) { return true; } else { return false; } 20 bool queueFull(Queue* queuePtr) { if (queuePtr->count >= MAXQUEUE) { return true; } else { return false; }
21
void append(Queue* queuePtr, float item) { if (queueFull(queuePtr)) { fprintf(stderr, “Queue is full\n”); exit(1); } else { queuePtr->rear++; if (queuePtr->rear == MAXQUEUE) { queuePtr->rear = 0; } queuePtr->entry[queuePtr->rear] = item; queuePtr->count++; } 21
22
float serve(Queue* queuePtr) { float item; if (queueEmpty(queuePtr)) { fprintf(stderr, “Queue is empty\n”); exit(1); } else { item = queuePtr->entry[queuePtr->front]; queuePtr->front++; if (queuePtr->front == MAXQUEUE) { queuePtr->front = 0; } queuePtr->count--; } return item; } 22
23
23Revision Queue Main Operations Implementation. Revision: Reading Kruse: Chapter 4.1 to 4.2 Deitel & Deitel: Chapter 12.6 Standish: Chapter 7 Langsam: Chapter 4.1 Preparation Next lecture: Lists Kruse 4.5, 4.6, 4.8 Deitel & Deitel (2e) 12.1, 12.2, 12.4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.