简单有序链表的创建和查询修改
(1)建立一个单链表 21 3 15 27 11 18,并输出该链表; (2)输入序号n,查找序号为n的结点,并输出;
(3)输入值x,查找值为x的结点,并输出;
(4)插入结点: 输入序号 n和值x。在序号为n的结点后插入x,并输出该链表;
(5)删除结点: 输入序号 n,册除序号为 n 的结点,并输出该链表。
自己的代码写了不敢发出来,表示改了一天,未果,已卒

大神有空请上源代码,或者帮我改代码

程序代码:#include <stdio.h>
#include <malloc.h>
#define N 6
typedef struct data
{
int value;
struct data* next;
}tdata,*pdata;
pdata linkcre(void)
{
pdata head,pfirst,psecond;
int cr[6]={21,3,15,27,11,18};
pfirst=(pdata)malloc(sizeof(tdata));
head=pfirst;
head->value=0;
psecond=pfirst;
int i;
for(i=0;i<N;i++)
{
pfirst=(pdata)malloc(sizeof(tdata));
pfirst->value=cr[i];
pfirst->next=NULL;
psecond->next=pfirst;
psecond=pfirst;
}
return head;
}
void prnlist(pdata head)
{
pdata pfirst=head->next;
while(pfirst!=NULL)
{
printf("%d ",pfirst->value);
pfirst=pfirst->next;
}
printf("\n");
}
int main(int argc, char* argv[])
{
pdata head;
head=linkcre();
prnlist(head);
return 0;
}
程序代码:void searchlistp(pdata head,int pos)
{
pdata pfirst=head->next;
int i=0;
while(pfirst!=NULL)
{
i++;
if(i==pos) break;
pfirst=pfirst->next;
}
if(i<pos) printf("无此序列号 你输入的数值大概有些大\n");else printf("%d \n",pfirst->value);
}
int main(int argc, char* argv[])
{
pdata head;
head=linkcre();
prnlist(head);
printf("请输入待查找的序号:");
int pos=0;
scanf("%d",&pos);
searchlistp(head,pos);
return 0;
}[此贴子已经被作者于2017-4-4 16:51编辑过]

程序代码:void searchlistv(pdata head,int value)
{
bool flag=false;
pdata pfirst=head->next;
while(pfirst!=NULL)
{
if(pfirst->value==value)
{
flag=true;
break;
}
pfirst=pfirst->next;
}
if(!flag) printf("你输入的数值不存在于链表之中\n");else printf("%d \n",pfirst->value);
}
int main(int argc, char* argv[])
{
pdata head;
head=linkcre();
prnlist(head);
printf("请输入待查找的序号:");
int pos=0;
scanf("%d",&pos);
searchlistp(head,pos);
printf("请输入待查找的值:");
int value=0;
scanf("%d",&value);
searchlistv(head,value);
return 0;
}

程序代码:void inslist(pdata head,int pos,int value)
{
pdata pfirst=head->next;
int i=0;
while(pfirst!=NULL && pos>0)
{
i++;
if(i==pos) break;
pfirst=pfirst->next;
}
pdata ptmp;
if(pos==0) ptmp=head->next; else ptmp=pfirst->next;
pdata pins=(pdata)malloc(sizeof(tdata));
if(pos==0) head->next=pins;else pfirst->next=pins;
pins->value=value;
pins->next=ptmp;
}
int main(int argc, char* argv[])
{
pdata head;
head=linkcre();
prnlist(head);
inslist(head,0,56); //测试头部插入
prnlist(head);
inslist(head,5,777); //测试中间插入
prnlist(head);
inslist(head,8,99999); //测试尾部插入
prnlist(head);
printf("请输入待查找的序号:");
int pos=0;
scanf("%d",&pos);
searchlistp(head,pos);
printf("请输入待查找的值:");
int value=0;
scanf("%d",&value);
searchlistv(head,value);
return 0;
}

程序代码://看大虾们的代码 删除链表 还有一个释放节点指针的步骤
//我这里直接省了 把那块内存直接丢掉了
void dellist(pdata head,int pos)
{
if(pos==0)
{
printf("根节点不可删除\n");
return;
}
pdata pfirst=head->next;
pdata ptmp=pfirst;
int i=0;
while(pfirst!=NULL && pos>1)
{
i++;
if(i==pos) break;
ptmp=pfirst;
pfirst=pfirst->next;
}
if(pos==1) head->next=head->next->next; else ptmp->next=pfirst->next;
}
int main(int argc, char* argv[])
{
pdata head;
head=linkcre();
prnlist(head);
inslist(head,0,56); //测试头部插入
prnlist(head);
inslist(head,5,777); //测试中间插入
prnlist(head);
inslist(head,8,99999); //测试尾部插入
prnlist(head);
dellist(head,1);
prnlist(head);
printf("请输入待查找的序号:");
int pos=0;
scanf("%d",&pos);
searchlistp(head,pos);
printf("请输入待查找的值:");
int value=0;
scanf("%d",&value);
searchlistv(head,value);
return 0;
}
