之前的代码,最近要清理一下电脑的文件,先把代码上传上来,有时间再写。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
typedef struct node link;
typedef struct node* linkp;
linkp create()
{
linkp head=NULL,p,q;
p=(linkp)malloc(sizeof(link));
scanf("%d",&p->data);
q=p;
while(p->data)
{
if(head==NULL)
head=p;
else
q->next=p;
q=p;
p=(linkp)malloc(sizeof(link));
scanf("%d",&p->data);
}
q->next=NULL;
return head;
}
void p1(linkp h)
{
linkp p=h;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void p2(linkp h)
{
linkp p=h,q;
while(p->next)
{
q=p;p=p->next;
printf("%d ",q->data);
printf("%d ",p->data);
}
printf("\n");
}
linkp sort1(linkp h)
{
linkp h1,h2,p,q,t;
h1=h;
h2=h;
h1=h1->next;
h2->next=NULL;
while(h1)
{
t=h1;
for(p=q=h2;q&&(t->data>q->data);p=q,q=q->next);
h1=h1->next;
if(q==h2)
{
h2=t;
t->next=q;
}
else
{
t->next=q;
p->next=t;
}
}
return h2;
}
void insert(linkp *h,int x)
{
linkp p,q,s;
p=q=*h;
while(q&&(x>q->data))
{
p=q;
q=q->next;
}
s=(linkp)malloc(sizeof(link));
s->data=x;
if(q==*h)
{
s->next=q;
*h=s;
}
else
{
s->next=q;
p->next=s;
}
}
void del(linkp *h,int x)
{
linkp p,q;
p=q=*h;
while(q&&(x!=q->data))
{
p=q;
q=q->next;
}
if(q==NULL)
{
printf("不存在此数据!");
return ;
}
if(q==*h)
{
*h=(*h)->next;
free(q);
}
else
{
p->next=q->next;
free(q);
}
}
main()
{
linkp head;
head=create();
p1(head);
head=sort1(head);
p1(head);
insert(&head,222);
p1(head);
del(&head,32);
p1(head);
p2(head);
}
/*
12 3 54 6 21 7 18 10 32 8 1 0
*/