akie
BAN USERstudent
void insert(node **head,int data)
{
if(!head)
return;
struct node *temp=head;
struct node * newnode=(struct node *)(malloc(sizeof(struct node)));
newnode->next=NULL;
while(temp)
{
if(temp->data<data)
{temp=temp->next;
}
else
{
newnode->next=temp->next;
temp->next=newnode;
break;
}
}
}
code in C
- akie July 21, 2013#include<stdio.h>
struct node
{
int data;
struct node * next;
struct node * other;
};
struct node * newnode(int data)
{
struct node * newnode=(struct node *)malloc(sizeof(struct node));
newnode->next=NULL;
newnode->other=NULL;
newnode->data=data;
return newnode;
}
void func(struct node * head1,struct node **head2)
{
static struct node * temp=NULL;
if (head1!=NULL)
{
func(head1->next,head2);
struct node * newn= newnode(head1->data);
newn->next=temp;
*head2=newn;
temp=newn;
}
}
void fun(struct node * head1, struct node **head2)
{
struct node *resultlist=*head2;
struct node *head1temp=head1->next;
while(head1!=NULL)
{
head1->next=(*head2);
(*head2)->other=head1;
(*head2)=(*head2)->next;
head1=head1temp;
if(head1temp)
head1temp=head1temp->next;
}
*head2=resultlist;
while((*head2)!=NULL)
{
(*head2)->other=(*head2)->other->other->next;
(*head2)=(*head2)->next;
}
*head2=resultlist;
}
void printlist(struct node * root)
{
while(root!=NULL)
{
printf("\n data of node is :%d",root->data);
printf("\n other of this node is :%d",(root->other)->data);
if(root->next)
printf("\n next of this node is :%d",(root->next)->data);
printf("\n\n");
root=root->next;
}
}
int main()
{
struct node* head2=NULL;
struct node * head1=newnode(1);
head1->next=newnode(2);
head1->next->next=newnode(3);
head1->other=head1->next->next;
head1->next->other=head1;
head1->next->next->other=head1->next;
printf("\n\noriginal list is :\n \n");
printlist(head1);
func(head1,&head2);
fun(head1,&head2);
printf("\n\n copy list is :\n \n");
printlist(head2);
return 0;
}
Output:
original list is :
data of node is :1
other of this node is :3
next of this node is :2
data of node is :2
other of this node is :1
next of this node is :3
data of node is :3
other of this node is :2
copy list is :
data of node is :1
other of this node is :3
next of this node is :2
data of node is :2
other of this node is :1
next of this node is :3
data of node is :3
other of this node is :2
this is the full code in C . but it doesnt preserve the original list.
@jack, typecasting is done to convert to char pointer because if it is ,lets say,a float pointer,the statement
(&data+1)-&data,
would give the correct size/4 (4 i.e the size of float)
whereas
(char *)(&data+1)-(char *)(&data)
gives us the correct size/1 (1 i.e. the size of char)
RepDedicated administrative assistant with years of experience managing large and small offices. I have worked with numerous branches,including payroll ...
Repkenneroce, Cheap Escort Service in Lajpat Nagar 8447779280 Call Girls at Persistent Systems
Hi, I am Kenne working as a LAN manager. One day, I read how to break up a couple from ...
Repkylecfarrell, Personnel at Bocada
Property custodian with a reputed organization and help in keeping accurate check over the supplies and inventory of materials and ...
Repnnatashanaomi, Android test engineer at 247quickbookshelp
I am Marketing managers who play a crucial role in helping a business to promote and its customers. I manage ...
can you explain it a bit more? how to access the parent?
- akie September 06, 2013