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

Linear Single Linked List C++ part II

Posted by Admin On April - 15 - 2009

This part of Linear Single Linked List will tell you how to delete a node at any index. It’s simple like adding a data, you just have to change the node pointer to other that next to the node that you want to delete. If you want to delete the most front data, you have to set the head onto the next node and delete the first node. Looks like front, when we want to delete the last node, we have to set the 2nd node from the last to tail and delete the last data/ node.

Delete any index in Single Linked List

Delete any index in Single Linked List

Delete first data on Single Linked List

Delete first data on Single Linked List

Delete the last index of Single Linked List

Delete the last index of Single Linked List

The code is quite simple rather than add code, I think. Human kind is rather destroying than creating and managing these days.

void DeleteLastIndex()
{
node *temp;
temp=head;
while(temp->nget()!=tail)
{
temp=temp->nget();
}
delete tail;
tail = temp;
tail->nset(NULL);
}

void DeleteAnyIndex(int index)
{
node *temp,*dnew;
temp=head;
dnew=head;
int i=0;
for(i=0;i<index;i++)
{
temp=temp->nget();
}
for(i=0;i<x+1;i++)
{
dnew=dnew->nget();
}
temp->nset(dnew->nget());
delete dnew;
}

void DeleteFront()
{
node *temp;
temp=head;
head=head->nget();
delete temp;
}

I think this code should works fine…. I think, LoL.

Popularity: 51% [?]

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

Linear Single Linked List C++

Posted by Admin On March - 26 - 2009

Hey, back to programming again in data structure. It’s now about Linked List, a linear single linked list it’s about to be explained in this post. We can imagine this like a chain, every data has a link to another data. Single Linked List is different from array. In array, we can only insert a new data to the last index after the last data. We can not insert at any place we want. So, the linked list was made.

linear-single-link-list

The Node

At linked list, we add a header, we called it node. This node is used for data to be linked to another data. It’s like each one person has one another person’s address. So, this is the advantage to use linked list instead of array. If a new data want to be placed between another data, we just have to change the address/ node of the data.

linear-single-link-list-add

Insert data

Feel confused? Let’s get to the code. I will make a simple linear single linked list that can insert a data at any index.

//====START====//

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

class node
{
private:
 int data;
 node * next;
public:
 node()
 {
  next=NULL;
 }
  int get()
 {
  return data;
 }
 void set(int x)
 {
  data=x;
 }
 node * nget()
 {
  return (next);
 }
 void nset(node * nextnode)
 {
  next=nextnode;
 }
};

class Link
{
private:
 node * head, * tail;
public:
 Link()
 {
  head=NULL;
  tail=NULL;
 }
 void add(int data)
 {
  node * baru;
  baru=new node;
  baru->set(data);
  if(!head)
  {
   head=baru;
   tail=head;
  }
  else
  {
   tail->nset(baru);
   tail=baru;
  }
 }
 void view()
 {
  node * temp;
  temp=head;
  if(temp)
  {
   while(temp)
   {
    printf("%d",temp->get());
    temp=temp->nget();
   }
  }
 }
 void addmid(int index,int data)
 {
  int i=0;
  node *temp,*ndata;
  temp=head;
  ndata=new node;
  ndata->set(data);
  while(i<index)
  {
   temp=temp->nget();
   i++;
  }
  ndata->nset(temp->nget());
  temp->nset(ndata);
 }

};

//====END====//

It’s done. You can start to implement this code in your main cpp.

Popularity: 38% [?]

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