implementing a queue as a linked structure
Download
Skip this Video
Download Presentation
Implementing a Queue as a Linked Structure

Loading in 2 Seconds...

play fullscreen
1 / 21

Implementing a Queue as a Linked Structure - PowerPoint PPT Presentation


  • 105 Views
  • Uploaded on

Implementing a Queue as a Linked Structure. CS 308 – Data Structures. Implementing queues using arrays. Simple implementation The size of the queue must be determined when a stack object is declared Space is wasted if we use less elements

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 'Implementing a Queue as a Linked Structure' - keena


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
implementing queues using arrays
Implementing queues using arrays
  • Simple implementation
  • The size of the queue must be determined when a stack object is declared
  • Space is wasted if we use less elements
  • We cannot "enqueue" more elements than the array can hold
implementing queues using linked lists
Implementing queues using linked lists
  • Allocate memory for each new element dynamically
  • Link the queue elements together
  • Use two pointers, qFront and qRear, to mark the front and rear of the queue
queue class specification
Queue class specification

// forward declaration of NodeType (like function prototype)

template

struct NodeType;

template

class QueueType {

public:

QueueType();

~QueueType();

void MakeEmpty();

bool IsEmpty() const;

bool IsFull() const;

void Enqueue(ItemType);

void Dequeue(ItemType&);

private:

NodeType* qFront;

NodeType* qRear;

};

enqueuing empty queue
Enqueuing (empty queue)
  • We need to make qFront point to the new node also

qFront = NULL

New Node

qRear = NULL

newNode

function enqueue
Function Enqueue

template

void QueueType::Enqueue(ItemType newItem)

{

NodeType* newNode;

newNode = new NodeType;

newNode->info = newItem;

newNode->next = NULL;

if(qRear == NULL)

qFront = newNode;

else

qRear->next = newNode;

qRear = newNode;

}

dequeueing the queue contains only one element
Dequeueing (the queue contains only one element)
  • We need to reset qRear to NULL also

qFront

After dequeue:

qFront = NULL

qRear = NULL

Node

qRear

function dequeue
Function Dequeue

template

void QueueType::Dequeue(ItemType& item)

{

NodeType* tempPtr;

tempPtr = qFront;

item = qFront->info;

qFront = qFront->next;

if(qFront == NULL)

qRear = NULL;

delete tempPtr;

}

qrear qfront revisited
qRear, qFront revisited
  • The relative positions of qFront and qRear are important!
other queue functions
Other Queue functions

template

QueueType::QueueType()

{

qFront = NULL;

qRear = NULL;

}

template

void QueueType::MakeEmpty()

{

NodeType* tempPtr;

while(qFront != NULL) {

tempPtr = qFront;

qFront = qFront->next;

delete tempPtr;

}

qRear=NULL;

}

other queue functions cont
Other Queue functions (cont.)

template

bool QueueType::IsEmpty() const

{

return(qFront == NULL);

}

template

bool QueueType::IsFull() const

{

NodeType* ptr;

ptr = new NodeType;

if(ptr == NULL)

return true;

else {

delete ptr;

return false;

}

}

other queue functions cont14
Other Queue functions (cont.)

template

QueueType::~QueueType()

{

MakeEmpty();

}

comparing queue implementations
Comparing queue implementations
  • Memory requirements
    • Array-based implementation
      • Assume a queue (size: 100) of strings (80 bytes each)
      • Assume indices take 2 bytes
      • Total memory: (80 bytes x 101 slots) + (2 bytes x 2 indexes) = 8084 bytes
    • Linked-list-based implementation
      • Assume pointers take 4 bytes
      • Total memory per node: 80 bytes + 4 bytes = 84 bytes
comparing queue implementations18
Comparing queue implementations

(cont.)

  • Memory requirements
    • Array-based implementation
      • Assume a queue (size: 100) of short integers (2 bytes each)
      • Assume indices take 2 bytes
      • Total memory: (2 bytes x 101 slots) + (2 bytes x 2 indexes) = 206 bytes
    • Linked-list-based implementation
      • Assume pointers take 4 bytes
      • Total memory per node: 2 bytes + 4 bytes = 6 bytes
ad