cse1303 part a data structures and algorithms lecture a4 basic data structures continued queues l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
PowerPoint Presentation
Download Presentation

Loading in 2 Seconds...

play fullscreen
1 / 23

- PowerPoint PPT Presentation


  • 237 Views
  • Uploaded on

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

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

PowerPoint Slideshow about '' - MikeCarlo


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

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

basic data structures
Basic Data Structures
  • Stacks
  • Queues
  • Lists
overview
Overview
  • What is a Queue?
  • Queue Operations.
  • Applications.
  • Linear Implementation.
  • Circular Implementation.
append

Before

Front

Rear

After

Front

Rear

Append
serve

Before

Front

Rear

After

This comes off the queue

Rear

Front

Serve
operations
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
Applications
  • In operating systems, e.g. printer queues, process queues, etc.
  • Simulation programs.
  • Algorithms.
linear implementation
Linear Implementation

0

1

2

3

4

5

6

7

dog

fish

duck

cat

finch

Rear

Front

append9
Append

snake

0

1

2

3

4

5

6

7

dog

fish

duck

cat

finch

snake

Front

Rear

append10
Append

eel

0

1

2

3

4

5

6

7

dog

fish

duck

cat

finch

snake

eel

Front

Rear

serve11
Serve

0

1

2

3

4

5

6

7

fish

duck

cat

finch

snake

eel

Front

Rear

dog

This comes off the queue

serve12
Serve

0

1

2

3

4

5

6

7

duck

cat

finch

snake

eel

Rear

Front

fish

This comes off the queue

append13
Append

tiger

0

1

2

3

4

5

6

7

duck

cat

finch

snake

eel

tiger

Front

Rear

append14

ROOM HERE

NO ROOM

HERE

Append

ape

0

1

2

3

4

5

6

7

duck

cat

finch

snake

eel

tiger

Front

Rear

append16

Circular Implementation

Append

ape

0

1

2

3

4

5

6

7

ape

duck

cat

finch

snake

eel

tiger

Rear

Front

slide17

Queue:

#define MAXQUEUE 20

struct QueueRec

{

int count;

int front;

int rear;

float entry[MAXQUEUE];

};

typedef struct QueueRec Queue;

count:

front:

rear:

entry:

.

.

.

slide18

#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

#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

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

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

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