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 l.jpg
Download
1 / 23

  • 222 Views
  • Uploaded on
  • Presentation posted in: Pets / Animals

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.

Download Presentation

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

An Image/Link below is provided (as is) to download presentation

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

Presentation Transcript


Cse1303 part a data structures and algorithms lecture a4 basic data structures continued queues l.jpg

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


Basic data structures l.jpg

Basic Data Structures

  • Stacks

  • Queues

  • Lists


Overview l.jpg

Overview

  • What is a Queue?

  • Queue Operations.

  • Applications.

  • Linear Implementation.

  • Circular Implementation.


Append l.jpg

Before

Front

Rear

After

Front

Rear

Append


Serve l.jpg

Before

Front

Rear

After

This comes off the queue

Rear

Front

Serve


Operations l.jpg

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 l.jpg

Applications

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

  • Simulation programs.

  • Algorithms.


Linear implementation l.jpg

Linear Implementation

0

1

2

3

4

5

6

7

dog

fish

duck

cat

finch

Rear

Front


Append9 l.jpg

Append

snake

0

1

2

3

4

5

6

7

dog

fish

duck

cat

finch

snake

Front

Rear


Append10 l.jpg

Append

eel

0

1

2

3

4

5

6

7

dog

fish

duck

cat

finch

snake

eel

Front

Rear


Serve11 l.jpg

Serve

0

1

2

3

4

5

6

7

fish

duck

cat

finch

snake

eel

Front

Rear

dog

This comes off the queue


Serve12 l.jpg

Serve

0

1

2

3

4

5

6

7

duck

cat

finch

snake

eel

Rear

Front

fish

This comes off the queue


Append13 l.jpg

Append

tiger

0

1

2

3

4

5

6

7

duck

cat

finch

snake

eel

tiger

Front

Rear


Append14 l.jpg

ROOM HERE

NO ROOM

HERE

Append

ape

0

1

2

3

4

5

6

7

duck

cat

finch

snake

eel

tiger

Front

Rear


Circular implementation l.jpg

Circular Implementation

0

7

6

1

5

2

4

3


Append16 l.jpg

Circular Implementation

Append

ape

0

1

2

3

4

5

6

7

ape

duck

cat

finch

snake

eel

tiger

Rear

Front


Slide17 l.jpg

Queue:

#define MAXQUEUE 20

struct QueueRec

{

int count;

int front;

int rear;

float entry[MAXQUEUE];

};

typedef struct QueueRec Queue;

count:

front:

rear:

entry:

.

.

.


Slide18 l.jpg

#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


Slide19 l.jpg

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

addr ofQueue

19

rear:

entry:

.

.

.

19


Slide20 l.jpg

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


Slide21 l.jpg

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


Slide22 l.jpg

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 l.jpg

Revision

Revision: Reading

  • 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


  • Login