TECH GUIDE BLOG

Tech Guide Programming Tutorial Tips Tricks

Circular Queue C++

Posted by Admin On July - 16 - 2009

Let’s get to the point, what’s the different beetwen Circular and Linear? Just like previous post, the different is the head is always move around or you can say dynamic. The first and the last data is likely become linked to another one. If we do enqueue, the tail will be point to the new data. If you can see the picture below, you will know that the data are never move like linear queue, but the head node. This head node always move when you do dequeue and enqueue new data.

Circular Queue

Circular Queue

While we using circular queue, we can use mod (%) to help us for the head movement. Here is the example of Circular Queue. This code is the class of Queue, it’s just made for keep integer data up to small size, it’s just for simple example program of Circular Queue.

#include <iostream.h>
#include <conio.h>
#include <windows.h>

#define MAX 5

class Cqueue
{
private:
	int element[MAX];
	int head;
	int tail;
public:
	Cqueue()
	{
		head=0;
		tail=0;
	}
	void enqueue(int x)
	{
		if(!full())
		{
			element[(head+tail)%MAX]=x;
			tail++;
		}
	}
	int dequeue()
	{
		if(!empty())
		{
			int temp;
			temp=element[head];
			head++;
			head=head%MAX;
			tail--;
			return temp;
		}
	}
	int full()
	{
		if(tail==MAX)
			return 1;
		else
			return 0;
	}
	int empty()
	{
		if(tail==0)
			return 1;
		else
			return 0;
	}
	void view()
	{
		for(int i=0;i<tail;i++)
			cout<<element[(i+head)%MAX]<<endl;
		getch();
	}
};

Popularity: 43% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • MySpace
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter

Queue C++ Data Structure Code

Posted by Admin On February - 15 - 2009

Another data structures type is queue. Adding data is the same as stack, it’s called enqueue. The different between stack is queue use “first in first out”. The first data will be kicked out first when you dequeue it (getting data from queue). There are 2 pointers we can use in queue. Head is pointer that point the first data and tail to point the last data. This picture below may help you to understand.

linear_queue

Linear Queue

There are 2 types of queue, linear and circular. A circular queue has a shape like donut, so we can say the queue is connected. The different is at head position, a linear queue has a static head position, and a circular has a dynamic head position every time you do dequeue.

Circular Queue

Circular Queue

This time, I will only tell you how to make a linear queue class in header :D, it looks like stack application at previous post, so it’s unnecessary to write the main code.

//============== start of code ==============//
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define MAX 255

class lqueue
{
private:
	int data[MAX];
	int tail; // linear queue has a static head, assume head = 0
public:
	lqueue()
	{
		tail = 0;
	}
	void enqueue(int value)
	{
		if (!full())
		{
			data[tail] = value;
			tail++;
		}
	}
	int dequeue()
	{
		if (!empty())
		{
			int temp;
			temp = data[head];
			for (int i = 0; i < tail; i++)
			{
				data[i] = data[i+1];
			}
			tail--;
			return temp;
		}
	}
	bool full()
	{
		if (tail == MAX)
			return 1;
		else
			return 0;
	}
	bool empty()
	{
		if (tail == 0)
			return 1;
		else
			return 0;
	}
	void view()
	{
		for (int i = 0; i < tail; i++)
			printf("%d ",data[i]);
		getch();
	}
};
//=============== end of code ===============//

It’s done. I think it should work fine :))

Popularity: 24% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • MySpace
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter