All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++
Wednesday, 8 April 2015
Friday, 12 December 2014
Least Recently Used Page Replacement Program
AIM: To Implement LRU Page Replacement Program
#include<iostream>using namespace std;
int main()
{
int p[50],fr[10],used[10];
int i,j,k,n,fs,flag=0,pf=0,index;
cout<<"Enter no of Refrence Strings: ";
cin>>n;
cout<<"Enter Refrence Strings: ";
for(i=0;i<n;i++)
cin>>p[i];
cout<<"Enter Frame Size: ";
cin>>fs;
Implement MVT with First Fit Memory Management Technique
AIM: Implement MVT with No of Partititon and Apply First Fit Memory Management.
#include<iostream>
using namespace std;
int main()
{
int tm,rm,os,n,i,ps,pn,pt[10],pt1[10];
int fra1,fra2,tf=0,tp=0,flag=0,c=0;
cout<<"Enter Total Memory: ";
cin>>tm;
cout<<"Enter Memory Given to OS: ";
cin>>os;
rm=tm-os;
cout<<"Enter No of Partiotion: ";
cin>>pn;
cout<<"Enter The Size for Each Partition\n";
for(i=0;i<pn;i++)
{
cout<<"\nPartition"<<i+1<<": ";
cin>>pt1[i];
if(tp+pt1[i]<rm)
{
#include<iostream>
using namespace std;
{
int tm,rm,os,n,i,ps,pn,pt[10],pt1[10];
int fra1,fra2,tf=0,tp=0,flag=0,c=0;
cout<<"Enter Total Memory: ";
cin>>tm;
cout<<"Enter Memory Given to OS: ";
cin>>os;
rm=tm-os;
cout<<"Enter No of Partiotion: ";
cin>>pn;
cout<<"Enter The Size for Each Partition\n";
for(i=0;i<pn;i++)
{
cout<<"\nPartition"<<i+1<<": ";
cin>>pt1[i];
if(tp+pt1[i]<rm)
{
Tuesday, 28 October 2014
LRU Page Replacement Program
LRU Page Replacement Program
#include<iostream>using namespace std;
int main()
{
int p[50],q[20],b[20],c2[20];
int c=0,k=0,c1,d,f,i,j,n,r,t;
cout<<"Enter no of pages:";
cin>>n;
for(i=0;i<n;i++)
cin>>p[i];
cout<<"Enter no of frames:";
cin>>f;
q[k]=p[k];
cout<<"\n\t"<<q[k]<<endl;
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
Tuesday, 21 October 2014
FIFO Page Replacement Program
To Implement FIFO Page Replacement
#include<iostream.h>#include<conio.h>
void main()
{
clrscr();
int a[50],frame[10],count=0;
cout<<"\n Enter The Number of Pages:";
cin>>n;
cout<<"\n Enter The Page Number :";
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"\n Enter The Number Of Frames :";
cin>>no;
for(i=0;i<no;i++)
frame[i]=-1;
j=0;
cout<<"\nRef_Str \t Page_frames\n";
for(i=1;i<=n;i++)
{
Friday, 19 September 2014
PROGRAM FOR MFT
#include<iostream>
using namespace std;
int main()
{
int m,os,p,s,p1;
int ps,i,f1=0,fra1;
cout<<"Enter the Memory Size: ";
cin>>m;
cout<<"Enter Memory given to OS: ";
cin>>os;
m-=os;
cout<<"Enter the no of Partitions: ";
cin>>p;
s=m/p;
cout<<"Each Partition size is: "<<s;
cout<<"\nEnter the no of Processes: ";
cin>>p1;
for(i=0;i<p1;i++)
{
using namespace std;
int main()
{
int m,os,p,s,p1;
int ps,i,f1=0,fra1;
cout<<"Enter the Memory Size: ";
cin>>m;
cout<<"Enter Memory given to OS: ";
cin>>os;
m-=os;
cout<<"Enter the no of Partitions: ";
cin>>p;
s=m/p;
cout<<"Each Partition size is: "<<s;
cout<<"\nEnter the no of Processes: ";
cin>>p1;
for(i=0;i<p1;i++)
{
Tuesday, 16 September 2014
Implement MVT
AIM: To Implement MVT.
#include<iostream.h>
int main()
{
int tm,rm,os,n,i,c=0,ps;
cout<<"Enter Total Memory: ";
cin>>tm;
cout<<"Enter Memory Given to OS: ";
cin>>os;
rm=tm-os;
cout<<"Enter No of Processes: ";
cin>>n;
for(i=0;i<n;i++)
{
Tuesday, 9 September 2014
C++ Code for Priority Based Scheduling Algorithm.
// Priority Based Scheduling Algorithm.
#include<iostream>
using namespace std;
struct process
{
int pno[10];
int p[10];
int bt[10];
}p;
int main()
{
int i,n,wt[10];
int twt=0;
float avg;
cout<<"Enter No of Processes: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<" Enter Process no,Priority and Brust time :";
cin>>p.pno[i]>>p.p[i]>>p.bt[i];
}
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(p.p[i]>p.p[j])
{
int temp=p.p[j];
p.p[j]=p.p[i];
p.p[i]=temp;
temp=p.bt[j];
p.bt[j]=p.bt[i];
p.bt[i]=temp;
temp=p.pno[j];
p.pno[j]=p.pno[i];
p.pno[i]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+p.bt[i-1];
twt+=wt[i];
}
cout<<"P_NO\tPriority\tB_Time\tW_time\n";
for(i=0;i<n;i++)
{
cout<<p.pno[i]<<"\t"<<p.p[i]<<"\t"<<p.bt[i]<<"\t"<<wt[i]<<endl;
}
avg=twt/(float) n;
cout<<"Total waiting Time="<<twt<<endl;
cout<<"Average Waiting Time="<<avg;
return 0;
}
#include<iostream>
using namespace std;
struct process
{
int pno[10];
int bt[10];
}p;
int main()
{
int i,n,wt[10];
int twt=0;
float avg;
cout<<"Enter No of Processes: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<" Enter Process no,Priority and Brust time :";
cin>>p.pno[i]>>p.p[i]>>p.bt[i];
}
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(p.p[i]>p.p[j])
{
int temp=p.p[j];
p.p[j]=p.p[i];
p.p[i]=temp;
temp=p.bt[j];
p.bt[j]=p.bt[i];
p.bt[i]=temp;
temp=p.pno[j];
p.pno[j]=p.pno[i];
p.pno[i]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+p.bt[i-1];
twt+=wt[i];
}
cout<<"P_NO\tPriority\tB_Time\tW_time\n";
for(i=0;i<n;i++)
{
cout<<p.pno[i]<<"\t"<<p.p[i]<<"\t"<<p.bt[i]<<"\t"<<wt[i]<<endl;
}
avg=twt/(float) n;
cout<<"Total waiting Time="<<twt<<endl;
cout<<"Average Waiting Time="<<avg;
return 0;
}
Round_robin using structures
#include<iostream.h>
#include<conio.h>
struct pro
{
int t[50]; //time of each turn
int burst; //burst time
int rem; //rem time of process
int tc; //time at which process is completed
int ti; //for counting purpose
int c; //set when process is completed
int tu; //number of turns
float wait; //total waiting time for that process
};
void main()
{ clrscr();
struct pro p[100];
int n,count=0,time_slice,s;
float avg_wait=0,total_wait=0;
cout<<"enter number of processes "<<endl;
cin>>n;
cout<<"enter burst time of each process "<<endl;
for(int i=0;i<n;i++)
{
cin>>p[i].burst;
p[i].rem=p[i].burst;
p[i].c=0;
p[i].ti=0;
p[i].tu=0;
}
s=n;
cout<<"enter time slice "<<endl;
cin>>time_slice;
while(s!=0)
{
for(i=0;i<n;i++)
{
if(p[i].c==0)
{
if(p[i].rem<time_slice)
{
count=count+p[i].rem;
p[i].tc=count;
p[i].t[p[i].ti]=p[i].rem;
p[i].rem=0;
p[i].tu++;
p[i].ti++;
}
else
{ count=count+time_slice;
p[i].tc=count;
p[i].t[p[i].ti]=time_slice;
p[i].rem=p[i].rem-time_slice;
p[i].tu++;
p[i].ti++;
}
if(p[i].rem==0)
{
p[i].c=1;
s--;
}
}
} }
for(i=0;i<n;i++)
{ int temp=0;
for(int j=0;j<p[i].tu;j++)
{ temp=temp+p[i].t[j];
}
p[i].wait=p[i].tc-temp;
}
cout<<endl;
cout<<"process name\twaiting time\tturns "<<endl;
for(i=0;i<n;i++)
{
cout<<"p"<<i+1<<"\t\t\t"<<p[i].wait<<"\t"<<p[i].tu<<endl;
total_wait=total_wait+p[i].wait;
}
cout<<endl<<"total waiting time of all processes= "<<total_wait<<endl;
avg_wait=total_wait/n;
cout<<endl<<"average waiting time= "<<avg_wait;
getch();
}
#include<conio.h>
struct pro
{
int t[50]; //time of each turn
int burst; //burst time
int rem; //rem time of process
int tc; //time at which process is completed
int ti; //for counting purpose
int c; //set when process is completed
int tu; //number of turns
float wait; //total waiting time for that process
};
void main()
{ clrscr();
struct pro p[100];
int n,count=0,time_slice,s;
float avg_wait=0,total_wait=0;
cout<<"enter number of processes "<<endl;
cin>>n;
cout<<"enter burst time of each process "<<endl;
for(int i=0;i<n;i++)
{
cin>>p[i].burst;
p[i].rem=p[i].burst;
p[i].c=0;
p[i].ti=0;
p[i].tu=0;
}
s=n;
cout<<"enter time slice "<<endl;
cin>>time_slice;
while(s!=0)
{
for(i=0;i<n;i++)
{
if(p[i].c==0)
{
if(p[i].rem<time_slice)
{
count=count+p[i].rem;
p[i].tc=count;
p[i].t[p[i].ti]=p[i].rem;
p[i].rem=0;
p[i].tu++;
p[i].ti++;
}
else
{ count=count+time_slice;
p[i].tc=count;
p[i].t[p[i].ti]=time_slice;
p[i].rem=p[i].rem-time_slice;
p[i].tu++;
p[i].ti++;
}
if(p[i].rem==0)
{
p[i].c=1;
s--;
}
}
} }
for(i=0;i<n;i++)
{ int temp=0;
for(int j=0;j<p[i].tu;j++)
{ temp=temp+p[i].t[j];
}
p[i].wait=p[i].tc-temp;
}
cout<<endl;
cout<<"process name\twaiting time\tturns "<<endl;
for(i=0;i<n;i++)
{
cout<<"p"<<i+1<<"\t\t\t"<<p[i].wait<<"\t"<<p[i].tu<<endl;
total_wait=total_wait+p[i].wait;
}
cout<<endl<<"total waiting time of all processes= "<<total_wait<<endl;
avg_wait=total_wait/n;
cout<<endl<<"average waiting time= "<<avg_wait;
getch();
}
Tuesday, 26 August 2014
Round Robin Algorithm
using namespace std;
int main()
{
int n,i,ts,rp,t;
int twt=0,tat=0,f=0;
int bt[i],rt[i];
cout<<"Enter No of Process :";
cin>>n;
rp=n;
for(i=0;i<n;i++)
{
cout<<"Enter Burst time for Process"<<i<<": ";
cin>>bt[i];
rt[i]=bt[i];
}
cout<<"Enter Time Slice: ";
cin>>ts;
cout<<"\nP_NO\tB_Time\tW_time\tT_Time\n";
for(t=0,i=0;rp!=0;)
{
if(rt[i]>ts)
{
rt[i]-=ts;
t+=ts;
}
else if(rt[i]<=ts&&rt[i]>0)
{
t+=rt[i];
rt[i]=0;
f=1;
}
if(rt[i]==0&&f==1)
{
rp--;
cout<<"P"<<i<<"\t"<<bt[i]<<"\t"<<t-bt[i]<<"\t"<<t<<endl;
twt+=t-bt[i];
tat+=t;
f=0;
}
if(i==n-1)
i=0;
else
i++;
}
cout<<"\nTotal Waiting Time="<<twt<<"\t\t";
cout<<"Average Waiting time="<<twt/(float)n;
cout<<"\nTotal Turnaround Time="<<tat<<"\t";
cout<<"Average Turnaround Time="<<tat/(float)n;
return 0;
}
Friday, 8 August 2014
Shortest Job First (Non-PreEmptive)
/*Shortest Job First Scheduling Progrmam (Non-PreEmptive) */
#include<iostream>
using namespace std;
int main()
{
int i,j,n,total=0,temp,ptemp;
int wtime[10],btime[10],process[10];
float avg=0;
cout<<"\nEnter number of Processes: ";
cin>>n;
for(i=0;i<n;i++)
{
process[i]=i;
cout<<"Enter P"<<i+1<<" Burst Time:";
cin>>btime[i];
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(btime[i]>btime[j])
{
temp=btime[i];
btime[i]=btime[j];
btime[j]=temp;
ptemp=process[i];
process[i]=process[j];
process[j]=ptemp;
}
}
}
wtime[0]=0;
for(i=1;i<n;i++)
{
wtime[i]=wtime[i-1]+btime[i-1];
total=total+wtime[i];
}
avg=(float)total/n;
cout<<"\nP_ID\tB_TIME\t W_TIME\n";
for(i=0;i<n;i++)
cout<<process[i]+1<<"\t"<<btime[i]<<"\t"<<wtime[i]<<endl;
cout<<"\nTotal Waiting Time: "<<total;
cout<<"\nAverage Waiting Time: "<<avg;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i,j,n,total=0,temp,ptemp;
int wtime[10],btime[10],process[10];
float avg=0;
cout<<"\nEnter number of Processes: ";
cin>>n;
for(i=0;i<n;i++)
{
process[i]=i;
cout<<"Enter P"<<i+1<<" Burst Time:";
cin>>btime[i];
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(btime[i]>btime[j])
{
temp=btime[i];
btime[i]=btime[j];
btime[j]=temp;
ptemp=process[i];
process[i]=process[j];
process[j]=ptemp;
}
}
}
wtime[0]=0;
for(i=1;i<n;i++)
{
wtime[i]=wtime[i-1]+btime[i-1];
total=total+wtime[i];
}
avg=(float)total/n;
cout<<"\nP_ID\tB_TIME\t W_TIME\n";
for(i=0;i<n;i++)
cout<<process[i]+1<<"\t"<<btime[i]<<"\t"<<wtime[i]<<endl;
cout<<"\nTotal Waiting Time: "<<total;
cout<<"\nAverage Waiting Time: "<<avg;
return 0;
}
Tuesday, 5 August 2014
FCFS Program
#include<iostream.h>
#include<conio.h>
void main()
{
int i, n, process[10], total=0, wtime[10], ptime[10],turntotal[10];
float avg=0;
clrscr();
cout<<"\nEnter number of Processes:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Process "<<i+1<<" ID:";
cin>>process[i];
cout<<"Enter Process "<<i+1<<" Burst Time:";
cin>>ptime[i];
}
wtime[0]=0;
for(i=1;i<n;i++)
{
wtime[i]=wtime[i-1]+ptime[i-1];
total=total+wtime[i];
}
for(i=0;i<n;i++)
{
turntotal[i]=ptime[i]+wtime[i];
}
avg=(float)total/n;
cout<<"\nP_ID\t P_TIME\t W_TIME\t TURN_Time\n";
for(i=0;i<n;i++)
cout<<process[i]<<"\t"<<ptime[i]<<"\t"<<wtime[i]<<"\t"<<turntotal[i]<<"\n";
cout<<"\nTotal Waiting Time:"<<total;
cout<<"\nAverage Waiting Time:"<<avg;
getch();
}
#include<conio.h>
void main()
{
int i, n, process[10], total=0, wtime[10], ptime[10],turntotal[10];
float avg=0;
clrscr();
cout<<"\nEnter number of Processes:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Process "<<i+1<<" ID:";
cin>>process[i];
cout<<"Enter Process "<<i+1<<" Burst Time:";
cin>>ptime[i];
}
wtime[0]=0;
for(i=1;i<n;i++)
{
wtime[i]=wtime[i-1]+ptime[i-1];
total=total+wtime[i];
}
for(i=0;i<n;i++)
{
turntotal[i]=ptime[i]+wtime[i];
}
avg=(float)total/n;
cout<<"\nP_ID\t P_TIME\t W_TIME\t TURN_Time\n";
for(i=0;i<n;i++)
cout<<process[i]<<"\t"<<ptime[i]<<"\t"<<wtime[i]<<"\t"<<turntotal[i]<<"\n";
cout<<"\nTotal Waiting Time:"<<total;
cout<<"\nAverage Waiting Time:"<<avg;
getch();
}
Friday, 28 March 2014
Searching in Sorted Linked List (Ascending or Descending)
//Searching In Sorted Linked List
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
int main()
{
node *start,*p,*pp,*temp;
start=p=NULL;
//Creating Linked List
int n;
cout<<"Press [0] to Exit :: Input Elements To List :";
do
{
cin>>n;
if(n)
{
temp=new node;
temp->info=n;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
}
}while(n);
//printing nodes
p=start;
cout<<"Created Nodes are : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
//Check Accending or descending
pp=start;p=start->link;
if (pp->info<p->info)
{
cout<<"\nAsecnding list \n";
/////Search location
cout<<"Enter Item to Search :";
int item;
cin>>item;
int loc=0,k=1;
if(item<p->info)
{
cout<<"Not Present" ;
exit(0);
}
while(p!=NULL)
{
if(item==p->info)
{
k++;
loc=k;
break;
}
else
{
p=p->link;
k++;
}
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}
else
{
cout<<"\nDescending List \n";
cout<<"Enter Item to Search :";
int item;
cin>>item;
int loc=0,k=1;
if(item>p->info)
{
cout<<"Not Present" ;
exit(0);
}
while(p!=NULL)
{
if(item==p->info)
{
k++;
loc=k;
break;
}
else
{
p=p->link;
k++;
}
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}
return 0;
}
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
int main()
{
node *start,*p,*pp,*temp;
start=p=NULL;
//Creating Linked List
int n;
cout<<"Press [0] to Exit :: Input Elements To List :";
do
{
cin>>n;
if(n)
{
temp=new node;
temp->info=n;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
}
}while(n);
//printing nodes
p=start;
cout<<"Created Nodes are : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
//Check Accending or descending
pp=start;p=start->link;
if (pp->info<p->info)
{
cout<<"\nAsecnding list \n";
/////Search location
cout<<"Enter Item to Search :";
int item;
cin>>item;
int loc=0,k=1;
if(item<p->info)
{
cout<<"Not Present" ;
exit(0);
}
while(p!=NULL)
{
if(item==p->info)
{
k++;
loc=k;
break;
}
else
{
p=p->link;
k++;
}
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}
else
{
cout<<"\nDescending List \n";
cout<<"Enter Item to Search :";
int item;
cin>>item;
int loc=0,k=1;
if(item>p->info)
{
cout<<"Not Present" ;
exit(0);
}
while(p!=NULL)
{
if(item==p->info)
{
k++;
loc=k;
break;
}
else
{
p=p->link;
k++;
}
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}
return 0;
}
Insertion in A Sorted Linked List (Ascending or Descending)
//Insertion in Sorted Linked List
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
int main()
{
node *start,*p,*pp,*temp;
start=p=NULL;
//Creating Linked List
int n;
cout<<"Press [0] to Exit :: Input Elements To List :";
do
{
cin>>n;
if(n)
{
temp=new node;
temp->info=n;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
}
}while(n);
//printing nodes
p=start;
cout<<"Created Nodes are : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
//find Location and insert item
cout<<"Enter Item to Insert :";
int item;
cin>>item;
if(start==NULL)
{
temp=new node;
temp->info=item;
temp->link=NULL;
start=temp;
p=temp;
}
if(item<start->info)
{
p=start;
temp=new node;
temp->info=item;
temp->link=p;
start=temp;
p=temp;
}
pp=start;
p=start->link;
while(p!=NULL)
{
if(item<p->info)
{
temp=new node;
temp->info=item;
pp->link=temp;
temp->link=p;
break;
}
pp=p,p=p->link;
}
if(p==NULL)
{
temp=new node;
temp->info=item;
pp->link=temp;
temp->link=NULL;
}
//printing
p=start;
cout<<"After Insertion : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
return 0;
}
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
int main()
{
node *start,*p,*pp,*temp;
start=p=NULL;
//Creating Linked List
int n;
cout<<"Press [0] to Exit :: Input Elements To List :";
do
{
cin>>n;
if(n)
{
temp=new node;
temp->info=n;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
}
}while(n);
//printing nodes
p=start;
cout<<"Created Nodes are : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
//find Location and insert item
cout<<"Enter Item to Insert :";
int item;
cin>>item;
if(start==NULL)
{
temp=new node;
temp->info=item;
temp->link=NULL;
start=temp;
p=temp;
}
if(item<start->info)
{
p=start;
temp=new node;
temp->info=item;
temp->link=p;
start=temp;
p=temp;
}
pp=start;
p=start->link;
while(p!=NULL)
{
if(item<p->info)
{
temp=new node;
temp->info=item;
pp->link=temp;
temp->link=p;
break;
}
pp=p,p=p->link;
}
if(p==NULL)
{
temp=new node;
temp->info=item;
pp->link=temp;
temp->link=NULL;
}
//printing
p=start;
cout<<"After Insertion : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
return 0;
}
Insertion In A Linked List At Some Location
//Insertion At Location
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
int main()
{
node *start,*p,*loc,*temp;
start=p=NULL;
//Creating Linked List
int n;
cout<<"Press [0] to Exit :: Input Elements To List :";
do
{
cin>>n;
if(n)
{
temp=new node;
temp->info=n;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
}
}while(n);
//printing nodes
p=start;
cout<<"Created Nodes are : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
///////////////////////////////
cout<<"\nEnter item to insert ";
int item;
cin>>item;
cout<<"Place where to Insert :";
int k;
cin>>k;
loc=NULL;
p=start;
for(int i=1;i<=k-1;i++)
{
p=p->link;
loc=p;
}
temp=new node;
temp->info=item;
if(loc==NULL)
{
temp->link=start;
start=temp;
}
else
{
temp->link=loc->link;
loc->link=temp;
}
//printing
p=start;
cout<<"List After Insertion : ";
while(p!=NULL)
{
cout<<p->info<<" ";
p=p->link;
}
return 0;
}
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
int main()
{
node *start,*p,*loc,*temp;
start=p=NULL;
//Creating Linked List
int n;
cout<<"Press [0] to Exit :: Input Elements To List :";
do
{
cin>>n;
if(n)
{
temp=new node;
temp->info=n;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
}
}while(n);
//printing nodes
p=start;
cout<<"Created Nodes are : ";
while(p)
{
cout<<p->info<<" ";
p=p->link;
}
///////////////////////////////
cout<<"\nEnter item to insert ";
int item;
cin>>item;
cout<<"Place where to Insert :";
int k;
cin>>k;
loc=NULL;
p=start;
for(int i=1;i<=k-1;i++)
{
p=p->link;
loc=p;
}
temp=new node;
temp->info=item;
if(loc==NULL)
{
temp->link=start;
start=temp;
}
else
{
temp->link=loc->link;
loc->link=temp;
}
//printing
p=start;
cout<<"List After Insertion : ";
while(p!=NULL)
{
cout<<p->info<<" ";
p=p->link;
}
return 0;
}
Thursday, 27 March 2014
Merging two Accending Arrays
//Merging two Accending Arrays
#include<iostream.h>
int main()
{
int a[50],b[50];
int i,j,k,m,n;
cout<<"enter no of elements in A ";
cin>>m;
for(i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"enter no of elements in B ";
cin>>n;
for(j=0;j<n;j++)
{
cin>>b[j];
}
//////////////////////////////////////
int s=m+n;
int p=0;j=0;
label:i=p;
for(i=p;i<m;i++)
{
if(b[j]<a[i])
{
for(k=m;k>i;k--)
{
a[k]=a[k-1];
}
a[i]=b[j];
m++;
j++;
p=i+1;
goto label;
}
}
if(m<s)
{
for(i=j;i<=n;i++,j++)
{
a[m]=b[j];
m++;
}
}
for(i=0;i<s;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
#include<iostream.h>
int main()
{
int a[50],b[50];
int i,j,k,m,n;
cout<<"enter no of elements in A ";
cin>>m;
for(i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"enter no of elements in B ";
cin>>n;
for(j=0;j<n;j++)
{
cin>>b[j];
}
//////////////////////////////////////
int s=m+n;
int p=0;j=0;
label:i=p;
for(i=p;i<m;i++)
{
if(b[j]<a[i])
{
for(k=m;k>i;k--)
{
a[k]=a[k-1];
}
a[i]=b[j];
m++;
j++;
p=i+1;
goto label;
}
}
if(m<s)
{
for(i=j;i<=n;i++,j++)
{
a[m]=b[j];
m++;
}
}
for(i=0;i<s;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
Saturday, 15 March 2014
Creating Nodes in Singly Linked List Using Structure.
//Creating Nodes in Singly Linked List Using Structure.
#include<iostream.h>
using namespace std;
struct node
{
char info;
node *link;
};
int main()
{
node *start,*p,*temp;
start=p=NULL;
char ch;
do
{
char item;
cout<<"Input Element to info :";
cin>>item;
temp=new node;
temp->info=item;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
cout<<"Create More Nodes y or n :";
cin>>ch;
}while(ch=='y');
//printing nodes
p=start;
while(p)
{
cout<<p->info;
p=p->link;
}
return 0;
}
#include<iostream.h>
using namespace std;
struct node
{
char info;
node *link;
};
int main()
{
node *start,*p,*temp;
start=p=NULL;
char ch;
do
{
char item;
cout<<"Input Element to info :";
cin>>item;
temp=new node;
temp->info=item;
temp->link=NULL;
if(start==NULL)
start=temp;
else
p->link=temp;
p=temp;
cout<<"Create More Nodes y or n :";
cin>>ch;
}while(ch=='y');
//printing nodes
p=start;
while(p)
{
cout<<p->info;
p=p->link;
}
return 0;
}
Thursday, 13 March 2014
Creation of linklist by using the concept of Parallel Array
//Creation of linklist by using the concept of Parallel Array
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int info[10],link[10],item,p,i;
for(i=1;i<10;i++)
{link[i]=i+1;}
link[i]=0;
int start=0,free=1,node=0;
char ch;
do
{
if(start==0)
{
start=free;
cout<<"enter the item to insert ";
cin>>item;
info[start]=item;
free=link[free];
link[start]=0;
node++;
}
else
{
p=start;
while(link[p]!=0)
{p=link[p];}
link[p]=free;
p=link[p];
cout<<"enter the no to be insert ";
cin>>item;
info[p]=item;
free=link[free];
link[p]=0;
node++;
}
cout<<"wants to continue y or n: ";
cin>>ch;
}while(ch=='y');
cout<<"created linklist are ";
for(i=1;i<=node;i++)
cout<<info[i]<<" ";
return 0;
getch();
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int info[10],link[10],item,p,i;
for(i=1;i<10;i++)
{link[i]=i+1;}
link[i]=0;
int start=0,free=1,node=0;
char ch;
do
{
if(start==0)
{
start=free;
cout<<"enter the item to insert ";
cin>>item;
info[start]=item;
free=link[free];
link[start]=0;
node++;
}
else
{
p=start;
while(link[p]!=0)
{p=link[p];}
link[p]=free;
p=link[p];
cout<<"enter the no to be insert ";
cin>>item;
info[p]=item;
free=link[free];
link[p]=0;
node++;
}
cout<<"wants to continue y or n: ";
cin>>ch;
}while(ch=='y');
cout<<"created linklist are ";
for(i=1;i<=node;i++)
cout<<info[i]<<" ";
return 0;
getch();
}
Thursday, 6 March 2014
Creation Of A Node In Singly Link List using Structure in C
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
//-------------------------------------------------
struct node
{
int data;
struct node *next;
}*start=NULL;
//-------------------------------------------------
void create()
{
char ch;
do
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("\n Do you want to creat another : ");
ch=getche();
}while(ch!='n');
}
//------------------------------------------------------------------
void display()
{
struct node *new_node;
printf("The Linked List : \n");
new_node=start;
while(new_node!=NULL)
{
printf("%d->",new_node->data);
new_node=new_node->next;
}
printf(" NULL");
}
//----------------------------------------------------
void main()
{
clrscr();
create();
display();
getch();
}
//----------------------------------------------------
#include<conio.h>
#include<alloc.h>
//-------------------------------------------------
struct node
{
int data;
struct node *next;
}*start=NULL;
//-------------------------------------------------
void create()
{
char ch;
do
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("\n Do you want to creat another : ");
ch=getche();
}while(ch!='n');
}
//------------------------------------------------------------------
void display()
{
struct node *new_node;
printf("The Linked List : \n");
new_node=start;
while(new_node!=NULL)
{
printf("%d->",new_node->data);
new_node=new_node->next;
}
printf(" NULL");
}
//----------------------------------------------------
void main()
{
clrscr();
create();
display();
getch();
}
//----------------------------------------------------
Wednesday, 26 February 2014
Merging At Last of A Tabular Array
//Merging At Last of A Tabular Array
#include<iostream.h>
#include<conio.h>
int main()
{
int a[10],d[10];
float b[10],e[10];
char c[10][10],f[10][10];
int m,n,i,j;
cout<<"Enter no of elements in List 1 & List 2";
cin>>m>>n;
cout<<"Enter Roll no, Marks & Name of List 1"<<endl;
for(i=0;i<m;i++)
{
cin>>a[i]>>b[i];
cin>>c[i];
}
cout<<"Enter Roll no, Marks & Name of List 2"<<endl;
for(j=0;j<n;j++)
{
cin>>d[j]>>e[j];
cin>>f[j];
}
//merging
for(i=0;i<n;i++)
{
a[m+i]=d[i];
b[m+i]=e[i];
strcpy(c[m+i],f[i]);
}
//printing Table
cout<<"Roll No"<<"\t"<<"Marks"<<"\t"<<"Name"<<endl;
for(i=0;i<m+n;i++)
{
cout<<a[i]<<"\t"<<b[i]<<"\t";
cout<<c[i]<<endl;
}
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
int a[10],d[10];
float b[10],e[10];
char c[10][10],f[10][10];
int m,n,i,j;
cout<<"Enter no of elements in List 1 & List 2";
cin>>m>>n;
cout<<"Enter Roll no, Marks & Name of List 1"<<endl;
for(i=0;i<m;i++)
{
cin>>a[i]>>b[i];
cin>>c[i];
}
cout<<"Enter Roll no, Marks & Name of List 2"<<endl;
for(j=0;j<n;j++)
{
cin>>d[j]>>e[j];
cin>>f[j];
}
//merging
for(i=0;i<n;i++)
{
a[m+i]=d[i];
b[m+i]=e[i];
strcpy(c[m+i],f[i]);
}
//printing Table
cout<<"Roll No"<<"\t"<<"Marks"<<"\t"<<"Name"<<endl;
for(i=0;i<m+n;i++)
{
cout<<a[i]<<"\t"<<b[i]<<"\t";
cout<<c[i]<<endl;
}
getch();
return 0;
}
Subscribe to:
Posts (Atom)