Queues: A First-Class Abstract Data Type, Slides of Computer Science

An overview of queues as an abstract data type (adt). It explains the concept of queues, their insert and remove operations, and their implementation using both linked lists and arrays. The document also discusses the first-class nature of queues as a data type.

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dharmaraaj
dharmaraaj 🇮🇳

4.4

(68)

145 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Abstract Data Types
Queues
Docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Queues: A First-Class Abstract Data Type and more Slides Computer Science in PDF only on Docsity!

Abstract Data Types

Queues

Queues

  • Queue
    • Similar to a supermarket checkout line
    • First-in, first-out (FIFO)
    • Nodes are removed only from the head
    • Nodes are inserted only at the tail
  • Insert and remove operations
    • Enqueue (insert) and dequeue (remove)
    • Queue Model
      • queue is a list, with insertion done only at one end and deletion done at the other end.
    • Linked list implementation of queues
      • operating as a list
      • constant time for enqueue & dequeue (keeping pointer to both the head and tail of the list)

FIFO queue ADT interface

template

class QUEUE

private:

// Implementation-dependent code

public:

QUEUE(int);

int empty();

void put(Item);

Item get();

FIFO queue linked-list implementation template class QUEUE { private: struct node { Item item; node* next; node(Item x) { item = x; next = 0; } }; typedef node *link; link head, tail; public: QUEUE(int) { head = 0; } int empty() const { return head == 0; } void put(Item x) { link t = tail; tail = new node(x); if (head == 0) head = tail; else t->next = tail; } Item get() { Item v = head->item; link t = head->next; delete head; head = t; return v; } };

First-class ADT

  • Sedgewick Definition 4.4:

A first-class data type is one for which we can have potentially

many different instances, and which we can assign to variables

which we declare to hold the instances.

Our Fraction ADT

is a first-class

ADT.

First-class Queue ADT

template

class QUEUE

private:

// Implementation-dependent code

public:

QUEUE(int);

QUEUE(const QUEUE&);

QUEUE& operator=(const QUEUE&);

~QUEUE();

int empty() const;

void put(Item);

Item get();

Notice how each and every

interface operations now includes

a references to a particular Q. We

can create as many queues as we

need.