Add Time

This is a program to two time entered in format hh:mm:ss..

#include<iostream.h>
#include<conio.h>
class time
{
int h,s,m;
public : void print();
friend time calc(time,time);
time()
{
h=0;
m=0;
s=0;
}
time(int hr,int mn,int sec)
{
h=hr;
m=mn;
s=sec;
}
};
time calc(time p,time q)
{
time t;
t.s=p.s+q.s;
t.m=t.s/60;
t.s=t.s%60;
t.m=p.m+q.m+t.m;
t.h=t.m/60;
t.m=t.m%60;
t.h=p.h+q.h+t.h;
return t;
}
void time::print()
{
cout<<h<<":"<<m<<":"<<s<<"\n";
}
main()
{
time t3;
time t1(2,56,34);
time t2(3,4,56);
clrscr();
t3=calc(t1,t2);
cout<<"Time 1 = ";t1.print();
cout<<"Time 2 = ";t2.print();
cout<<"Final time = ";
t3.print();
getch();
return 0;
}

DFS

Program to achieve DFS operation...

#include<stdio.h>
#include<conio.h>
int adj[10][10],visit[10]={0},n=0,count=0;
main()
{
void dfs(int node);
int s,i,j;
clrscr();
printf("\nEnter the no of nodes : ");scanf("%d",&n);
printf("\nEnter the adjacency matrix \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
printf("\nEnter the starting node : "); scanf("%d",&s);
printf("\n\n");
printf(" ~~ TREE ~~ \n\n");
dfs(s);
if(count==n)
printf("\n\n\nResult : Connected graph..!!");
else
printf("\n\n\nResult : Not connected..!");
getch();
return 0;
}
void dfs(int node)
{
int i;
count++;
visit[node]=1;
printf("%d\t",node);
for(i=1;i<=n;i++)
if(adj[node][i]==1 && visit[i]==0)
dfs(i);
}

BFS

Program to achieve BFS operation...


/* Program to achieve BFS opration*/
#include<stdio.h>
#include<conio.h>
int adj[10][10],visit[10]={0},count=0,n=0;
void bfs(int s)
{
int que[10],i,f,front=-1,rear=-1;
count++;
visit[s]=1;
que[++rear]=s;
while(front<=rear)
{
f=front+1;
for(i=1;i<=n;i++)
{
if(adj[que[f]][i]==1 && visit[i]==0)
{
count++;
printf("%d\t",i);
visit[i]=1;
que[++rear]=i;
}
}
front++;
}
}

main()
{
int s,i,j;
clrscr();
printf("\nEnter the no of nodes : ");scanf("%d",&n);
printf("\nEnter the adjacency matrix \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
printf("\nEnter the source node : ");scanf("%d",&s);
printf("\nNodes reachable from %d are \n",s);
bfs(s);
if(count==1)
printf("\nNo nodes..!!");
getch();
return 0;
}

Double linked list c++

Program to implement double linked list in C++ with the member functions
1 : To insert a node at a specified position
2 : Delete a node at specified position
3 : Display the list after every peration
Note that the first two inputs to the list must be done to position 1..


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class dll
{
struct node
{
int info;
struct node *left,*right;
};
typedef struct node *nodeptr;
nodeptr list;
public: dll()
{
list=NULL;
}
void ins_pos(int,int);
void del_pos(int);
void display();
};
void dll::ins_pos(int x,int pos)
{
nodeptr p,q;
int count=0,i;
p=new node;
p->left=NULL;
p->right=NULL;
p->info=x;
if(list==NULL)
{
list=p;
}
else
if(pos==1)
{
p->right=list;
list=p;
}
else
{
for(q=list;q->right!=NULL;q=q->right)
count++;
if(pos<=count+1)
{
q=list;
for(i=1;i<pos-1;i++)
q=q->right;
(q->right)->left=p;
p->right=q->right;
q->right=p;
p->left=q;
}
}
cout<<"\n"<<x<<" inserted @ "<<pos<<" position..!";
display();
}
void dll::display()
{
nodeptr p;
if(list==NULL)
cout<<"\nInvalid operation..!";
else
{
cout<<"\n\nDLL elements\n--------------";
for(p=list;p!=NULL;p=p->right)
cout<<"\n"<<p->info;
cout<<"\n\n";
}
}
void dll::del_pos(int pos)
{
int count=0,x,i;
nodeptr p,q;
if(list==NULL)
cout<<"\nInvalid operation..!";
else
if(pos==1)
{
p=list;
list=list->right;
x=p->info;
delete p;
cout<<"\n"<<x<<" deleted form position "<<pos;
display();
}
else
{
for(p=list;p!=NULL;p=p->right)
count++;
if(pos<=count)
{
p=list;
for(i=1;i<pos-1;i++)
p=p->right;
q=p->right;
p->right=q->right;
(q->right)->left=p;
x=q->info;
delete(q);
cout<<"\n"<<x<<" deleted form position "<<pos;
display();
}
}
}

main()
{
int ele,pos,c;
dll l;
clrscr();
while(1)
{
cout<<"\n1 : Insert element @ a position??\n2 : Delete element from a position??\n3 : Exit??\n";
cout<<"Reply pls.. ";cin>>c;
switch(c)
{
case 1:cout<<"Enter the value and position : ";
cin>>ele>>pos;
l.ins_pos(ele,pos);
break;
case 2:cout<<"\nEnter the position : "; cin>>pos;
l.del_pos(pos);
break;
case 3:exit(0);
break;
}
}
getch();
return 0;
}

File program

Program to input student details and store it in a file..
The program performs following operations.
1 : Print the total no of student details entered into the file
2 : Print the student details entered.
3 : Update a particular student detail.


#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
char name[10];
int usn,sem,total;
public:void read();
void disp();
};
void student::read()
{
cout<<"Enter name,usn,sem,total : ";
cin>>name>>usn>>sem>>total;
}
void student::disp()
{
cout<<"Name \t"<<name<<"\n"<<"Usn\t"<<usn<<"\n"<<"Sem\t "<<sem<<"\n"<<"Total\t"<<total<<"\n";
}
main()
{
int i;
student s1,s[10];
fstream fp;
clrscr();
fp.open("bio.text",ios::ate|ios::in|ios::out|ios::binary);
cout<<"Enter student details \n";
for(i=1;i<=3;i++) /*Input of dtails into the file*/
{
s[i].read();
fp.write((char*)&s[i],sizeof(s[i]));
}
fp.seekg(0);
cout<<"Details\n\n"; /*Printing the details*/
for(i=1;i<=3;i++)
{
fp.read((char*)&s[i],sizeof(s[i]));
s[i].disp();
cout<<"\n";
}
int pt=fp.tellg();
int n=pt/sizeof(*s);
cout<<"No of student details entered : "<<n<<"\n";
cout<<"Enter object no to be updated : "; /* To update the current existing details of objects*/
int obj;
cin>>obj;
int loc=(obj-1)*sizeof(s1);
fp.seekp(loc);
cout<<"Enter new values \n";
s1.read();
fp.write((char*)&s1,sizeof(s1));
fp.seekg(0);
cout<<"Details\n\n"; /*Final print after the update*/
for(i=1;i<=3;i++)
{
fp.read((char*)&s[i],sizeof(s[i]));
s[i].disp();
cout<<"\n";
}
fp.close();
getch();
return 0;
}

Binary search tree


Program to perform binary tree construction...
Inorder traverse technique used to display the nodes of the tree..
Sample output for the program is given above.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
struct node
{
int info;
struct node *left,*right;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
return p;
}
nodeptr maketree(int x)
{
nodeptr p;
p=getnode();
p->info=x;
p->left=NULL;
p->right=NULL;
return p;
}

void setleft(nodeptr p,int x)
{
nodeptr q;
if(p==NULL||p->left!=NULL)
printf("\nInvalid operation..!");
else
{
q=maketree(x);
p->left=q;
}
}
void setright(nodeptr p,int x)
{
nodeptr q;
if(p==NULL||p->right!=NULL)
printf("\nInvalid operation..!");
else
{
q=maketree(x);
p->right=q;
}
}
void inorder(nodeptr p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d\t",p->info);
inorder(p->right);
}
}
main()
{
int ele,i;
nodeptr p,q,root;
clrscr();
printf("Insertion of element into the tree\n");
printf("\nEnter the root node element : ");scanf("%d",&ele);
printf("\nNode values accepted untill cltr+z is pressed..\n");
root=maketree(ele);
while(scanf("%d",&ele)!=EOF)
{
p=q=root;
while(p->info!=ele && q!=NULL)
{
p=q;
if(ele>p->info)
q=p->right;
else
q=p->left;
}
if(p->info==ele)
printf("Duplicate element entered..!\n");
else
if(ele<p->info)
setleft(p,ele);
else
setright(p,ele);
}
printf("\n +***--TREE--***+\n\n");
inorder(root);
getch();
return 0;
}

Single linked list

Program to perform operations on a singly linked list
Operations: -
1 : Insert element at the front
2 : Insert element at the end
3 : Insert element after element
4 : Insert element at a position
5 : Delete at the front
6 : Delete at the end
7 : Delete the element
8 : Delete element after

#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
struct node
{
int info;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
p->next=NULL;
return p;
}
void freenode(nodeptr p)
{
free(p);
}
nodeptr ins_front(nodeptr list,int x)
{
nodeptr p;
p=getnode();
p->info=x;
if(list==NULL)
{
list=p;
}
else
{
p->next=list;
list=p;
}
return list;
}
void display(nodeptr list)
{
nodeptr p;
if(list==NULL)
{
printf("\nList empty..!");
return;
}
for(p=list;p!=NULL;p=p->next)
printf("\n%d",p->info);
printf("\n");
}
nodeptr ins_end(nodeptr list,int x)
{
nodeptr p,q;
p=getnode();
p->info=x;
if(list==NULL)
{
list=p;
}
else
{
for(q=list;q->next!=NULL;q=q->next);
q->next=p;
}
return list;
}
nodeptr ins_y(nodeptr list,int x,int y)
{
nodeptr p,q;
int flag=0;
q=getnode();
q->info=x;
if(list==NULL)
{
printf("\nList empty..! Element not found");
}
else
{
for(p=list;p!=NULL;p=p->next)
if(p->info==y)
{
flag=1;
break;
}
if(flag)
{
for(p=list;p->info!=y;p=p->next);
q->next=p->next;
p->next=q;
}
}
return list;
}
nodeptr ins_pos(nodeptr list,int x,int pos)
{
nodeptr p,t,r;
int c=0,i;
p=getnode();
p->info=x;
for(t=list;t!=NULL;t=t->next)
c++;
if(pos==1)
{
p->next=list;
list=p;
}
else
{
if(pos<=c+1)
{
t=list;
for(i=1;i 'lesser than' pos-1;i++)
t=t->next;
r=t->next;
t->next=p;
p->next=r;
}
}
return list;
}
nodeptr del_front(nodeptr list)
{
nodeptr p;
int x;
if(list==NULL)
{
printf("\nInvalid operation...!");
return 0;
}
else
{
p=list;
list=list->next;
x=p->info;
freenode(p);
printf("\n%d deleted from the list",x);
return list;
}
}
nodeptr del_end(nodeptr list)
{
nodeptr p,q;
int x;
if(list==NULL)
{
printf("\nInvalid operation..!");
return 0;
}
else
{
q=NULL;
for(p=list;p->next!=NULL;p=p->next)
q=p;
x=p->info;
freenode(p);
q->next=NULL;
printf("\n%d deleted from the list..",x);
}
return list;
}
nodeptr del_y(nodeptr list,int y)
{
nodeptr p,q;
if(list==NULL)
{
printf("\nInvalid operaton..!");
return NULL;
}
else
if(list->info==y)
{
p=list;
list=list->next;
freenode(p);
printf("\n%d deleted from the list...",y);
}
else
{
q=NULL;
for(p=list;p!=NULL;p=p->next)
{
if(y==p->info)
break;
q=p;
}
q->next=p->next;
freenode(p);
printf("\n%d deleted from the list..",y);
}
return list;
}
nodeptr del_afty(nodeptr list,int y)
{
nodeptr p,q;
int x;
q=NULL;
for(p=list;p!=NULL;p=p->next)
{
if(p->info==y)
{
q=p->next;
p->next=q->next;
x=q->info;
freenode(q);
printf("\n%d is deleted from the list",x);
break;
}
}
return list;
}

