Wednesday, October 24, 2007
solved binary search tree and dequeue
hi frineds, the BST and DEQUEUE have been successfully solved check them.... and I am glad to announce the upcoming programs avl trees ......FRIENDS MAKE THIS SITE A GOOD PROGRAMMING ARENA ASK WHAT YOU WANT AND GET THE RESULT WITHIN NO TIME EVERY ONE IS WELCOME and if you want to take part in this program just mail me at vdineshsreddy@yahoo.co.in or just ring to +919866700961.... happy programming
Tuesday, October 23, 2007
7.Dequeue //executed//
//hi friends this is a perfectly executed dequeue program earlier errors have been corrected
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
template<class t>
class dq
{
struct node
{
t data;
struct node *flink;
struct node *rlink;
};
typedef struct node node;
int front,rear,cnt;
struct node *cur,*fnode,*lnode,*temp;
public:
dq()
{
front=rear=-1;
cnt=0;
fnode=lnode=NULL;
}
void inslft(t item)
{
#include<conio.h>
#include<stdlib.h>
#define NULL 0
template<class t>
class dq
{
struct node
{
t data;
struct node *flink;
struct node *rlink;
};
typedef struct node node;
int front,rear,cnt;
struct node *cur,*fnode,*lnode,*temp;
public:
dq()
{
front=rear=-1;
cnt=0;
fnode=lnode=NULL;
}
void inslft(t item)
{
cur=fnode;
for(int i=0;i<cnt-1;i++)
cur=cur->flink;
temp=new node;
temp->data=item;
temp->flink=NULL;
temp->rlink=NULL;
if(rear==-1)
{
fnode=temp;
front++;
}
else
{
cur->flink=temp;
temp->rlink=cur;
lnode=temp;
}
rear++;
cnt++;
}
void insrt(t item)
{
cur=lnode;
for(int i=0;i<cnt-1;i++)
cur=cur->rlink;
temp=new node;
temp->data=item;
temp->flink=NULL;
temp->rlink=NULL;
if(front==-1)
{
lnode=temp;
rear++;
}
else
{
cur->rlink=temp;
temp->flink=cur;
fnode=temp;
}
front++;
cnt++;
}
void remvlft()
{
if(front<0)
{
cout<<"dequeue is empty";
return;
}
cur=fnode;
cout<<"the deleted element="<<cur->data;
fnode=cur->flink;
fnode->rlink=NULL;
delete cur;
cnt--;
rear--;
if(rear==-1)
front=-1;
}
void remvrt( )
{
if(rear<0) {
cout<<"dequeue is empty";
return;
}
cur=lnode;
cout<<"the deleted element="<<cur->data;
lnode=cur->rlink;
lnode->flink=NULL;
delete cur;
cnt--;
front--;
if(front==-1)
rear=-1;
}
void displ2r( )
{
if(cnt==0){
cout<<"dequeue is empty \n";
return;
}
cout<<"the element of dequeue are \n";
cur=fnode;
while(cur!=NULL)
{
cout<<cur->data<<" ";
cur=cur->flink;
}
}
void dispr2l( )
{
if(cnt==0) {
cout<<"dequeue empty";
return;
}
cout<<"the elements are \n:";
cur=lnode;
while(cur!=NULL)
{
cout<<cur->data<<" ";
cur=cur->rlink;
}
}
void size( )
{
if(cnt==0)
{
cout<<"dequeue is empty \n";
return;
}
cout<<"the size of dq="<<cnt;
}
};
void main()
{
dq<int>q;
int ch,element;
while(1)
{
clrscr();
cout<<"main menu \n";
cout<<"1.insert node at left end \n";
cout<<"2.insert node at right end \n";
cout<<"3.delete node at left end";
cout<<"\n4.delete node at right end";
cout<<"\n5.display dq form left to right";
cout<<"\n6.display dq from right to left";
cout<<"\n7.size";
cout<<"8.exit";
cout<<"\nEnter your choice \n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter new element \n";
cin>>element;
q.inslft(element);
getch();
break;
case 2:
cout<<"enter new element";
cin>>element;
q.insrt(element);
getch();
break;
case 3:
q.remvlft();
//cout<<"deleted element";
getch();
break;
case 4:
q.remvrt();
//cout<<"deleted element"<<element;
getch();
break;
case 5:
q.displ2r();
getch();
break;
case 6:
q.dispr2l();
getch();
break;
case 7:
q.size();
getch();
break;
case 8:
exit(0);
}
}
getch( );
}
for(int i=0;i<cnt-1;i++)
cur=cur->flink;
temp=new node;
temp->data=item;
temp->flink=NULL;
temp->rlink=NULL;
if(rear==-1)
{
fnode=temp;
front++;
}
else
{
cur->flink=temp;
temp->rlink=cur;
lnode=temp;
}
rear++;
cnt++;
}
void insrt(t item)
{
cur=lnode;
for(int i=0;i<cnt-1;i++)
cur=cur->rlink;
temp=new node;
temp->data=item;
temp->flink=NULL;
temp->rlink=NULL;
if(front==-1)
{
lnode=temp;
rear++;
}
else
{
cur->rlink=temp;
temp->flink=cur;
fnode=temp;
}
front++;
cnt++;
}
void remvlft()
{
if(front<0)
{
cout<<"dequeue is empty";
return;
}
cur=fnode;
cout<<"the deleted element="<<cur->data;
fnode=cur->flink;
fnode->rlink=NULL;
delete cur;
cnt--;
rear--;
if(rear==-1)
front=-1;
}
void remvrt( )
{
if(rear<0) {
cout<<"dequeue is empty";
return;
}
cur=lnode;
cout<<"the deleted element="<<cur->data;
lnode=cur->rlink;
lnode->flink=NULL;
delete cur;
cnt--;
front--;
if(front==-1)
rear=-1;
}
void displ2r( )
{
if(cnt==0){
cout<<"dequeue is empty \n";
return;
}
cout<<"the element of dequeue are \n";
cur=fnode;
while(cur!=NULL)
{
cout<<cur->data<<" ";
cur=cur->flink;
}
}
void dispr2l( )
{
if(cnt==0) {
cout<<"dequeue empty";
return;
}
cout<<"the elements are \n:";
cur=lnode;
while(cur!=NULL)
{
cout<<cur->data<<" ";
cur=cur->rlink;
}
}
void size( )
{
if(cnt==0)
{
cout<<"dequeue is empty \n";
return;
}
cout<<"the size of dq="<<cnt;
}
};
void main()
{
dq<int>q;
int ch,element;
while(1)
{
clrscr();
cout<<"main menu \n";
cout<<"1.insert node at left end \n";
cout<<"2.insert node at right end \n";
cout<<"3.delete node at left end";
cout<<"\n4.delete node at right end";
cout<<"\n5.display dq form left to right";
cout<<"\n6.display dq from right to left";
cout<<"\n7.size";
cout<<"8.exit";
cout<<"\nEnter your choice \n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter new element \n";
cin>>element;
q.inslft(element);
getch();
break;
case 2:
cout<<"enter new element";
cin>>element;
q.insrt(element);
getch();
break;
case 3:
q.remvlft();
//cout<<"deleted element";
getch();
break;
case 4:
q.remvrt();
//cout<<"deleted element"<<element;
getch();
break;
case 5:
q.displ2r();
getch();
break;
case 6:
q.dispr2l();
getch();
break;
case 7:
q.size();
getch();
break;
case 8:
exit(0);
}
}
getch( );
}
Love shopping? Click here to find the best shops in every city.
perfectly executed BINARY SEARCH TREE
//hi friends at last here it is the perfectly executed economical BST program
# include <conio.h>
# include <process.h>
# include <iostream.h>
# include <alloc.h>
static int cnt;
struct node
{
int ele;
node *left;
node *right;
};
# include <process.h>
# include <iostream.h>
# include <alloc.h>
static int cnt;
struct node
{
int ele;
node *left;
node *right;
};
typedef struct node *nodeptr;
class bstree
{
public:
void insert(int,nodeptr &);
void del(int,nodeptr &);
int deletemin(nodeptr &);
void find(int,nodeptr &);
void preorder(nodeptr);
void inorder(nodeptr);
void postorder(nodeptr);
class bstree
{
public:
void insert(int,nodeptr &);
void del(int,nodeptr &);
int deletemin(nodeptr &);
void find(int,nodeptr &);
void preorder(nodeptr);
void inorder(nodeptr);
void postorder(nodeptr);
};
void bstree::insert(int x,nodeptr &p)
{
if (p==NULL)
{
p = new node;
p->ele=x;
p->left=NULL;
p->right=NULL;
}
else
{
if (x < p->ele)
insert(x,p->left);
else if (x>p->ele)
insert(x,p->right);
else
cout<<"Element already Exits !";
}
{
if (p==NULL)
{
p = new node;
p->ele=x;
p->left=NULL;
p->right=NULL;
}
else
{
if (x < p->ele)
insert(x,p->left);
else if (x>p->ele)
insert(x,p->right);
else
cout<<"Element already Exits !";
}
}
void bstree:: del(int x,nodeptr &p)
{
nodeptr d;
if (p==NULL)
cout<<"Element not found ";
else if (x < p->ele)
del(x,p->left);
else if (x > p->ele)
del(x,p->right);
else if ((p->left == NULL) && (p->right ==NULL))
{
d=p;
free(d);
p=NULL;
}
else if (p->left == NULL)
{
d=p;
free(d);
p=p->right;
}
else if (p->right ==NULL)
{
d=p;
p=p->left;
free(d);
}
else
p->ele=deletemin(p->right);
}
{
nodeptr d;
if (p==NULL)
cout<<"Element not found ";
else if (x < p->ele)
del(x,p->left);
else if (x > p->ele)
del(x,p->right);
else if ((p->left == NULL) && (p->right ==NULL))
{
d=p;
free(d);
p=NULL;
}
else if (p->left == NULL)
{
d=p;
free(d);
p=p->right;
}
else if (p->right ==NULL)
{
d=p;
p=p->left;
free(d);
}
else
p->ele=deletemin(p->right);
}
int bstree::deletemin(nodeptr &p)
{
int c;cnt=0;
if (p->left == NULL)
{
c=p->ele;
p=p->right;
return c;
}
else
c=deletemin(p->left);
return c;
}
void bstree::find(int x,nodeptr &p)
{
if (p==NULL)
cout<<"Element not found !";
else
{
if (x <p->ele)
find(x,p->left);
else if ( x> p->ele)
find(x,p->right);
else
cout<<"Element Found !";
}
}
void bstree::preorder(nodeptr p)
{
if (p!=NULL)
{
cout<<p->ele<<"-->";
preorder(p->left);
preorder(p->right);
}
}
void bstree::inorder(nodeptr p)
{
{
int c;cnt=0;
if (p->left == NULL)
{
c=p->ele;
p=p->right;
return c;
}
else
c=deletemin(p->left);
return c;
}
void bstree::find(int x,nodeptr &p)
{
if (p==NULL)
cout<<"Element not found !";
else
{
if (x <p->ele)
find(x,p->left);
else if ( x> p->ele)
find(x,p->right);
else
cout<<"Element Found !";
}
}
void bstree::preorder(nodeptr p)
{
if (p!=NULL)
{
cout<<p->ele<<"-->";
preorder(p->left);
preorder(p->right);
}
}
void bstree::inorder(nodeptr p)
{
if (p!=NULL)
{
inorder(p->left);
cout<<p->ele<<"-->";cnt++;
inorder(p->right);
}
}
{
inorder(p->left);
cout<<p->ele<<"-->";cnt++;
inorder(p->right);
}
}
void bstree::postorder(nodeptr p)
{
if (p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->ele<<"-->";
}
}
int main()
{
int ch,x;
bstree bst;
char c='y';
nodeptr root;
root=NULL;
do
{
{
if (p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->ele<<"-->";
}
}
int main()
{
int ch,x;
bstree bst;
char c='y';
nodeptr root;
root=NULL;
do
{
clrscr();
cout<<" Binary Search Tree";
cout<<"\n1.Insertion\n2.Deletion\n3.Find";
cout<<"4.Preorder\n5.Inorder\n6.Postorder\n7.Size";
cout<<"\nEnter your choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<" 1.Insertion";
cout<<"Enter Node Element : ";
cin>>x;
bst.insert(x,root);
break;
cout<<" Binary Search Tree";
cout<<"\n1.Insertion\n2.Deletion\n3.Find";
cout<<"4.Preorder\n5.Inorder\n6.Postorder\n7.Size";
cout<<"\nEnter your choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<" 1.Insertion";
cout<<"Enter Node Element : ";
cin>>x;
bst.insert(x,root);
break;
case 2:
cout<<" 2.Deletion";
cout<<"Enter Element to Delete ?";
cin>>x;
bst.del(x,root);
break;
case 3:
cout<<" 3.Find";
cout<<"Enter the element to Search ?";
cin>>x;
bst.find(x,root);
break;
cout<<" 2.Deletion";
cout<<"Enter Element to Delete ?";
cin>>x;
bst.del(x,root);
break;
case 3:
cout<<" 3.Find";
cout<<"Enter the element to Search ?";
cin>>x;
bst.find(x,root);
break;
case 4:
cout<<" 4.Preorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nPreorder traversal is :";
bst.preorder(root);
}
break;
cout<<" 4.Preorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nPreorder traversal is :";
bst.preorder(root);
}
break;
case 5:
cout<<" 5.Inorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nInorder traversal is :";
bst.inorder(root);
}
break;
cout<<" 5.Inorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nInorder traversal is :";
bst.inorder(root);
}
break;
case 6:
cout<<" 6.Postorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nPostorder traversal is :";
bst.postorder(root);
}
break;
case 7: cout<<"\nnumber of nodes :"<<cnt;
break;
default:cout<<"invalid option..";
break;
}
cout<<"Continue (y/n) ?";
cin>>c;
}while (c!='n');
return 0;
}
cout<<" 6.Postorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nPostorder traversal is :";
bst.postorder(root);
}
break;
case 7: cout<<"\nnumber of nodes :"<<cnt;
break;
default:cout<<"invalid option..";
break;
}
cout<<"Continue (y/n) ?";
cin>>c;
}while (c!='n');
return 0;
}
Chat on a cool, new interface. No download required. Click here.
Monday, October 22, 2007
UPDATED
Hi, friends I am happy for the response you gave... it amazed me that about 100 visits have came only on this day through different parts of the world I am really happy for that, with that tremondous response I am successful to complete BINARY SEARCH TREE for both c++ compiler and visual c++ compiler....HAPPY PROGRAMMING GUYS
Sunday, October 21, 2007
updated dqueue and binary search tree
Hi friends, at last i could solve the problem and the programs from 1 to 5 here are clean and bug free..... I wish u could enjoy them... If any one found any bugs or difficulty accessing the site mail me to vdineshsreddy@yahoo.co.in or message me to 9866700961.......
NOTE:if any one solved the programs of DEQUEUE and BINARY SEARCH TREE please mail or message, and also mail the program, so that you can help otheres...... and finally i thank my friend ram koteshwar rao who helped me to solve queue using linked list problem...... bye friends happy programming.....
I appreciate one of my friends who mailed dequeue and binary search tree programs but I remind u that they are not EXECUTED.
NOTE:if any one solved the programs of DEQUEUE and BINARY SEARCH TREE please mail or message, and also mail the program, so that you can help otheres...... and finally i thank my friend ram koteshwar rao who helped me to solve queue using linked list problem...... bye friends happy programming.....
I appreciate one of my friends who mailed dequeue and binary search tree programs but I remind u that they are not EXECUTED.
Friday, October 19, 2007
5.circular queue
/*circular queue*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define null 0
#define mxs 5
template<class t>
class cq
{
int cnt,front,rear;
t *q;
public:cq(){front=rear=0;cnt=0;}
void addcq(t ele)
{
int t1;
t1=(rear+1)%mxs;
if(t1==front)
{cout<<"q is full";return;}
else{rear=t1;q[rear]=ele;}
}
void delcq()
{if(rear==front)
{cout<<"q is empty";return;
}
elsefront=(front+1)%mxs;
cout<<"removed ele:"<<q[front];
}
void display()
{cout<<"the circular q elements";
if(rear>front)
{
for(int i=front+1;i<=rear;i++)
cout<<q[i]<<" ";
}
else
{
for(int i=front+1;i<=mxs;i++)
cout<<q[i]<<" ";
for(i=0;i<=rear;i++)
cout<<q[i]<<" ";
}
}
};
void main()
{
cq<int>x;
int ele,ch;
while(1)
{
clrscr();
cout<<"***main menu***";
cout<<"\n1.insert\n2.delete\n3.display\n4.exit\n";
cout<<"enter u r choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter new ele";
cin>>ele;
x.addcq(ele);
break;
case 2:x.delcq();
break;
case 3:x.display();
break;
case 4:exit(1);
break;
}
getch();
}
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define null 0
#define mxs 5
template<class t>
class cq
{
int cnt,front,rear;
t *q;
public:cq(){front=rear=0;cnt=0;}
void addcq(t ele)
{
int t1;
t1=(rear+1)%mxs;
if(t1==front)
{cout<<"q is full";return;}
else{rear=t1;q[rear]=ele;}
}
void delcq()
{if(rear==front)
{cout<<"q is empty";return;
}
elsefront=(front+1)%mxs;
cout<<"removed ele:"<<q[front];
}
void display()
{cout<<"the circular q elements";
if(rear>front)
{
for(int i=front+1;i<=rear;i++)
cout<<q[i]<<" ";
}
else
{
for(int i=front+1;i<=mxs;i++)
cout<<q[i]<<" ";
for(i=0;i<=rear;i++)
cout<<q[i]<<" ";
}
}
};
void main()
{
cq<int>x;
int ele,ch;
while(1)
{
clrscr();
cout<<"***main menu***";
cout<<"\n1.insert\n2.delete\n3.display\n4.exit\n";
cout<<"enter u r choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter new ele";
cin>>ele;
x.addcq(ele);
break;
case 2:x.delcq();
break;
case 3:x.display();
break;
case 4:exit(1);
break;
}
getch();
}
}
4.queue using linked list
/*.........queue using linked list......... */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int ele;
template<class t>
class queue
{
struct node
{ t info;
struct node *next;
};
typedef struct node node;
node *front, *rear;
node *create(){return new node;}
public: queue() { front=NULL; rear=NULL; }
void insert (t ele)
{
node *temp=create();
if(rear==NULL&&front==NULL)
{ temp->info=ele;
temp->next=rear;
rear=temp;
front=rear;
}
else
{ temp->info=ele;
temp->next=rear;
rear=temp;
}
cout<<"\nThe element inserted is"<<temp->info;
}
void del()
{
node *temp;
temp=rear;
if(front==NULL&&rear==NULL)
{
cout<<"underflow";
getch();
}
else
{ if (temp->next==NULL)
{ cout<<"\ndeleted element is "<<temp->info;
delete temp;
rear=NULL;
front=NULL;
}
else
{ node *t;
while(temp->next!=NULL)
{
t=temp;
temp=temp->next;
}
cout<<"\nthe deleted element is "<<temp->info;
delete temp;
t->next=NULL;
}
}
}
void display()
{
node *temp;
temp=rear;
if(front==NULL&&rear==NULL)
cout<<"Queue is empty...";
cout<<"\nthe elements:";
while(temp!=NULL)
{
cout<<"\t"<<temp->info; temp=temp->next;
}
}
};
void main()
{ queue<int>q;
int ch;
int ele;
while(1)
{clrscr();
cout<<"\nselect any choice\n 1.insert\n2.delete\n3.display\n4.exit"; cin>>ch;
switch(ch)
{
case 1: cout<<"enter element to insert"; cin>>ele;
q.insert(ele);
break;
case 2: q.del();
getch();
break;
case 3: q.display();
getch();
break;
case 4: exit(0);
}
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
int ele;
template<class t>
class queue
{
struct node
{ t info;
struct node *next;
};
typedef struct node node;
node *front, *rear;
node *create(){return new node;}
public: queue() { front=NULL; rear=NULL; }
void insert (t ele)
{
node *temp=create();
if(rear==NULL&&front==NULL)
{ temp->info=ele;
temp->next=rear;
rear=temp;
front=rear;
}
else
{ temp->info=ele;
temp->next=rear;
rear=temp;
}
cout<<"\nThe element inserted is"<<temp->info;
}
void del()
{
node *temp;
temp=rear;
if(front==NULL&&rear==NULL)
{
cout<<"underflow";
getch();
}
else
{ if (temp->next==NULL)
{ cout<<"\ndeleted element is "<<temp->info;
delete temp;
rear=NULL;
front=NULL;
}
else
{ node *t;
while(temp->next!=NULL)
{
t=temp;
temp=temp->next;
}
cout<<"\nthe deleted element is "<<temp->info;
delete temp;
t->next=NULL;
}
}
}
void display()
{
node *temp;
temp=rear;
if(front==NULL&&rear==NULL)
cout<<"Queue is empty...";
cout<<"\nthe elements:";
while(temp!=NULL)
{
cout<<"\t"<<temp->info; temp=temp->next;
}
}
};
void main()
{ queue<int>q;
int ch;
int ele;
while(1)
{clrscr();
cout<<"\nselect any choice\n 1.insert\n2.delete\n3.display\n4.exit"; cin>>ch;
switch(ch)
{
case 1: cout<<"enter element to insert"; cin>>ele;
q.insert(ele);
break;
case 2: q.del();
getch();
break;
case 3: q.display();
getch();
break;
case 4: exit(0);
}
}
getch();
}
3.implementing stack using linked list
/*implementing stack using linked list*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
template<class t>
class stack
{
struct node1
{t sno;struct node1 *next;};
typedef node1 node;
node *top;
node *create(){ return new node;}
public:stack(){top=NULL;}
void push(t ele)
{
node *temp=create();
temp->sno=ele;
temp->next=top;
top=temp;
cout<<"the element inserted is:"<<temp->sno;}
void pop()
{
node *temp;
temp=top;
if(temp==NULL)
{
cout<<"\nunderflow";
getch();
}
else
{
top=top->next;
cout<<"\nthe deleted element is:"<<temp->sno;
delete temp ;
}
return;
}
void display()
{
node *temp;
temp=top;
if(top==0)
cout<<"\nqueue empty..";
else
{
cout<<"the elements:";
while(temp!=NULL)
{
cout<<"\n"<<endl<<temp->sno;
temp=temp->next;
}
}
}
};
void main()
{
stack<int> s;
int choice;
clrscr();
do
{
cout<<"\n 1.push\n2.pop\n3.display\n4.exit\n";
cout<<"Enter your coice:";
cin>>choice;
switch(choice)
{
case 1:int ele;
cout<<"enter element";
cin>>ele;
s.push(ele);
break;
case 2:s.pop();
break;
case 3:s.display();
break;
case 4:exit(0);
break;
}
getch();
}
while(1);
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
template<class t>
class stack
{
struct node1
{t sno;struct node1 *next;};
typedef node1 node;
node *top;
node *create(){ return new node;}
public:stack(){top=NULL;}
void push(t ele)
{
node *temp=create();
temp->sno=ele;
temp->next=top;
top=temp;
cout<<"the element inserted is:"<<temp->sno;}
void pop()
{
node *temp;
temp=top;
if(temp==NULL)
{
cout<<"\nunderflow";
getch();
}
else
{
top=top->next;
cout<<"\nthe deleted element is:"<<temp->sno;
delete temp ;
}
return;
}
void display()
{
node *temp;
temp=top;
if(top==0)
cout<<"\nqueue empty..";
else
{
cout<<"the elements:";
while(temp!=NULL)
{
cout<<"\n"<<endl<<temp->sno;
temp=temp->next;
}
}
}
};
void main()
{
stack<int> s;
int choice;
clrscr();
do
{
cout<<"\n 1.push\n2.pop\n3.display\n4.exit\n";
cout<<"Enter your coice:";
cin>>choice;
switch(choice)
{
case 1:int ele;
cout<<"enter element";
cin>>ele;
s.push(ele);
break;
case 2:s.pop();
break;
case 3:s.display();
break;
case 4:exit(0);
break;
}
getch();
}
while(1);
}
2.queue using array
//queue using array
#include<iostream.h>
#include<conio.h>
#include<process.h>
template <class t>
class queue
{
int f,r,temp;
int a[10];
public:
queue() {f=0;r=-1;}
void insert(t &ele)
{
cout<<"\ninsert an element";
cin>>ele;
r++;
a[r]=ele;
}
t deletion()
{
temp=a[f];
f++;
return(temp);
}
void display()
{
if(f>r)
{cout<<"queue is empty"; return; }
cout<<"the elements are";
for(int i=f;i<=r;i++)
cout<<a[i]<<"\n";
}
};
void main()
{
queue <int> q;
clrscr();
int choice; char n;
while(1)
{
cout<<"1.insert\n2.delete\n3.display\n4.exit\n";
cout<<"\nenter ur choice";
cin>>choice;
switch(choice)
{
case 1:q.insert(n);
break;
case 2:q.deletion();
break;
case 3:q.display();
break;
case 4:exit(1);
}
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
template <class t>
class queue
{
int f,r,temp;
int a[10];
public:
queue() {f=0;r=-1;}
void insert(t &ele)
{
cout<<"\ninsert an element";
cin>>ele;
r++;
a[r]=ele;
}
t deletion()
{
temp=a[f];
f++;
return(temp);
}
void display()
{
if(f>r)
{cout<<"queue is empty"; return; }
cout<<"the elements are";
for(int i=f;i<=r;i++)
cout<<a[i]<<"\n";
}
};
void main()
{
queue <int> q;
clrscr();
int choice; char n;
while(1)
{
cout<<"1.insert\n2.delete\n3.display\n4.exit\n";
cout<<"\nenter ur choice";
cin>>choice;
switch(choice)
{
case 1:q.insert(n);
break;
case 2:q.deletion();
break;
case 3:q.display();
break;
case 4:exit(1);
}
}
getch();
}
Here comes the first program /*stack adt using array*/
//implementing stack adt using array
#include<iostream.h>
#include<conio.h>
template <class t>
class stack{int top;t *a;
public: stack() {top=0;}
void push(t &el) { a[++top]=el; }
t pop()
{
if (top==0)
{cout<<"stack empty";}
return(a[top--]);
}
void display()
{
if (top==0)
{ cout<<"stack is empty"; return;
}
cout<<"the elements are \n";
for(int i=top;i>0;i--)
{cout<<a[i]<<" ";}
}
};
void main()
{
stack <int> st;
char cnt='y';
int choice, element;
clrscr();
do
{
cout<<"main menu\n"; cout<<"1.push\n2.pop\n3.display\nenter your choice\n";
cin>>choice;
switch(choice)
{
case 1:cout<<"enter new element\n";
cin>>element;
st.push(element);
break;
case 2:cout<<"deleted elements\n";
st.pop();
break;
case 3:cout<<"the elements of stack are\n";
st.display();
break;
}
cout<<" do u want to continue(y/n)";
cin>>cnt; getch();
}
while(cnt!='n');
getch();
}
/* note:this is more simpler than the previous one ...If any one found bugs or logical errors please comment......*/
Welcome
Hi, friends these are some of the programs in adsa I mainly concentrated to work for this internal that is why they are more simple and easy to understand and error free...... I assure that these programs are error free and perfectly executed...... So happy programing.....
your dinesh
your dinesh
Subscribe to:
Comments (Atom)