1 / 23

# CSE1303 Part A Data Structures and Algorithms Lecture A4 – Basic Data Structures – Continued (Queues) - PowerPoint PPT Presentation

CSE1303 Part A Data Structures and Algorithms Lecture A4 – Basic Data Structures – Continued (Queues) Basic Data Structures Stacks Queues Lists Overview What is a Queue? Queue Operations. Applications. Linear Implementation. Circular Implementation. Before Front Rear After Front

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

CSE1303 Part A Data Structures and Algorithms Lecture A4 – Basic Data Structures – Continued (Queues)

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## CSE1303 Part AData Structures and AlgorithmsLecture A4 – Basic Data Structures – Continued (Queues)

• Stacks

• Queues

• Lists

### Overview

• What is a Queue?

• Queue Operations.

• Applications.

• Linear Implementation.

• Circular Implementation.

Before

Front

Rear

After

Front

Rear

### Append

Before

Front

Rear

After

This comes off the queue

Rear

Front

### 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?

### Applications

• In operating systems, e.g. printer queues, process queues, etc.

• Simulation programs.

• Algorithms.

0

1

2

3

4

5

6

7

dog

fish

duck

cat

finch

Rear

Front

snake

0

1

2

3

4

5

6

7

dog

fish

duck

cat

finch

snake

Front

Rear

eel

0

1

2

3

4

5

6

7

dog

fish

duck

cat

finch

snake

eel

Front

Rear

### Serve

0

1

2

3

4

5

6

7

fish

duck

cat

finch

snake

eel

Front

Rear

dog

This comes off the queue

### Serve

0

1

2

3

4

5

6

7

duck

cat

finch

snake

eel

Rear

Front

fish

This comes off the queue

tiger

0

1

2

3

4

5

6

7

duck

cat

finch

snake

eel

tiger

Front

Rear

ROOM HERE

NO ROOM

HERE

ape

0

1

2

3

4

5

6

7

duck

cat

finch

snake

eel

tiger

Front

Rear

### Circular Implementation

0

7

6

1

5

2

4

3

Circular Implementation

### Append

ape

0

1

2

3

4

5

6

7

ape

duck

cat

finch

snake

eel

tiger

Rear

Front

Queue:

#define MAXQUEUE 20

struct QueueRec

{

int count;

int front;

int rear;

float entry[MAXQUEUE];

};

typedef struct QueueRec Queue;

count:

front:

rear:

entry:

.

.

.

#ifndef QUEUEH

#define QUEUEH

#include <stdbool.h>

#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

#include <stdio.h>

#include <stdlib.h>

#include “queue.h”

void initializeQueue(Queue* queuePtr)

{

queuePtr -> count = 0;

queuePtr -> front = 0;

queuePtr -> rear = MAXQUEUE-1;

}

Queue:

0

count:

0

front:

queuePtr:

19

rear:

entry:

.

.

.

19

bool queueEmpty(const Queue* queuePtr)

{

if (queuePtr->count <= 0)

{

return true;

}

else

{

return false;

}

}

bool queueFull(Queue* queuePtr)

{

if (queuePtr->count >= MAXQUEUE)

{

return true;

}

else

{

return false;

}

}

20

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

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

### Revision

• Queue

• Main Operations

• Implementation.

• 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