C语言题目6

//用C编写程序:输入一个字符串,将其中的数字字符移动到非数字字符之后,
//并保持数字字符和非数字字符输入时的顺序。
#include"stdio.h"
#include"stdlib.h"

typedef struct node{
  char x;
  struct node *next;
}llnode;

int main()
{
  char c;
  llnode *head = (llnode *)malloc(sizeof(llnode));
  llnode *p;
  llnode *n;
  head->next = NULL;
  //由于不知道用户会输入多少字符,所以要用链表
  printf("请输入一个字符串,输入完成后敲击回车:");
  while(1)
    {
      c = getchar();
      if(c == '\n')
	{
	  break;
	}
      n = (llnode *)malloc(sizeof(llnode));
      n->x = c;
      n->next = NULL;
      if(head->next == NULL)
	{
	  head->next = n;
	}
      else
	{
	  p = head->next;
	  if((n->x)>='0' && (n->x)<='9')
	    {
	      while(p->next != NULL)
		{
		  p = p->next;
		}
	      p->next = n;
	    }
	  else
	    {
	      while(p->next != NULL)
		{
		  if((p->next->x)>='0' && (p->next->x)<='9')
		    {
		      n->next = p->next;
		      p->next = n;
		      break;
		    }
		  else
		    {
		      p = p->next;
		    }
		}
	      p->next = n;
	    }
	}
    }
  //调整完毕,下面输出
  p = head->next;
  while(p)
    {
      printf("%c", p->x);
      p = p->next;
    }
  printf("\n");
  return 1;
}