#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node;
node *Creat(int n)//顺序创建
{
node *head, *tail, *q, *p;
int i;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
for(i=0; i<n; i++)
{
p = (struct node *)malloc(sizeof(struct node));
scanf("%d", &p->data);
p->next = NULL;
tail->next = p;
tail=p;
}
return head;
}
node *Creat(int n)//逆序创建
{
node *head, *q, *p;
int i;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
for(i=0; i<n; i++)
{
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = head->next;
head->next = p;
}
return head;
}
node *Delete(struct node *head)//删除
{
struct node *p, *q1, *q2;
p=head->next;
while(p)
{
q1=p;
q2=q1->next;
while(q2)
{
if(q2->data==p->data)
{
q1->next = q2->next;
q2=q2->next;
}
else
{
q2=q2->next;
q1=q1->next;
}
}
p=p->next;
}
return head;
}
node *reverse(struct node *head)//逆置
{
node *p, *q;
p=head->next;
head->next=NULL;
q=p->next;//q用来保存余下的链表使其不丢失
while(p)
{
p->next = head->next;
head->next = p;
p=q;
if(q)//判断是否为最后一个结点
q=q->next;
}
}
node * merge(struct node *head1, struct node *head2)//归并
{
node *p1, *p2, *tail;
p1 = head1->next;
p2 = head2->next;
tail = head1;
free(head2);
while(p1&&p2)
{
if(p1->data<p2->data)
{
tail->next = p1;
tail = p1;
p1 = p1->next;
tail->next = NULL;
}
else
{
tail->next = p2;
tail = p2;
p2 = p2->next;
tail->next = NULL;
}
}
if(p1)
tail->next=p1;
if(p2)
tail->next=p2;
return (head1);
}
node * split(struct node * head1)//拆分
{
node *q, *p, *head2;
p=head1->next;
q=p->next;
head1->next=NULL;
head2 = (struct node *)malloc(sizeof(struct node));
head2->next=NULL;
while(p)
{
if(p->data>0)
{
p->next = head1->next;
head1->next = p;
}
else
{
p->next = head2->next;
head2->next = p;
}
p=q;
if(q) q=q->next;
}
return head2;
}
node *Display(struct node *head)//输出
{
struct node *q;
q=head->next;
while(q)
{
if(q->next!=NULL)
printf("%d ", q->data);
else
printf("%d\n", q->data);
q=q->next;
}
}
主函数调用
int n;
scanf("%d", &n);
node *head1 = Creat(n);
node *head1 = Creat(n);
Delete(head1);
reverse(head1);
head1=merge(head1, head2);
head2=split(head1);
Display(head1, head2);