main()
{
int x,y,pos,n,n1;
nodeptr list;
clrscr();
list=NULL;
printf("Operations\n-----------");
for(;;)
{

printf("\n1 : Insert\n2 : Delete\n3 : Display\n4 : Exit\n");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&n);
switch(n)
{
case 1:printf("\n1 : Insert element at the front\n2 : Insert element at the end\n3 : Insert element after element Y\n4 : Insert element at a position\n");
printf("\nEnter your option : ");
scanf("%d",&n1);
switch(n1)
{
case 1:printf("\nEnter the element to be inserted : ");
scanf("%d",&x);
clrscr();
printf("\n%d inserted at the front...\n",x);
list=ins_front(list,x);
break;
case 2:printf("\nEnter the element to be inserted : ");
scanf("%d",&x);
clrscr();
printf("\n%d inserted at the end...\n",x);
list=ins_end(list,x);
break;
case 3:printf("\nEnter the element to be inserted : ");scanf("%d",&x);
printf("Enter the element Y : ");scanf("%d",&y);
printf("\n%d inserted after element y",x);
clrscr();
list=ins_y(list,x,y);
break;
case 4:printf("\nEnter the element to be inserted : ");scanf("%d",&x);
printf("Enter the position : ");scanf("%d",&pos);
printf("\n%d inserted at position %d",x,pos);
clrscr();
list=ins_pos(list,x,pos);
break;
default :printf("\nInavalid operation!!");
break;
}
break;
case 2:printf("\n1 : Delete at the front\n2 : Delete at the end\n3 : Delete the element Y\n4 : Delete element after Y\n");
printf("\nEnter your option : ");scanf("%d",&n1);
switch(n1)
{
case 1:list=del_front(list);
break;
case 2:list=del_end(list);
break;
case 3:printf("\nEnter the element Y : ");scanf("%d",&y);
list=del_y(list,y);
break;
case 4:printf("\nEnter the element Y: ");scanf("%d",&y);
list=del_afty(list,y);
break;
default:printf("\nInvalid operation!!");
break;
}
break;
case 3:clrscr();
printf("\nElements in the list are\n------------------------");
display(list);
break;
case 4:exit(0);
break;
default:printf("\nInvalid operatiom...");
break;
}
}
getch();
return 0;
}