Procedure Language(PL)
**********************
mysql> delimiter //
mysql> CREATE PROCEDURE arul (OUT param1 INT)
-> BEGIN
-> select * from place_header;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> call arul(@a);
-> //
+----------+------------+----------------------+-------------+
| place_id | place_name | place_address | bus_station |
+----------+------------+----------------------+-------------+
| 1 | madras | 10, ptc road | parrys |
| 2 | madurai | 21,canalbank road | kknagar |
| 3 | trichy | 11, first cross road | bheltown |
| 4 | bangalore | 15, first main road | cubbon park |
| 5 | hyderabad | 115,lake view road | charminar |
| 6 | thanjavur | 12, temple road | railway jn |
+----------+------------+----------------------+-------------+
6 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Friday, October 29, 2010
Thursday, October 28, 2010
Queries to create tables & insert values into them
create table category_header(cat_code int,cat_desc varchar(20));
create table route_header(route_id int,route_no int,cat_code int,origin varchar(20),destination varchar(20),fare float,distance int,capacity int);
create table place_header(place_id int,place_name varchar(20),place_address varchar(20),bus_station varchar(20));
create table fleet_header(fleet_id int,day date,route_id int,cat_code int);
create table ticket_header(fleet_id int,ticket_no int,doi date,dot date,time_travel varchar(12),board_place varchar(20),origin varchar(20),destination varchar(20),adults int,children int,total_fare float,route_id int);
create table ticket_detail(ticket_no int,name varchar(20),sex char(1),age int,fare float);
insert into category_header values(01,'superdeluxe');
insert into category_header values(02,'deluxe');
insert into category_header values(03,'superfast');
insert into category_header values(04,'normal');
insert into route_header values(101,33,01,'Madurai','Madras',35,250,50);
insert into route_header values(102,25,02,'Trichy','Madurai',40,159,50);
insert into route_header values(103,15,03,'Thanjavur','Madurai',59,140,50);
insert into route_header values(104,36,04,'Madras','Bangalore',79,375,50);
insert into route_header values(105,40,01,'Bangalore','Madras',80,375,50);
insert into route_header values(106,38,02,'Madras','Madurai',39,250,50);
insert into route_header values(107,39,03,'Hydrabad','Madras',50,430,50);
insert into route_header values(108,41,04,'Madras','Cochin',47,576,50);
insert into place_header values(01,'Madras','10 PTC Road','Parrys');
insert into place_header values(02,'Madurai','21 Canalbank Road','KK nagar');
insert into place_header values(03,'Trichy','11 First Cross Road','Bheltown');
insert into place_header values(04,'Bangalore','15 First Main Road','Cubbonpark');
insert into place_header values(05,'Hydrabad','115 Lake view Road','Charminar');
insert into place_header values(06,'Thanjavur','12 Temple Road','Railway Jn');
insert into fleet_header values(01,'2010-08-04',101,01);
insert into fleet_header values(02,'2010-08-04',101,01);
insert into fleet_header values(03,'2010-08-04',101,01);
insert into fleet_header values(04,'2010-08-04',101,02);
insert into fleet_header values(05,'2010-08-04',101,03);
insert into fleet_header values(06,'2010-08-04',101,04);
insert into ticket_header values(01,01,'2010-08-04','2010-09-04','15:00:00','parrys','Madras','Madurai',1,1,60,101);
insert into ticket_header values(02,02,'2010-08-04','2010-09-04','09:00:00','KK Nagar','Madurai','Madras',2,1,60,102);
insert into ticket_header values(03,03,'2010-08-04','2010-09-04','21:00:00','Cubbonpark','Bangalore','Madras',4,2,400,101);
insert into ticket_header values(04,04,'2010-08-04','2010-09-04','10:00:00','Charminar','Hydrabad','Madras',10,0,500,103);
insert into ticket_header values(05,250,'2010-08-04','2010-09-04','15:00:00','parrys','Madras','Cochin',2,2,141,103);
insert into ticket_detail values(01,'Charu','f',24,'14.00');
insert into ticket_detail values(01,'Latha','f',10,'15.55');
insert into ticket_detail values(02,'anand','M',28,'17.80');
insert into ticket_detail values(02,'gautham','M',28,'16.00');
insert into ticket_detail values(02,'bala','M',09,'17.65');
insert into ticket_detail values(05,'sandeep','M',30,'18.00');
alter table route_header add comments long;
desc rout_header;
select distinct cat_code from route_header order by cat_code desc;
alter table route_header modify(distance int);
create table route_detail(route_id int,place_id int,nonstop char);
insert into route_detail values(105,01,'n');
insert into route_detail values(102,02,'s');
insert into route_detail values(106,01,'s');
insert into route_detail values(108,05,'n');
create table route_header(route_id int,route_no int,cat_code int,origin varchar(20),destination varchar(20),fare float,distance int,capacity int);
create table place_header(place_id int,place_name varchar(20),place_address varchar(20),bus_station varchar(20));
create table fleet_header(fleet_id int,day date,route_id int,cat_code int);
create table ticket_header(fleet_id int,ticket_no int,doi date,dot date,time_travel varchar(12),board_place varchar(20),origin varchar(20),destination varchar(20),adults int,children int,total_fare float,route_id int);
create table ticket_detail(ticket_no int,name varchar(20),sex char(1),age int,fare float);
insert into category_header values(01,'superdeluxe');
insert into category_header values(02,'deluxe');
insert into category_header values(03,'superfast');
insert into category_header values(04,'normal');
insert into route_header values(101,33,01,'Madurai','Madras',35,250,50);
insert into route_header values(102,25,02,'Trichy','Madurai',40,159,50);
insert into route_header values(103,15,03,'Thanjavur','Madurai',59,140,50);
insert into route_header values(104,36,04,'Madras','Bangalore',79,375,50);
insert into route_header values(105,40,01,'Bangalore','Madras',80,375,50);
insert into route_header values(106,38,02,'Madras','Madurai',39,250,50);
insert into route_header values(107,39,03,'Hydrabad','Madras',50,430,50);
insert into route_header values(108,41,04,'Madras','Cochin',47,576,50);
insert into place_header values(01,'Madras','10 PTC Road','Parrys');
insert into place_header values(02,'Madurai','21 Canalbank Road','KK nagar');
insert into place_header values(03,'Trichy','11 First Cross Road','Bheltown');
insert into place_header values(04,'Bangalore','15 First Main Road','Cubbonpark');
insert into place_header values(05,'Hydrabad','115 Lake view Road','Charminar');
insert into place_header values(06,'Thanjavur','12 Temple Road','Railway Jn');
insert into fleet_header values(01,'2010-08-04',101,01);
insert into fleet_header values(02,'2010-08-04',101,01);
insert into fleet_header values(03,'2010-08-04',101,01);
insert into fleet_header values(04,'2010-08-04',101,02);
insert into fleet_header values(05,'2010-08-04',101,03);
insert into fleet_header values(06,'2010-08-04',101,04);
insert into ticket_header values(01,01,'2010-08-04','2010-09-04','15:00:00','parrys','Madras','Madurai',1,1,60,101);
insert into ticket_header values(02,02,'2010-08-04','2010-09-04','09:00:00','KK Nagar','Madurai','Madras',2,1,60,102);
insert into ticket_header values(03,03,'2010-08-04','2010-09-04','21:00:00','Cubbonpark','Bangalore','Madras',4,2,400,101);
insert into ticket_header values(04,04,'2010-08-04','2010-09-04','10:00:00','Charminar','Hydrabad','Madras',10,0,500,103);
insert into ticket_header values(05,250,'2010-08-04','2010-09-04','15:00:00','parrys','Madras','Cochin',2,2,141,103);
insert into ticket_detail values(01,'Charu','f',24,'14.00');
insert into ticket_detail values(01,'Latha','f',10,'15.55');
insert into ticket_detail values(02,'anand','M',28,'17.80');
insert into ticket_detail values(02,'gautham','M',28,'16.00');
insert into ticket_detail values(02,'bala','M',09,'17.65');
insert into ticket_detail values(05,'sandeep','M',30,'18.00');
alter table route_header add comments long;
desc rout_header;
select distinct cat_code from route_header order by cat_code desc;
alter table route_header modify(distance int);
create table route_detail(route_id int,place_id int,nonstop char);
insert into route_detail values(105,01,'n');
insert into route_detail values(102,02,'s');
insert into route_detail values(106,01,'s');
insert into route_detail values(108,05,'n');
MYSQL Lab List Solutions
1. select * from route_header where origin='madras' and destination='cochin';
2. update route_header set distance=300 where origin in('madras','madurai') and destination in('madras','madurai');
3. delete from route_header where origin = 'madras' and destination = 'bangalore';
4. select * from route_header where origin like 'm%';
5. select * from route_header where fare between 30 and 50;
6. select fare,origin from route_header where route_no>15;
7. select * from place_header where place_name like 'm%';
8. select * from place_header where place_name like 'a%';
9. create table pseudo as (select * from category_header);
10. select add_months(doi,2) from ticket_header;
11. select last_day(dot) from ticket_header;
12. select months_between(dot,doi) from ticket_header;
13. select next_day(dot,'friday') from ticket_header;
14.
I. select round(to_date(day,'dd-mon-yy'),'year') from fleet_header;
II. select round(to_date(day,'dd-mon-yy'),'month') from fleet_header;
III. select round(to_date(day,'dd-mon-yy'),'day') from fleet_header;
IV. select round(to_date(day,'dd-mon-yy')) from fleet_header;
15.
I. select trunc(to_date(day,'dd-mon-yy'),'year') from fleet_header;
II. select trunc(to_date(day,'dd-mon-yy'),'month') from fleet_header;
III. select trunc(to_date(day,'dd-mon-yy'),'day') from fleet_header;
IV. select trunc(to_date(day,'dd-mon-yy')) from fleet_header;
16. select greatest(to_date(doi),to_date(dot)) from ticket_header;
17. select day+31 from fleet_header;
18. select day-60 from fleet_header;
19. select concat(upper(left(name,1)),substring(name,2)) from ticket_detail;
20. select upper(name) from ticket_detail;
21. select lower(place_name) from place_header;
22. select rtrim(place_name,'galore') from place_header where place_name='bangalore';
23. select ltrim(place_name,'hydera') from place_header where place_name='hyderabad';
24. select translate(place_name,'m','v') from place_header where place_name like 'm%';
25. select substr(name,3,3) from ticket_detail;
26. select replace(place_name,'a','e') from place_header;
27. select round(total_fare,2) from ticket_header;
28. select floor(total_fare) from ticket_header;
29. select to_char(sysdate,'ddth "of" fmmonth yyyy')) from dual;
30. select to_char(september 20 1996,'month-dd-yyyy')) from dual;
31. insert into category_header(cat_code) values(80);select nvl(cat_desc,0) from category_header;
32. select avg(total_fare) from ticket_header;
33. select name from ticket_detail where fare=(select max(fare) from ticket_detail);
34. select * from route_header where distance=(select min(distance) from route_header);
35. select sum(total_fare) from ticket_header;
36. select name,count(name) from ticket_detail group by name;
37. update (tablename) set day=’wednesday’ where day=’sunday’
Tuesday, October 26, 2010
Programs List
Single Linked List
Double Linked List
Circular Linked list
Insertion Sort
Merge Sort
Bubble Sort
Quick sort
Binary Tree Traversal
In-Order Traversal of Binary Tree
Pre-Order Traversal of Binary Tree
Post-Order Traversal of Binary Tree
Binary Search
Hashing
Binary Search
#include<iostream.h>
#include<conio.h>
class binary
{
public:
int a[50],n,x;
void getdata();
void getitem();
};
class search:public binary
{
public:
int high,j,low,temp;
search()
{
temp=0,low=0;
}
void findata();
void display();
};
void binary::getdata()
{
cout<<"\n Enter the total no of value:";
cin>>n;
cout<<"\n Enter "<<n<<" values one by one:";
for(int i=0;i<n;i++)
{
cout<<"\n Position "<<i<<":";
cin>>a[i];
}
}
void binary::getitem()
{
cout<<"\n Enter the value to be searched:";
cin>>x;
}
void search::findata()
{
high=n-1;
do
{
j=(low+high)/2;
if(a[j]==x)
temp=1;
else if(x<a[j])
high=j-1;
else
low=j+1;
}
while(low<=high&&temp==0);
}
void search::display()
{
if(temp==1)
cout<<"\n The value is located the position:"<<j;
else
cout<<"\n The value is not found";
}
void main()
{
clrscr();
search s;
s.getdata();
s.getitem();
s.findata();
s.display();
getch();
}
#include<conio.h>
class binary
{
public:
int a[50],n,x;
void getdata();
void getitem();
};
class search:public binary
{
public:
int high,j,low,temp;
search()
{
temp=0,low=0;
}
void findata();
void display();
};
void binary::getdata()
{
cout<<"\n Enter the total no of value:";
cin>>n;
cout<<"\n Enter "<<n<<" values one by one:";
for(int i=0;i<n;i++)
{
cout<<"\n Position "<<i<<":";
cin>>a[i];
}
}
void binary::getitem()
{
cout<<"\n Enter the value to be searched:";
cin>>x;
}
void search::findata()
{
high=n-1;
do
{
j=(low+high)/2;
if(a[j]==x)
temp=1;
else if(x<a[j])
high=j-1;
else
low=j+1;
}
while(low<=high&&temp==0);
}
void search::display()
{
if(temp==1)
cout<<"\n The value is located the position:"<<j;
else
cout<<"\n The value is not found";
}
void main()
{
clrscr();
search s;
s.getdata();
s.getitem();
s.findata();
s.display();
getch();
}
Merge Sort
#include <iostream.h>
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
void main()
{
int num,i;
cout<<"*************************"<<endl;
cout<<"MERGE SORT PROGRAM"<<endl;
cout<<"**************************"<<endl<<endl<<endl;
cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]:"<<endl;
cin>>num;
cout<<endl;
cout<<"Now, Please Enter the ( "<< num <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:"<<endl;
for(i=1;i<=num;i++)
{
cin>>a[i] ;
}
merge_sort(1,num);
cout<<endl;
cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
cout<<endl<<endl;
for(i=1;i<=num;i++)
cout<<a[i]<<" ";
cout<<endl<<endl<<endl<<endl;
}
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
void main()
{
int num,i;
cout<<"*************************"<<endl;
cout<<"MERGE SORT PROGRAM"<<endl;
cout<<"**************************"<<endl<<endl<<endl;
cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]:"<<endl;
cin>>num;
cout<<endl;
cout<<"Now, Please Enter the ( "<< num <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:"<<endl;
for(i=1;i<=num;i++)
{
cin>>a[i] ;
}
merge_sort(1,num);
cout<<endl;
cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
cout<<endl<<endl;
for(i=1;i<=num;i++)
cout<<a[i]<<" ";
cout<<endl<<endl<<endl<<endl;
}
Monday, October 25, 2010
Post-Order Traversal of Binary Tree
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void postorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1---INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2---POST-ORDER TRAVERSAL.";
cout<<"\n\t3---EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\n\tPOST-ORDER TRAVERSAL OF A TREE\n\t";
postorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data<<"\t";
}
}
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void postorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1---INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2---POST-ORDER TRAVERSAL.";
cout<<"\n\t3---EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\n\tPOST-ORDER TRAVERSAL OF A TREE\n\t";
postorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data<<"\t";
}
}
Pre-Order Traversal of Binary Tree
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void preorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n1---INSERT A NODE IN A BINARY TREE.";
cout<<"\n2---PRE-ORDER TRAVERSAL.";
cout<<"\n3---EXIT.";
cout<<"\nENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\nENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\nPRE-ORDER TRAVERSAL OF A TREE\n\t";
preorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void preorder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data<<"\t";
preorder(tree->left);
preorder(tree->right);
}
}
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void preorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n1---INSERT A NODE IN A BINARY TREE.";
cout<<"\n2---PRE-ORDER TRAVERSAL.";
cout<<"\n3---EXIT.";
cout<<"\nENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\nENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\nPRE-ORDER TRAVERSAL OF A TREE\n\t";
preorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void preorder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data<<"\t";
preorder(tree->left);
preorder(tree->right);
}
}
In-Order Traversal of Binary Tree
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void inorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2----IN-ORDER TRAVERSAL.";
cout<<"\n\t3----EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\n\tIN-ORDER TRAVERSAL OF A TREE\n\t";
inorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void inorder(node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data<<"\t";
inorder(tree->right);
}
}
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void inorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2----IN-ORDER TRAVERSAL.";
cout<<"\n\t3----EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\n\tIN-ORDER TRAVERSAL OF A TREE\n\t";
inorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void inorder(node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data<<"\t";
inorder(tree->right);
}
}
Binary tree Traversal
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void preorder(node *tree);
void inorder(node *tree);
void postorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2----PRE-ORDER TRAVERSAL.";
cout<<"\n\t3----IN-ORDER TRAVERSAL.";
cout<<"\n\t4----POST-ORDER TRAVERSAL.";
cout<<"\n\t5----EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\n\t****PRE-ORDER TRAVERSAL OF A TREE****";
preorder(tree);
getch();
break;
case '3':
cout<<"\n\t****IN-ORDER TRAVERSAL OF A TREE****";
inorder(tree);
getch();
break;
case '4':
cout<<"\n\t****POST-ORDER TRAVERSAL OF A TREE****";
postorder(tree);
getch();
break;
case '5':
exit(0);
}
}while(ch!='5');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else
if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void
preorder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data<<"\t";
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data<<"\t";
inorder(tree->right);
}
}
void postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data<<"\t";
}
}
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void preorder(node *tree);
void inorder(node *tree);
void postorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2----PRE-ORDER TRAVERSAL.";
cout<<"\n\t3----IN-ORDER TRAVERSAL.";
cout<<"\n\t4----POST-ORDER TRAVERSAL.";
cout<<"\n\t5----EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\n\t****PRE-ORDER TRAVERSAL OF A TREE****";
preorder(tree);
getch();
break;
case '3':
cout<<"\n\t****IN-ORDER TRAVERSAL OF A TREE****";
inorder(tree);
getch();
break;
case '4':
cout<<"\n\t****POST-ORDER TRAVERSAL OF A TREE****";
postorder(tree);
getch();
break;
case '5':
exit(0);
}
}while(ch!='5');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else
if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void
preorder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data<<"\t";
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data<<"\t";
inorder(tree->right);
}
}
void postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data<<"\t";
}
}
Hashing Using C++
#include<iostream.h>
#include<conio.h>
#include<string.h>
class hash
{
struct node
{
char key[50];
long add;
node *next;
}*head;
public:
hash()
{
head=NULL;
}
void folding(char a[])
{
long i=0,j=0,k=0,l=0,s=0;
i=strlen(a);
j=i/2;
while(k<j)
{
l=l*10+a[k];
k++;
}
while(j<i)
{
s=s*10+a[j];
j++;
}
l=s+l;
chain(a,l);
}
void chain(char a[],long l)
{
node *temp,*r;
temp=head;
if(temp==NULL)
{
temp=new node;
strcpy(temp->key,a);
temp->add=l;
temp->next=NULL;
head=temp;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
r=new node;
strcpy( r->key,a);
r->add=l;
r->next=NULL;
temp->next=r;
}
}
void show()
{
node *temp;
temp=head;
if(head==NULL)
cout<<"\n Table is empty";
else
{
cout<<" key " <<" address ";
cout<<"\n ----........--------";
while(temp!=NULL)
{
cout<<"\n";
cout<<temp->key<<" "<<temp->add;
temp=temp->next;
}
}
}
void search(char a[])
{
long i=0,j=0,k=0,l=0,s=0;
node *temp;
temp=head;
i=strlen(a);
j=i/2;
while(k<j)
{
l=l*10+a[k];
k++;
}
while(j<i)
{
s=s*10+a[j];
j++;
}
l=s+l;
while(temp!=NULL)
{
if(temp->add==l)
{
cout<<"\n"<<a<<" is found";
s=-10;
break;
}
else
{
temp=temp->next;
}
}
if(s!=-10)
{
cout<<"\n"<<a<<" string not found ";
}
}
};
void main()
{
hash k;
clrscr();
char c='y',ch[50];
while(1)
{
cout<<"\nEnter string to save in hash table ";
cin>>ch;
k.folding(ch);
cout<<"\nFor again enter y for exit press any ";
cin>>c;
if(c!='y')
break;
}
k.show();
cout<<"\n\nFor searching Enter string ";
cin>>ch;
k.search(ch);
getch();
}
#include<conio.h>
#include<string.h>
class hash
{
struct node
{
char key[50];
long add;
node *next;
}*head;
public:
hash()
{
head=NULL;
}
void folding(char a[])
{
long i=0,j=0,k=0,l=0,s=0;
i=strlen(a);
j=i/2;
while(k<j)
{
l=l*10+a[k];
k++;
}
while(j<i)
{
s=s*10+a[j];
j++;
}
l=s+l;
chain(a,l);
}
void chain(char a[],long l)
{
node *temp,*r;
temp=head;
if(temp==NULL)
{
temp=new node;
strcpy(temp->key,a);
temp->add=l;
temp->next=NULL;
head=temp;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
r=new node;
strcpy( r->key,a);
r->add=l;
r->next=NULL;
temp->next=r;
}
}
void show()
{
node *temp;
temp=head;
if(head==NULL)
cout<<"\n Table is empty";
else
{
cout<<" key " <<" address ";
cout<<"\n ----........--------";
while(temp!=NULL)
{
cout<<"\n";
cout<<temp->key<<" "<<temp->add;
temp=temp->next;
}
}
}
void search(char a[])
{
long i=0,j=0,k=0,l=0,s=0;
node *temp;
temp=head;
i=strlen(a);
j=i/2;
while(k<j)
{
l=l*10+a[k];
k++;
}
while(j<i)
{
s=s*10+a[j];
j++;
}
l=s+l;
while(temp!=NULL)
{
if(temp->add==l)
{
cout<<"\n"<<a<<" is found";
s=-10;
break;
}
else
{
temp=temp->next;
}
}
if(s!=-10)
{
cout<<"\n"<<a<<" string not found ";
}
}
};
void main()
{
hash k;
clrscr();
char c='y',ch[50];
while(1)
{
cout<<"\nEnter string to save in hash table ";
cin>>ch;
k.folding(ch);
cout<<"\nFor again enter y for exit press any ";
cin>>c;
if(c!='y')
break;
}
k.show();
cout<<"\n\nFor searching Enter string ";
cin>>ch;
k.search(ch);
getch();
}
Circular Linked List in C++
#include<iostream.h>
#include<conio.h>
class list
{
struct node
{
int info;
node *next;
}*head;
public:
list()
{
head=NULL;
}
void append(int data)
{
node *temp,*temp1;
if(head==NULL)
{
temp=new node;
temp->info=data;
head=temp;
temp->next=head;
}
else
{
temp=head;
while(temp->next!=head)
temp=temp->next;
temp1=new node;
temp1->info=data;
temp1->next=head;
temp->next=temp1;
}
}
void beg_insert(int data)
{
node *temp,*temp1;
temp=new node;
temp1=head;
if(temp1==NULL)
cout<<"\n Firstely create using 1 , list not exist";
else
{
while(temp1->next!=head)
temp1=temp1->next;
temp->info=data;
temp->next=head;
head=temp;
temp1->next=head;
}
}
void in_insert(int pos,int data)
{
node *temp,*t;
int a;
a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(temp==NULL)
cout<<"\n\t firstely create using 1 , List not exist ";
else if(a==0)
beg_insert(data);
else
{
t=new node;
t->info=data;
t->next=temp->next;
temp->next=t;
}
}
void del( )
{
node *temp,*t;
temp=head;
if(temp==NULL)
cout<<"\n \tList not Exists ";
else if(temp->next==head)
{
delete temp;
head=NULL;
}
else
{
t=head->next;
while(t->next!=head)
{
t=t->next;
temp=temp->next;
}
// t=temp->next->next;
temp->next=head;
delete t;
}
}
void beg_del()
{
node *temp,*temp1;
temp=head;
if(head==NULL)
cout<<"\n\t Link list has no nodes";
else
{
temp1=head;
while(temp1->next!=head)
temp1=temp1->next;
head=temp->next;
temp1->next=head;
delete temp;
}
}
void mid_del(int pos)
{
node *temp,*t;
int a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 )
beg_del();
else if(temp==NULL)
cout<<"\n\t List not Exist ..";
else
{
t=temp->next;
temp->next=temp->next->next;
delete t;
}
}
void show()
{
node *temp;
temp=head;
cout<<"\n\t";
if(temp==NULL)
cout<<"\n \tList not exist";
cout<<temp->info;
temp=temp->next;
while(temp!=head)
{
cout<<" - "<<temp->info;
temp=temp->next;
}
}
};
void main()
{
list l;
clrscr();
int ch,pos;
while(1)
{
cout<<"\n\t\tENter your Choice : ";
cout<<"\n 1: To append\n 2: To insert at Begining\n 3: To insert at any position";
cout<<"\n 4: To delete\n 5: To del at begining\n 6: To Delete at any position ";
cout<<"\n 7: To show\n 8: To exit ";
cin>>ch;
if(ch==1)
{
cout<<"\n\t Enter data to append : ";
cin>>ch;
l.append(ch);
}
else if(ch==2)
{
cout<<"\n\t Enter element to insert at begining : " ;
cin>>ch;
l.beg_insert(ch);
}
else if(ch==3)
{
cout<<"\nEnter element to insert at specific position : ";
cin>>ch;
cout<<"\n Enter position : ";
cin>>pos;
l.in_insert(pos,ch);
}
else if(ch==4)
l.del();
else if (ch==5)
l.beg_del();
else if(ch==6)
{
cout<<"\n Enter position where u have to delete";
cin>>pos;
l.mid_del(pos);
}
else if(ch==7)
l.show();
else if(ch==8)
{
cout<<"\n Press Enter to Exit";
break;
}
else
cout<<"\n\t Wrong Choice ";
}
getch();
}
#include<conio.h>
class list
{
struct node
{
int info;
node *next;
}*head;
public:
list()
{
head=NULL;
}
void append(int data)
{
node *temp,*temp1;
if(head==NULL)
{
temp=new node;
temp->info=data;
head=temp;
temp->next=head;
}
else
{
temp=head;
while(temp->next!=head)
temp=temp->next;
temp1=new node;
temp1->info=data;
temp1->next=head;
temp->next=temp1;
}
}
void beg_insert(int data)
{
node *temp,*temp1;
temp=new node;
temp1=head;
if(temp1==NULL)
cout<<"\n Firstely create using 1 , list not exist";
else
{
while(temp1->next!=head)
temp1=temp1->next;
temp->info=data;
temp->next=head;
head=temp;
temp1->next=head;
}
}
void in_insert(int pos,int data)
{
node *temp,*t;
int a;
a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(temp==NULL)
cout<<"\n\t firstely create using 1 , List not exist ";
else if(a==0)
beg_insert(data);
else
{
t=new node;
t->info=data;
t->next=temp->next;
temp->next=t;
}
}
void del( )
{
node *temp,*t;
temp=head;
if(temp==NULL)
cout<<"\n \tList not Exists ";
else if(temp->next==head)
{
delete temp;
head=NULL;
}
else
{
t=head->next;
while(t->next!=head)
{
t=t->next;
temp=temp->next;
}
// t=temp->next->next;
temp->next=head;
delete t;
}
}
void beg_del()
{
node *temp,*temp1;
temp=head;
if(head==NULL)
cout<<"\n\t Link list has no nodes";
else
{
temp1=head;
while(temp1->next!=head)
temp1=temp1->next;
head=temp->next;
temp1->next=head;
delete temp;
}
}
void mid_del(int pos)
{
node *temp,*t;
int a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 )
beg_del();
else if(temp==NULL)
cout<<"\n\t List not Exist ..";
else
{
t=temp->next;
temp->next=temp->next->next;
delete t;
}
}
void show()
{
node *temp;
temp=head;
cout<<"\n\t";
if(temp==NULL)
cout<<"\n \tList not exist";
cout<<temp->info;
temp=temp->next;
while(temp!=head)
{
cout<<" - "<<temp->info;
temp=temp->next;
}
}
};
void main()
{
list l;
clrscr();
int ch,pos;
while(1)
{
cout<<"\n\t\tENter your Choice : ";
cout<<"\n 1: To append\n 2: To insert at Begining\n 3: To insert at any position";
cout<<"\n 4: To delete\n 5: To del at begining\n 6: To Delete at any position ";
cout<<"\n 7: To show\n 8: To exit ";
cin>>ch;
if(ch==1)
{
cout<<"\n\t Enter data to append : ";
cin>>ch;
l.append(ch);
}
else if(ch==2)
{
cout<<"\n\t Enter element to insert at begining : " ;
cin>>ch;
l.beg_insert(ch);
}
else if(ch==3)
{
cout<<"\nEnter element to insert at specific position : ";
cin>>ch;
cout<<"\n Enter position : ";
cin>>pos;
l.in_insert(pos,ch);
}
else if(ch==4)
l.del();
else if (ch==5)
l.beg_del();
else if(ch==6)
{
cout<<"\n Enter position where u have to delete";
cin>>pos;
l.mid_del(pos);
}
else if(ch==7)
l.show();
else if(ch==8)
{
cout<<"\n Press Enter to Exit";
break;
}
else
cout<<"\n\t Wrong Choice ";
}
getch();
}
Double Linked List in C++
#include<iostream.h>
#include<conio.h>
class list
{
struct node
{
int info;
node *next;
node *pre;
}*head;
public:
list()
{
head=NULL;
}
void append(int data)
{
node *temp,*temp1;
if(head==NULL)
{
temp=new node;
temp->info=data;
temp->next=NULL;
temp->pre=NULL;
head=temp;
}
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp1=new node;
temp1->info=data;
temp1->next=NULL;
temp1->pre=temp;
temp->next=temp1;
}
}
void beg_insert(int data)
{
node *temp;
temp=new node;
temp->info=data;
temp->next=head;
temp->pre=NULL;
temp->next->pre=temp;
head=temp;
}
void in_insert(int pos,int data)
{
node *temp,*t;
int a;
a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && temp==NULL)
append(data);
else if(temp==NULL)
cout<<"\n\tWrong position u given";
else
{
t=new node;
t->info=data;
t->next=temp->next;
t->pre=temp;
temp->next->pre=t;
temp->next=t;
}
}
void del( )
{
node *temp,*t;
temp=head;
if(temp==NULL)
cout<<"\n \tList not Exists ";
else if(temp->next==NULL)
{
delete temp;
head=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
t=temp;
temp->pre->next=NULL;
delete t;
}
}
void beg_del()
{
node *temp;
temp=head;
if(head==NULL)
cout<<"\n\t Link list has np nodes";
else
{
head=temp->next;
temp->next->pre=NULL;
delete temp;
}
}
void mid_del(int pos)
{
node *temp,*t;
int a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && head->next==NULL)
del();
else if(a==1 && head->pre==NULL)
beg_del();
else if(temp==NULL)
cout<<"\n\t List not Exist ..";
else
{
t=temp;
temp->pre->next=t->next;
temp->next->pre=t->pre;
delete t;
}
}
void show()
{
node *temp;
temp=head;
cout<<"\n\t";
if(temp==NULL)
cout<<"\n \tList not exist\n";
while(temp!=NULL)
{
cout<<" - "<<temp->info;
temp=temp->next;
}
temp=head;
while(temp->next!=NULL)
temp=temp->next;
cout<<"\nRevesrse ";
while(temp!=NULL)
{
cout<<" - "<<temp->info;
temp=temp->pre;
}
}
};
void main()
{
list l;
clrscr();
int ch,pos;
while(1)
{
cout<<"\n\t\tENter your Choice : ";
cout<<"\n 1: To append\n 2: To insert at Begining\n 3: To insert at any position";
cout<<"\n 4: To delete\n 5: To del at begining\n 6: To Delete at any position ";
cout<<"\n 7: To show\n 8: To exit ";
cin>>ch;
if(ch==1)
{
cout<<"\n\t Enter data to append : ";
cin>>ch;
l.append(ch);
}
else if(ch==2)
{
cout<<"\n\t Enter element to insert at begining : " ;
cin>>ch;
l.beg_insert(ch);
}
else if(ch==3)
{
cout<<"\nEnter element to insert at specific position : ";
cin>>ch;
cout<<"\n Enter position : ";
cin>>pos;
l.in_insert(pos,ch);
}
else if(ch==4)
l.del();
else if (ch==5)
l.beg_del();
else if(ch==6)
{
cout<<"\n Enter position where u have to delete ";
cin>>pos;
l.mid_del(pos);
}
else if(ch==7)
l.show();
else if(ch==8)
{
cout<<"\n Press Enter to Exit";
break;
}
else
cout<<"\n\t Wrong Choice ";
}
getch();
}
#include<conio.h>
class list
{
struct node
{
int info;
node *next;
node *pre;
}*head;
public:
list()
{
head=NULL;
}
void append(int data)
{
node *temp,*temp1;
if(head==NULL)
{
temp=new node;
temp->info=data;
temp->next=NULL;
temp->pre=NULL;
head=temp;
}
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp1=new node;
temp1->info=data;
temp1->next=NULL;
temp1->pre=temp;
temp->next=temp1;
}
}
void beg_insert(int data)
{
node *temp;
temp=new node;
temp->info=data;
temp->next=head;
temp->pre=NULL;
temp->next->pre=temp;
head=temp;
}
void in_insert(int pos,int data)
{
node *temp,*t;
int a;
a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && temp==NULL)
append(data);
else if(temp==NULL)
cout<<"\n\tWrong position u given";
else
{
t=new node;
t->info=data;
t->next=temp->next;
t->pre=temp;
temp->next->pre=t;
temp->next=t;
}
}
void del( )
{
node *temp,*t;
temp=head;
if(temp==NULL)
cout<<"\n \tList not Exists ";
else if(temp->next==NULL)
{
delete temp;
head=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
t=temp;
temp->pre->next=NULL;
delete t;
}
}
void beg_del()
{
node *temp;
temp=head;
if(head==NULL)
cout<<"\n\t Link list has np nodes";
else
{
head=temp->next;
temp->next->pre=NULL;
delete temp;
}
}
void mid_del(int pos)
{
node *temp,*t;
int a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && head->next==NULL)
del();
else if(a==1 && head->pre==NULL)
beg_del();
else if(temp==NULL)
cout<<"\n\t List not Exist ..";
else
{
t=temp;
temp->pre->next=t->next;
temp->next->pre=t->pre;
delete t;
}
}
void show()
{
node *temp;
temp=head;
cout<<"\n\t";
if(temp==NULL)
cout<<"\n \tList not exist\n";
while(temp!=NULL)
{
cout<<" - "<<temp->info;
temp=temp->next;
}
temp=head;
while(temp->next!=NULL)
temp=temp->next;
cout<<"\nRevesrse ";
while(temp!=NULL)
{
cout<<" - "<<temp->info;
temp=temp->pre;
}
}
};
void main()
{
list l;
clrscr();
int ch,pos;
while(1)
{
cout<<"\n\t\tENter your Choice : ";
cout<<"\n 1: To append\n 2: To insert at Begining\n 3: To insert at any position";
cout<<"\n 4: To delete\n 5: To del at begining\n 6: To Delete at any position ";
cout<<"\n 7: To show\n 8: To exit ";
cin>>ch;
if(ch==1)
{
cout<<"\n\t Enter data to append : ";
cin>>ch;
l.append(ch);
}
else if(ch==2)
{
cout<<"\n\t Enter element to insert at begining : " ;
cin>>ch;
l.beg_insert(ch);
}
else if(ch==3)
{
cout<<"\nEnter element to insert at specific position : ";
cin>>ch;
cout<<"\n Enter position : ";
cin>>pos;
l.in_insert(pos,ch);
}
else if(ch==4)
l.del();
else if (ch==5)
l.beg_del();
else if(ch==6)
{
cout<<"\n Enter position where u have to delete ";
cin>>pos;
l.mid_del(pos);
}
else if(ch==7)
l.show();
else if(ch==8)
{
cout<<"\n Press Enter to Exit";
break;
}
else
cout<<"\n\t Wrong Choice ";
}
getch();
}
Single Linked List in C++
#include<iostream.h>
#include<conio.h>
class list
{
struct node
{
int info;
node *next;
}*head;
public:
list()
{
head=NULL;
}
void append(int data)
{
node *temp,*temp1;
if(head==NULL)
{
temp=new node;
temp->info=data;
temp->next=NULL;
head=temp;
}
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp1=new node;
temp1->info=data;
temp1->next=NULL;
temp->next=temp1;
}
}
void beg_insert(int data)
{
node *temp;
temp=new node;
temp->info=data;
temp->next=head;
head=temp;
}
void in_insert(int pos,int data)
{
node *temp,*t;
int a;
a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && temp==NULL)
append(data);
else if(temp==NULL)
cout<<"\n\tWrong position u given";
else
{
t=new node;
t->info=data;
t->next=temp->next;
temp->next=t;
}
}
void del( )
{
node *temp,*t;
temp=head;
if(temp==NULL)
cout<<"\n \tList not Exists ";
else if(temp->next==NULL)
{
delete temp;
head=NULL;
}
else
{
t=head->next;
while(t->next!=NULL)
{
t=t->next;
temp=temp->next;
}
// t=temp->next->next;
temp->next=NULL;
delete t;
}
}
void beg_del()
{
node *temp;
temp=head;
if(head==NULL)
cout<<"\n\t Link list has np nodes";
else
{
head=temp->next;
delete temp;
}
}
void mid_del(int pos)
{
node *temp,*t;
int a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && head->next==NULL)
del();
else if(temp==NULL)
cout<<"\n\t List not Exist ..";
else
{
t=temp->next;
temp->next=temp->next->next;
delete t;
}
}
void show()
{
node *temp;
temp=head;
cout<<"\n\t";
if(temp==NULL)
cout<<"\n \tList not exist";
while(temp!=NULL)
{
cout<<" - "<<temp->info;
temp=temp->next;
}
}
void search(int data)
{
node *temp;
int a=1;
temp=head;
if(temp==NULL)
cout<<"\n\t List not exist ";
else
{
while(temp!=NULL)
{
if(temp->info==data)
{
cout<<"\n\t Element fount at : "<<a<<" Position ";
break;
}
a++;
temp=temp->next;
}
if(temp==NULL)
cout<<"\n\t Element is not in list ";
}
}
};
void main()
{
list l;
clrscr();
int ch,pos;
while(1)
{
cout<<"\n\t\tENter your Choice : ";
cout<<"\n 1: To append\n 2: To insert at Begining\n 3: To insert at any position";
cout<<"\n 4: To delete\n 5: To del at begining\n 6: To Delete at any position ";
cout<<"\n 7: To show\n 8: To exit\n 9: To Search ";
cin>>ch;
if(ch==1)
{
cout<<"\n\t Enter data to append : ";
cin>>ch;
l.append(ch);
}
else if(ch==2)
{
cout<<"\n\t Enter element to insert at begining : " ;
cin>>ch;
l.beg_insert(ch);
}
else if(ch==3)
{
cout<<"\nEnter element to insert at specific position : ";
cin>>ch;
cout<<"\n Enter position : ";
cin>>pos;
l.in_insert(pos,ch);
}
else if(ch==4)
l.del();
else if (ch==5)
l.beg_del();
else if(ch==6)
{
cout<<"\n Enter position where u have to delete";
cin>>pos;
l.mid_del(pos);
}
else if(ch==7)
l.show();
else if(ch==8)
{
cout<<"\n Press Enter to Exit";
break;
}
else if(ch==9)
{
cout<<"\n Enter your number for searching in list : ";
cin>>ch;
l.search(ch);
}
else
cout<<"\n\t Wrong Choice ";
}
getch();
}
#include<conio.h>
class list
{
struct node
{
int info;
node *next;
}*head;
public:
list()
{
head=NULL;
}
void append(int data)
{
node *temp,*temp1;
if(head==NULL)
{
temp=new node;
temp->info=data;
temp->next=NULL;
head=temp;
}
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp1=new node;
temp1->info=data;
temp1->next=NULL;
temp->next=temp1;
}
}
void beg_insert(int data)
{
node *temp;
temp=new node;
temp->info=data;
temp->next=head;
head=temp;
}
void in_insert(int pos,int data)
{
node *temp,*t;
int a;
a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && temp==NULL)
append(data);
else if(temp==NULL)
cout<<"\n\tWrong position u given";
else
{
t=new node;
t->info=data;
t->next=temp->next;
temp->next=t;
}
}
void del( )
{
node *temp,*t;
temp=head;
if(temp==NULL)
cout<<"\n \tList not Exists ";
else if(temp->next==NULL)
{
delete temp;
head=NULL;
}
else
{
t=head->next;
while(t->next!=NULL)
{
t=t->next;
temp=temp->next;
}
// t=temp->next->next;
temp->next=NULL;
delete t;
}
}
void beg_del()
{
node *temp;
temp=head;
if(head==NULL)
cout<<"\n\t Link list has np nodes";
else
{
head=temp->next;
delete temp;
}
}
void mid_del(int pos)
{
node *temp,*t;
int a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && head->next==NULL)
del();
else if(temp==NULL)
cout<<"\n\t List not Exist ..";
else
{
t=temp->next;
temp->next=temp->next->next;
delete t;
}
}
void show()
{
node *temp;
temp=head;
cout<<"\n\t";
if(temp==NULL)
cout<<"\n \tList not exist";
while(temp!=NULL)
{
cout<<" - "<<temp->info;
temp=temp->next;
}
}
void search(int data)
{
node *temp;
int a=1;
temp=head;
if(temp==NULL)
cout<<"\n\t List not exist ";
else
{
while(temp!=NULL)
{
if(temp->info==data)
{
cout<<"\n\t Element fount at : "<<a<<" Position ";
break;
}
a++;
temp=temp->next;
}
if(temp==NULL)
cout<<"\n\t Element is not in list ";
}
}
};
void main()
{
list l;
clrscr();
int ch,pos;
while(1)
{
cout<<"\n\t\tENter your Choice : ";
cout<<"\n 1: To append\n 2: To insert at Begining\n 3: To insert at any position";
cout<<"\n 4: To delete\n 5: To del at begining\n 6: To Delete at any position ";
cout<<"\n 7: To show\n 8: To exit\n 9: To Search ";
cin>>ch;
if(ch==1)
{
cout<<"\n\t Enter data to append : ";
cin>>ch;
l.append(ch);
}
else if(ch==2)
{
cout<<"\n\t Enter element to insert at begining : " ;
cin>>ch;
l.beg_insert(ch);
}
else if(ch==3)
{
cout<<"\nEnter element to insert at specific position : ";
cin>>ch;
cout<<"\n Enter position : ";
cin>>pos;
l.in_insert(pos,ch);
}
else if(ch==4)
l.del();
else if (ch==5)
l.beg_del();
else if(ch==6)
{
cout<<"\n Enter position where u have to delete";
cin>>pos;
l.mid_del(pos);
}
else if(ch==7)
l.show();
else if(ch==8)
{
cout<<"\n Press Enter to Exit";
break;
}
else if(ch==9)
{
cout<<"\n Enter your number for searching in list : ";
cin>>ch;
l.search(ch);
}
else
cout<<"\n\t Wrong Choice ";
}
getch();
}
Subscribe to:
Posts (Atom)