Linked list inserting with making two linked list & find the desired node

Standard

[enlighter lang=”c++”]

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct list
{
int data;
struct list *link;
};
int main()
{
struct list *head1, *nn, *cur1,*head2 ,*cur2,*ptr;
int i=0,j=0,n,k;
printf(“Enter the data for head1: n”);
while(++i)
{
if(i==1){
nn=(struct list*)malloc(sizeof(struct list*));
scanf(“%d”,&nn->data);
head1=nn;
cur1=nn;
}
else{
nn=(struct list*)malloc(sizeof(struct list*));
scanf(“%d”,&nn->data);
if((nn->data)==-1){
cur1->link=NULL;
break;
}
else
cur1->link=nn;
cur1=nn;
}
}
printf(“Enter the data for head2: n”);
while(++j){
if(j==1){
nn=(struct list*)malloc(sizeof(struct list*));
scanf(“%d”,&nn->data);
head2=nn;
cur2=nn;
}
else{
nn=(struct list*)malloc(sizeof(struct list*));
scanf(“%d”,&nn->data);
if((nn->data)==-1){
cur2->link=NULL;
break;
}
else
cur2->link=nn;
cur2=nn;
}
}
cur1=head1;
printf(“The data for head1nn”);
while(cur1!=NULL)
{
printf(“Current node add : %dnnCurrent node Data : %dnn”,cur1,cur1->data);
cur1=cur1->link;
printf(“Next node add: %dnn”,cur1);
}

printf(“The total num of node of head1: %dnn”,i-1);
printf(“The data for head2nn”);
cur2=head2;
while(cur2!=NULL){
printf(“Current node add : %dnnCurrent node Data : %dnn”,cur2,cur2->data);
cur2=cur2->link;
printf(“Next node add: %dnn”,cur2);
}
printf(“The total num of node of head2: %dn”,j-1);
printf(“Enter the node no: n”);
scanf(“%d”,&n);
cur1=head1;
for(k=1;k<n;k++){
cur1=cur1->link;
}
printf(“Data : %dnadd: %dn”,cur1->data,cur1);
nn=(struct list*)malloc(sizeof(struct list*));
printf(“Enter the data for insertingnn”);
scanf(“%d”,&nn->data);
nn->link=cur1->link;
cur1->link=nn;
cur1=head1;
printf(“The data of new head1:n”);
while(cur1!=NULL){
printf(“current add: %dnncurrent data: %dnn”,cur1,cur1->data);
cur1=cur1->link;
printf(“next node add: %dnn”,cur1);
}

return 0;
}

[/enlighter]

Link List Merging

Standard

[enlighter lang=”c++”]
/****************************************
* Author: Habibur Rahman Habib *
* Description: Link list Merging…*
* Date: 01th Feb, 2014 *
****************************************/

#include
#include
#include
#include

using namespace std;
struct list
{
int data;
struct list *link;
};

int main()
{
struct list *head1,*head2, *ptr1,*ptr2,*nn, *current;
int i=0,d,j,sum=0;
cout<>d;
if(i==1&&d>=0)
{
nn=(struct list*)malloc(sizeof(struct list*));
nn->data=d;
head1=nn;
ptr1=nn;
current=nn;
}
else if(i>1&&d>=0)
{
nn=(struct list*)malloc(sizeof(struct list*));
nn->data=d;
current->link=nn;
current=nn;
}
else if(i>1&&dlink=NULL;
break;
}
}
cout<<"nnCurrent Node addresstCurrent Node datat Linkn";
while(ptr1)
{
cout<<"t"<<ptr1<<" tt "<data<<" tt"<link<link;
}
i=0;
while(++i)
{
cin>>d;
if(i==1&&d>=0)
{
nn=(struct list*)malloc(sizeof(struct list*));
nn->data=d;
head2=nn;
ptr2=nn;
current=nn;
}
else if(i>1&&d>=0)
{
nn=(struct list*)malloc(sizeof(struct list*));
nn->data=d;
current->link=nn;
current=nn;
}
else if(i>1&&dlink=NULL;
break;
}
}
cout<<"nnCurrent Node addresstCurrent Node datat Linkn";
while(ptr2)
{
cout<<"t"<<ptr2<<" tt "<data<<" tt"<link<link;
}
ptr1=head1;
while(1)
{
if(ptr1->link==NULL){
ptr1->link=head2;
break;
}
ptr1=(struct list*) ptr1->link;
}
current=head1;
cout<<"Merging Output:"<<endl;
cout<<"nnCurrent Node addresstCurrent Node datat Linkn";
i=0;
while(current)
{
i++;
cout<<"t"<<current<<" tt "<data<<" tt"<link<data;
current=(struct list*)current->link;
}
cout<<"nnTotal node: "<<i<<endl;
cout<<"nnSum: "<<sum<<endl;
return 0;
}
[/enlighter]

Light Online Judge 1015:

Standard

[enlighter lang=”c”]
#include
int main()
{
int T,N,k,l=0,sum;
scanf(“%d”,&T);
if(T<=100)
while(T–){
scanf("%d",&N);
if(N0)
sum+=k;
}
printf(“Case %d: %dn”,++l,sum);
}
}
return 0;
}
[/enlighter]

Light Online Judge 1053:

Standard

#include
#include
int main()
{
long int max=0,T,a,b,c,l=0,test;
scanf(“%ld”,&T);
while(T–)
{
scanf(“%ld%ld%ld”,&a,&b,&c);
if(a>b && a>c){
test=sqrt(pow(b,2)+pow(c,2));
if(test==a) printf(“Case %d: yesn”,++l);
else printf(“Case %d: non”,++l);
}
else if(b>a && b>c){
test=sqrt(pow(a,2)+pow(c,2));
if(test==b) printf(“Case %d: yesn”,++l);
else printf(“Case %d: non”,++l);
}
else
{
test=sqrt(pow(a,2)+pow(b,2));
if(test==c) printf(“Case %d: yesn”,++l);
else printf(“Case %d: non”,++l);
}
}
return 0;
}

Light Online Judge 1054: Efficient Psedu Code

Standard

#include
#include
#define mod 1000000007
int main()
{
int test,k=0,n,m;
scanf(“%d”,&test);
if(test=1&&m>=0)
{
p=pow(n,m);
v=p;

for(i=2;i<v;i++)
{
if(p%i==0)
{
if(i==p/i)
count+=i;
else count+=i+(p/i);
v=p/i;
}
}

count+=1+p;
printf("Case %d: %ldn",++k,count%mod);
}
}
}
return 0;
}