小q教编程
A. 编程:C语言,动态链表中的数字排序和删除问题
#include<stdio.h>
#defineMAX1024
structNode
{
intvalue;
structNode*next;
};
structNode*head=NULL;
intGetNextStart(char*st,intlast,intlen)
{
inti;
for(i=last;i<len;i++)
{
if(st[i]==',')
returni+1;
}
returnlen+1;
}
voidDoWithValue(intvalue)
{
structNode*pCur=head,*plast=NULL;
if(head==NULL)
{
//没有节点,直接插入;
pCur=(structNode*)malloc(sizeof(structNode));
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
{
//插入
structNode*pNew=(structNode*)malloc(sizeof(structNode));
pNew->value=value;
if(plast==NULL)
{
//插入位置是第一个节点
pNew->next=head;
head=pNew;
}
else
{
//插入位置不是第一个节点
pNew->next=plast->next;
plast->next=pNew;
}
}
}
}
voidShowAllList()
{
structNode*p=head;
printf("ValuesofList: ");
while(p!=NULL)
{
printf("%d",p->value);
p=p->next;
}
printf(" ");
}
voidDelAllList()
{
structNode*p=head,*plast=NULL;
while(p!=NULL)
{
plast=p;
p=p->next;
free(plast);
}
}
intmain()
{
charstInput[MAX],ch;
inti=0,curStart=0,curEnd,nextStart,len;
intvalue;
printf("Pleaseinputalineofnumbers,splitwith[,]: ");
while((ch=getchar())!=' ')
{
stInput[i++]=ch;
}
stInput[i]='