20130330

How to write cpp program for Circular Queue using Array


How to write C++ program for Circular Queue using Array
C++ program to implement Circular Queue using Array
Circular Queue implementation using Array
CPP program for implementing Circular Queue using Array


#include<iostream>
using namespace std;
#define MAXSIZE 4

class queue
{
private:
int cq[MAXSIZE];
int front, rear;
public:
queue()
{
int cq[MAXSIZE]={0};//error: array must be initialized with a brace-enclosed initializer
front= -1;
rear = -1;
}


void insert(int item)
{
cout<<"\n in insert.\t";
if( ( (front==0)&&(rear==MAXSIZE-1)%MAXSIZE ) || front==(rear+1)%MAXSIZE )
{
cout<<"Q is full.\n";
}
else
{
if(front==-1)
front=rear=0;
else
rear=(rear+1)%MAXSIZE;

cq[rear]=item;

cout<<"\n front = "<<front<<" rear = "<<rear<<endl;

}

}


void display()
{
int i;
for(i=0;i<MAXSIZE;i++)
{
cout<<cq[i]<<" ";
}
cout<<endl;
}


void deletee()
{
if(front==-1)
{
cout<<"Q is empty.\n";
}
else
{
int temp;
temp=cq[front];
cq[front]=0;
if(front==rear)//only 1 elt was there
front=rear=-1;//delete the single so q is empty
else
front=(front+1)%MAXSIZE;//if more than 1 elt then delete frm front

cout<<"\n Deleted elt is:"<<temp<<endl;
cout<<"\n front = "<<front<<" rear = "<<rear<<endl;
}
}

};
int main()
{
queue q;
q.insert(10);
q.insert(20);
q.insert(30);
q.insert(40);
q.display();

q.insert(111);//Q is full so make same place by deleting fronts
q.display();

q.deletee();
q.display();
q.insert(100);
q.display();



return 0;
}
/*
in insert.
front = 0 rear = 0

in insert.
front = 0 rear = 1

in insert.
front = 0 rear = 2

in insert.
front = 0 rear = 3
10 20 30 40

in insert. Q is full.
10 20 30 40

Deleted elt is 10.

front = 1 rear = 3
0 20 30 40

in insert.
front = 1 rear = 0
100 20 30 40
*/
Simples Way to learn Data Structure in C++ CPP or C

How to write cpp program for Circular Queue using Linked List



 /*C++ Program for Circular Queue implementation using linked list
Cpp program to implement Circular Queue using Linked List*/
C++ Program for implementing Circular Queue with Linked List

#include<iostream>
using namespace std;

class queue
{
private:
struct node
{
int data;
node* link;
};
node *front;
node *rear;

public:
void insert(int);
void display();
void del();
queue()
{

How to write cpp program for BST Binary Search Tree


/*BST-Binary Search Tree Program for  insertion and traversal
How to write C++ Program for BST-Binary Search Tree
Cpp program for BST Binary Search Tree implementation with class*/
-->
//bst-Binary Search tree
#include<iostream>
using namespace std;

class BST
{
private:
struct node
{
int data;
node *left;
node *right;
};
node* start;

public:
BST(){start=NULL;}
void insert(int data);
void traverse();
void inorder(node *);
void preorder(node *);
void postorder(node *);
};

void BST:: insert(int d)
{
node *newnode;
newnode=new node;
newnode->data=d;
newnode->left=NULL;
newnode->right=NULL;

node *ptr;
ptr=start;
if(start==NULL)
{
start=newnode;
}
else
{
while(ptr!=NULL)
{
if(d<ptr->data)
{
if(ptr->left==NULL)
{
ptr->left=newnode;
ptr=NULL;
}
else
{
ptr=ptr->left;
}
}
else
{
//if(d>ptr->data)
//{
if(ptr->right==NULL)
{
ptr->right=newnode;
ptr=NULL;
}
else
{
ptr=ptr->right;
}
// }
}
}
}
}

void BST::traverse()
{
cout<<"\n Inorder:\n";
inorder(start);
cout<<"\n Preorder:\n";
preorder(start);
cout<<"\n Postorder:\n";
postorder(start);

}
void BST::inorder(node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
cout<<ptr->data<<"\t";
inorder(ptr->right);
}
}
void BST::preorder(node *ptr)
{
if(ptr!=NULL)
{
cout<<ptr->data<<"\t";
preorder(ptr->left);
preorder(ptr->right);
}
}
void BST::postorder(node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->data<<"\t";

}
}


int main()
{
BST b;
b.insert(8);
b.insert(3);
b.insert(10);
b.insert(1);
b.insert(6);
b.insert(7);
b.insert(4);
b.insert(10);
b.insert(14);
b.insert(13);

b.traverse();




return 0;
}
/*
$ g++ bst_revision.cxx
$ ./a.out

Inorder:
1 3 4 6 7 8 10 10 13 14
Preorder:
8 3 1 6 4 7 10 10 14 13
Postorder:
1 4 7 6 3 13 14 10 10 8
*


Depth-first
  • Pre-order traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right)
  • In-order traversal sequence: A, B, C, D, E, F, G, H ,I (left, root, right)
  • Post-order traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root)
Breadth-first
  • Level-order traversal sequence: F, B, G, A, D, I, C, E, H
Inorder:
1 2 3 4 5 6 7 8 9
Pre order:
6 2 14 3 5 7 9 8
Post order:
1 3 5 4 2 8 9 7 6
Simples Way to learn Data Structure in C++ CPP or C

How to write cpp program to reverse a linked list



//C++ program for reversing single Linked list nodes 
Cpp program for implement reversing Single Linked List
Single linked list program for reversing the nodes
How to reverse linked list 
#include<iostream>
using namespace std;

struct node
{
node *next;
int data;
};
class ll
{
private:
node *start;
public:
ll(){start=NULL;}

void insert(int a)
{
cout<<"Inside Insert."<<endl;
node *newnode=new node;
newnode->data=a;
newnode->next=start;
start=newnode;
//return;
}
void display();
void del();
void reverse();
};

How to write sorted linked list program in cpp with class


How to write sorted linked list program in cpp with class
C++ Program for sorted single linked list with class
//sorted linked list latest
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *start=NULL;

void insert(int d)
{
node *newnode=new node;
newnode->data=d;
newnode->next=NULL;
//insert as first elt
if(start==NULL)
{