33,007
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* creat()
{
struct node *p,*q,*h;
int i,n;
printf("输入想创建的节点个数:");
scanf("%d",&n);
h=(struct node*)malloc(sizeof(struct node));
h->data=n;
h->next=NULL;
q=h;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
printf("input num[%d]:",i);
q->next=p;
scanf("%d",&p->data);
p->next=NULL;
q=p;
}
printf("创建的链表为:");
return h;
}
void print(struct node *h)
{
struct node *p;
p=h->next;
while(p!=NULL)
{
printf("->%d",p->data);
p=p->next;
}
printf("\n");
}
struct node* contrary(struct node *head)
{
struct node *h,*p,*q,*r;
h=head;
p=head->next;
q=h;
q->next=NULL;
while(p!=NULL)
{
r=p->next;
p->next=q->next;
q->next=p;
p=r;
}
printf("逆置后的链表:");
return h;
}
main()
{
struct node *head,*head1;
head=creat();
print(head);
head1=contrary(head);
print(head1);
}
#include <stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void print(struct node *h)
{
struct node *p;
p=h->next;
while(p!=NULL)
{
printf("->%d",p->data);
p=p->next;
}
printf("\n\n");
}
struct node* order(struct node *head)
{
struct node *h,*p,*q;
int i,n,chg;
printf("ÊäÈ뵱ǰÅÅÐòÁ´±í³¤¶È:");
scanf("%d",&n);
h=head;
for(i=0;i<n-1;i++)
for(q=h->next,p=q->next;p!=NULL;q=p,p=p->next)
if(q->data>p->data)
{
chg=p->data;
p->data=q->data;
q->data=chg;
}
printf("ÅÅÐòºóµÄÁ´±íΪ:");
print(h);
return h;
}
struct node* creat()
{
int i,n;
struct node *p,*q,*h;
printf("ÊäÈëÒª´´½¨µÄÁ´±í³¤¶È:");
scanf("%d",&n);
h=(struct node*)malloc(sizeof(struct node));
h->data=n;
h->next=NULL;
q=h;
for(i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
printf("input num[%d]:",i);
scanf("%d",&p->data);
p->next=NULL;
q->next=p;
q=p;
}
printf("´´½¨µÄÁ´±íΪ:");
print(h);
return h;
}
struct node* join(struct node *head1,struct node *head2)
{
struct node *h,*p,*p1,*p2;
h=(struct node *)malloc(sizeof(struct node));
h->data=0;
h->next=NULL;
p=h;
p1=head1->next;
p2=head2->next;
while(p1&&p2)
{
if(p1->data<p2->data)
{
p->next=p1;
p=p1;
p1=p1->next;
}
else
{
p->next=p2;
p=p2;
p2=p2->next;
}
}
p->next=p1?p1:p2;
printf("ºÏ²¢ºóµÄÁ´±íΪ:");
print(h);
return h;
}
void main()
{
struct node *head,*head1,*head2;
head1=creat();
head1=order(head1);
head2=creat();
head2=order(head2);
head=join(head1,head2);
}