单链表排序

#include 
using namespace std;
typedef struct Node 
{
	int val;
	Node *next;
}LST;
LST *mycreat(int n)
{
	LST *head=NULL,*p=NULL,*sec=NULL;
	head=(LST*)malloc(sizeof(LST));
	p=head;
	while(n--)
	{
		int v;
		scanf("%d",&v);
		sec=(LST*)malloc(sizeof(LST));
		sec->val=v;
		p->next=sec;
		p=sec;
	}
	p->next=NULL;
	return head;
}
LST *mysort(LST *head)
{
	LST *p=NULL,*q=NULL,*t=NULL;
	p=head->next;
	while(p->next!=NULL)
	{
		t=p->next;
		while(t!=NULL)
		{
			if (t->valval)
			{
				int temp=t->val;
				t->val=p->val;
				p->val=temp;
			}
			t=t->next;
		}
		p=p->next;
	}
	return head;
}
int myrank(LST *head,int m)
{
	int c=0;
	LST *p=NULL;
	p=head->next;
	while(p!=NULL)
	{
		if(p->val==m) break;
		c++;
		p=p->next;
	}
	return c;
}
void myprint(LST *head)
{
	int f=0;
	LST *p=NULL;
	p=head->next;
	while(p!=NULL)
	{
		if(f==0)
			printf("%d",p->val);
		else
			printf(" %d",p->val);
		p=p->next;
		f++;
	}
				printf("\n");
				return ;
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		LST *head=NULL;
		head=mycreat(n);
		head=mysort(head);
		int no=myrank(head,m);
		printf("%d\n",no+1);
		myprint(head);
	}
	return 0;
}

你可能感兴趣的:(数据结构,单链表)