#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
node*next;
node*piror;
};
node*creat(int n) //creat the nodes;
{
node*head,*p1,*p2;
int i;
if(n==0)
{
printf("no node is found;\n");
head=NULL; //no node;
}
else
{
if(n==1)
{
printf("only one node exsit,please input the data to the node:");
head=(node*)malloc(sizeof(node));
scanf("%d",&head->num);
head->next=head;
head->piror=head;
}
else
{
head=p2=p1=(node*)malloc(sizeof(node));
for(i=0;i<n;++i)
{
printf("please input the data to the node:");
scanf("%d",&p1->num);
p1=(node*)malloc(sizeof(node));
p2->next=p1;
p1->piror=p2;
p2=p1;
}
p2->next=head;
head->piror=p2;
}
}
return head;
}
node*display(node*head) //print out the nodes;
{
node*p1;
p1=head;
if(p1==NULL)
{
printf("no any nodes;\n");
}
else
{
printf("Here are the information of the nodes:");
printf("\n");
do
{
printf("%d ",p1->num);
p1=p1->next;
}
while(p1->next!=head);
}
return head;
}
node*sort(node*head) //sort the nodes;
{
node*p1,*p2,*small;
int temp;
for(p1=head;p1->next!=head;p1=p1->next)
{
small=p1;//
for(p2=p1->next;p2->next!=head;p2=p2->next)
{
if(p2->num<small->num)
{
small=p2;
}
}
if(small!=p1)
{
temp=p1->num;
p1->num=small->num;
small->num=temp;
}
}
return head;
}
node *insert(node*head,int num) //
{
node*p1,*p2,*p3,*p4;;
p3=(node*)malloc(sizeof(node));//
p3->num=num;//
p1=head;//
p2=p1->next;
p4=head;
while(p4!=head)
{
p4=p4->next; //the node is tail's node;
}
// judge the head;
if(p3->num>=head->num)
{
p3->next=head;
p1->next=p3;
head->piror=p3;
p3->piror=p1;
head=p3;
return head;
//
}
do
{
if(p3->num>p1->num&&p3->num<p2->num)
{
p1->next=p3;
p3->next=p2;
p3->piror=p1;
p2->piror=p3;
return head;
}
p1=p1->next;
p2=p1->next;
}while(p2->next!=head);
// judge the tail node;
if(p3->num>=p4->num)
{
p3->piror=p1;
head->piror=p3;
p1->next=p3;
p3->next=head;
return head;
}
return head;
}
void main()
{
node*p,*p1,*p2;
printf("please input the account of the node:");
int n;
scanf("%d",&n);
printf("\n");
p=creat(n);//*** node*p;
printf("After displaying is:\n");
display(p);
printf("\n");
printf("After sorting is:");
printf("\n");
p1=sort(p); //****
display(p1);
printf("\n");
int num;//the num that will be inserted into the linkedlist;
printf("please input the data to the node that is inserted into the linkedlist:\t");
scanf("%d",&num);
p2=insert(p1,num);
printf("\n");
printf("After inserting ,the nodes are: \n");
display(p2);
printf("\n");
}
..
高手帮忙解答一下