编程:C语言,动态链表中的数字排序和删除问题

编写一个c程序,要求用户从键盘输入任意个数字,该程序能从小到大的排序,如果遇到相同的数字就把这个数字删去,并输出结果。比如输入4,6,5,3,输出3 4 5 6.如果输入4,6,5,3,4,输出3 5 6。要求使用动态链表。VC++6.0环境。能成功运行的一定追分。
数据结构体应该也用上

#include<stdio.h>

#define MAX 1024

struct Node 

{

 int value;

 struct Node * next;

}; 


 

struct Node* head = NULL;

int GetNextStart(char* st, int last, int len)

{

 int i;

 for(i=last; i<len; i++)

 {

  if(st[i]==',')

   return i+1;

 }

 return len+1;

}


 

void DoWithValue( int value ) 

{

 struct Node * pCur=head, *plast=NULL;

 if(head==NULL)

 {

  //没有节点,直接插入;

  pCur = (struct Node *)malloc(sizeof(struct Node));

  pCur->value = value;

  pCur->next = NULL;

  head = pCur;

  return;

 }

 else

 {

  while(pCur != NULL && value > pCur->value)

  {

   plast = pCur;

   pCur= pCur->next;

  }

  if(pCur != NULL && value == pCur->value)

  {

   // 删除

   if(plast==NULL)

   {

    // 删除的是第一个节点

    head = pCur->next;

    free(pCur);

   }

   else

   {

    // 删除的不是第一个节点

    plast->next = pCur->next;

    free(pCur);

   }

  }

  else

  {

   // 插入

   struct Node * pNew = (struct Node *)malloc(sizeof(struct Node));

   pNew->value = value;

   if(plast==NULL)

   {

    // 插入位置是第一个节点

    pNew->next = head;

    head = pNew;

   }

   else

   {

    // 插入位置不是第一个节点

    pNew->next = plast->next;

    plast->next = pNew;

   }

  }

 }

}


 

void ShowAllList() 

{

 struct Node* p = head;

 printf("Values of List:\n");

 while(p!=NULL)

 {

  printf("%d ",p->value);

  p = p->next;

 }

 printf("\n");

}


 

void DelAllList() 

{

 struct Node* p = head, *plast=NULL;

 while(p!=NULL)

 {

  plast =p;

  p = p->next;

  free(plast);

 }

}


 

int main()

{

 char stInput[MAX],ch;

 int i=0,curStart=0,curEnd,nextStart,len;

 int value;

 printf("Please input a line of numbers, split with [,]:\n");

 while((ch=getchar())!='\n')

 {

  stInput[i++]=ch;

 }

 stInput[i]='\0';

 len = strlen(stInput);

 while(curStart<len)

 {

  nextStart = GetNextStart(stInput, curStart, len);

  curEnd = nextStart-1;

  stInput[curEnd] = '\0';

  value = atoi(stInput+curStart);

  DoWithValue(value);

  curStart = nextStart;

 }


 

 ShowAllList();

 DelAllList();

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-12-13
经典的算法 - 单链表的选择排序如下:
#包括
包括
typedef结构节点{
int数据; />结构节点下;
} *链表,节点;
链表创造(N)
{链表头,R,P;
整数X,I; ...... />头=(节点)的malloc(如sizeof(节点));
R =头;
printf的(“请输入一个数:\ n”);
(i = n时; I> 0,我 - )
{scanf函数(“%d”,&X);
P =(节点)的malloc(如sizeof(节点));
P->;数据= X;
R->下一个= P;
R = P;}
R->下一个= NULL;
返回头;
}无效输出(链表头)
{链表P;
P =头 - >下一步;
做{
printf的(“%3D”,P->数据),P = P->下
}(P);
printf的(“\ n”);
}无效派旭(链表头)
{链表P,Q,小整数温度; BR />
为(P =头>下P->下!= NULL; P = P->)
{小= P;
(Q = P- >下,Q,Q = Q->)
(Q->数据数据)
小Q;
(small! = P) {温度= P->数据;
小P->数据>数据;
小>数据=温度;}
} printf(“请输出排序:\ n”) ;
输出(头);
}无效的主要()

{链表头;
整数x的,J,N;
输出( “输入号码的数量(N):\ n”);
scanf的(“%D”,&N);
头=创造(N);
输出(输出数字:\ n“);
输出(头);
printf的(”之类的数字:\ n“;};
派旭(头);}

第二:
#包括
包括
一个节点{
int数据;
结构节点下; />} *链表,节点;
链表创造(N)
{链表头,R,P;
诠释x;
头=(节点)的malloc (如sizeof(节点));
R =头;
printf的(“请输入一个数:\ n”);
(i = n时,我> 0; I - ) /> {scanf的(“%d”,&X);
P =(节点)的malloc(如sizeof(节点));
P->数据= X;
R->下= P;
R = P;}
R->下一个= NULL;
返回头;
}链表selectsort(节点* G)
{节点* P,Q,* T * S * H??;
H =(节点)的malloc(如sizeof(节点));
H->下一步=克;
P =?
(P->下一步 - >下!= NULL)
{
为(S = P,Q = P - > Q->下!= NULL,Q = Q->)
(Q->下一步 - >数据下数据)
S = Q
(S! = Q)
{
T = S->下;
S-> T->下;
T->下一个= P->下;
P->下一步= T ;
}
P = P->下;
}
G = H->下;
免费(H);
回报克; BR />}输出(链表头)
{链表P;
P =头下;
做{
的printf(“%3D”,P->数据),P = P->下;
}(P);
printf(“请\ n”);
}无效的主要()
{链表头; ...... /> X,J,N
printf的(“请输入数数(n):\ n”);
scanf的(“%d”,&N)头创造(N);
输出(数字输出:\ n“);
输出(头);
HEAD = selectsort(头);
输出(“数字排序:\ n”);
输出(头);
}
第2个回答  2012-12-10
typedef struct node{
int id;
struct node *next;
} Pnode;

Pnode * head;

int makelist(){

int ikey;
scanf("%d",&ikey)
while(ikey!=13)
showandellist(ckey);
showlist();
}

好久没有用c编程了,就这样一个思路。
相似回答