cs251 week2 - int last; int n; public: circularList(); void...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
In the same way that you can implement a stack using an array, you can also implement a queue using an array this is called “Circular queue” N=of elements in queue first->points to first element in queue (oldest) last->points to last element in queue (newest) Elements are removed (dequeued) from “first” Elements are added (enqueued) after “last” When first or last reach end of array, they will wrap around Theres a MAX # of elements in the queue #define MAX 20 class CircularList{ Int array[max]; int first;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int last; int n; public: circularList(); void enqueue(int data); int dequeue(); } CircularList.cpp #include circularList.h CircularList::CircularList(){ first = 0; last = 0; n=0; } Void CircularList:: enqeue(int data){ //check space if(n==MAX){ printf(Queue full\n); exit(1); } queue[last] = data; last = (last+1)%max; //wrap around } int CircularList::dequeue(){ if(n==0){ printf(List empty\n); exit(1); } int data = queue[first]; first=first+1}%MAX; return data; }...
View Full Document

This note was uploaded on 02/04/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 2

cs251 week2 - int last; int n; public: circularList(); void...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online