c语言单链表问题,请问这个那里错了,本人菜鸟,下面是程序
#include<stdio.h>#include<malloc.h>typedefstructNode{intdata;structNode*next;}Lnode,*...
#include<stdio.h>
#include<malloc.h>
typedef struct Node
{ int data;
struct Node *next;
} Lnode , *LinkList;
LinkList TCreatList()
{ Lnode *H,*T,*P; int x;
H=(Lnode *)malloc(sizeof(Lnode));
H->next=NULL; T=H;
scanf("%d",&x);
while( x!=0)
{ P= (Lnode *)malloc(sizeof(Lnode));
P->data=x; P->next=NULL;
T->next=P; T=P;
scanf("%d",&x);
} return H;
}
void PrintfList(LinkList H)
{ Lnode *p=H ;
while(p!=NULL)
{printf("%d",p->data);
p=p->next;
}
}
int LengthList( LinkList H)
{ Lnode *p=H; int i=0;
while (p->next!=NULL)
{ i++; p=p->next;}
return i;
printf ("链表的长度是%d/n",i);
}
Lnode *SearchX(LinkList H,int x)
{ Lnode *p=H->next;
while(p!=NULL && p->data != x)
p=p->next;
return p;
}
void InsertList(Lnode *H,int a,int x)
{printf("请输入要插入的数据:\n");
Lnode *s, *q = H ;
s=(Lnode *)malloc(sizeof(Lnode));
s->data=x; s->next=NULL;
while(q->next!=NULL&&q->next->data!=a)
q=q->next;
s->next=q->next;
q->next=s;}
int main()
{LinkList H;
H=TCreatList();
PrintfList(H);
LengthList(H);
SearchX(H);
InsertList(H);
return 0;} 展开
#include<malloc.h>
typedef struct Node
{ int data;
struct Node *next;
} Lnode , *LinkList;
LinkList TCreatList()
{ Lnode *H,*T,*P; int x;
H=(Lnode *)malloc(sizeof(Lnode));
H->next=NULL; T=H;
scanf("%d",&x);
while( x!=0)
{ P= (Lnode *)malloc(sizeof(Lnode));
P->data=x; P->next=NULL;
T->next=P; T=P;
scanf("%d",&x);
} return H;
}
void PrintfList(LinkList H)
{ Lnode *p=H ;
while(p!=NULL)
{printf("%d",p->data);
p=p->next;
}
}
int LengthList( LinkList H)
{ Lnode *p=H; int i=0;
while (p->next!=NULL)
{ i++; p=p->next;}
return i;
printf ("链表的长度是%d/n",i);
}
Lnode *SearchX(LinkList H,int x)
{ Lnode *p=H->next;
while(p!=NULL && p->data != x)
p=p->next;
return p;
}
void InsertList(Lnode *H,int a,int x)
{printf("请输入要插入的数据:\n");
Lnode *s, *q = H ;
s=(Lnode *)malloc(sizeof(Lnode));
s->data=x; s->next=NULL;
while(q->next!=NULL&&q->next->data!=a)
q=q->next;
s->next=q->next;
q->next=s;}
int main()
{LinkList H;
H=TCreatList();
PrintfList(H);
LengthList(H);
SearchX(H);
InsertList(H);
return 0;} 展开
2个回答
展开全部
不知道你有没有调这个程序,很明显的错误是调用SearchX(H);和InsertList(H);这2个函数时,参数少了,你的实现里SearchX有2个参数,InsertList有3个参数,那你调用肯定要传递足够的参数,其实也容易理解:你查找时(即Search时)传递一个链表头,自然也需要一个查找的数据;插入时(即insert时)需要一个表头,一个数据和一个插入位置。
之前写的一个程序,带测试信息,给你参考:我这里是用的全局节点变量,所有在插入时只传了2个参数,一个target目标(不是位置,换成指定位置更简单),一个数据:
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode
{
int data;
struct LinkNode *next;
}link,*plink;
plink head = NULL;
void addfront()
{
int value;
plink s,p;
printf("please enter the value\n");
scanf("%d",&value);
s = malloc(sizeof(link));
if(s==NULL)
{
printf("memory error\n");
return;
}
s->data = value;
if(head == NULL)
{
head = s;
s->next = NULL;
}else{
p = head;
head = s;
s->next = p;
}
}
void addtail()
{
int value;
plink s,p;
s = malloc(sizeof(link));
if(s==NULL)
{
printf("memory error\n");
return;
}
printf("please enter the value\n");
scanf("%d",&value);
s->data = value;
if(head == NULL)
{
head = s;
s->next = NULL;
}else{
p = head;
while(p->next != NULL)
{
p = p->next;
}
p->next = s;
s->next = NULL;
}
}
void addAll()
{
int count,i;
printf("please enter the count\n");
scanf("%d",&count);
for(i=0;i<count;i++)
{
addtail();
}
}
void display()
{
plink p;
p = head;
while(p)
{
printf("%3d",p->data);
p = p->next;
}
printf("\n");
}
plink search(int data)
{
int i = 0;
plink p;
if(head == NULL)
{
printf("single link is empty\n");
return NULL;
}
p = head;
while(p != NULL)
{
i++;
if(p->data == data)
{
printf("the number %d is at %d\n",data,i);
return p;
}else
{
p = p->next;
}
}
return NULL;
}
void insert(int target,int insertvalue)
{
plink p, q, s;
if(head == NULL)
{
printf("single link is empty\n");
return ;
}
s = (plink)malloc(sizeof(link));
if(s == NULL)
{
printf("memory error!\n");
return ;
}
s->data = insertvalue;
if(head->data == target)
{
s->next = head;
head = s;
return;
}else
{
p = q = head;
while(p != NULL)
{
if(p->data == target)
{
q->next = s;
s->next = p;
printf("insert successful\n");
return ;
}else
{
q = p;
p = p->next;
}
}
}
printf("insert failed!\n");
}
void delete(int data)
{
plink p, q;
if(head == NULL)
{
printf("single link is empty\n");
return ;
}
if(head->data == data)
{
p = head->next;
free(head);
head = p;
printf("delete successful\n");
return;
}else
{
p = q = head;
while(p != NULL)
{
if(p->data == data)
{
q->next = p->next;
free(p);
printf("delete successful\n");
return ;
}else
{
q = p;
p = p->next;
}
}
}
printf("delete failed!\n");
}
void deleteAll(int data)
{
plink p, q;
if(head == NULL)
{
printf("single link is empty\n");
return ;
}
p = q = head;
while(p != NULL)
{
if(head->data == data)
{
p = head->next;
free(head);
head = p;
}else if(p->data == data)
{
q->next = p->next;
free(p);
p = q->next;
}else
{
q = p;
p = p->next;
}
}
}
int main(void)
{
int operation;
printf("please enter your operation\n");
do{
printf("\t1.add a number at the first\n");
printf("\t2.add a number at the last\n");
printf("\t3.add multi number\n");
printf("\t4.display the singlelink\n");
printf("\t5.search a number\n");
printf("\t6.insert the insertvalue at the target\n");
printf("\t7.delete the value\n");
printf("\t8.delete all of the value\n");
printf("\t9.quit\n");
scanf("%d",&operation);
switch(operation)
{
case 1:addfront();break;
case 2:addtail();break;
case 3:addAll();break;
case 4:display();break;
case 5:
{int value;
printf("enter the search number\n");
scanf("%d",&value);
search(value);break;}
case 6:
{int target,insertvalue;
printf("enter the target and insertvalue \n");
scanf("%d%d",&target,&insertvalue);
insert(target,insertvalue);}
break;
case 7:
{int value;
printf("enter the value to delete \n");
scanf("%d",&value);
delete(value);}
break;
case 8:
{int value;
printf("enter the value to delete all \n");
scanf("%d",&value);
deleteAll(value);}
break;
case 9:return;
default:printf("error input\n");
}
}while(1);
}
之前写的一个程序,带测试信息,给你参考:我这里是用的全局节点变量,所有在插入时只传了2个参数,一个target目标(不是位置,换成指定位置更简单),一个数据:
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode
{
int data;
struct LinkNode *next;
}link,*plink;
plink head = NULL;
void addfront()
{
int value;
plink s,p;
printf("please enter the value\n");
scanf("%d",&value);
s = malloc(sizeof(link));
if(s==NULL)
{
printf("memory error\n");
return;
}
s->data = value;
if(head == NULL)
{
head = s;
s->next = NULL;
}else{
p = head;
head = s;
s->next = p;
}
}
void addtail()
{
int value;
plink s,p;
s = malloc(sizeof(link));
if(s==NULL)
{
printf("memory error\n");
return;
}
printf("please enter the value\n");
scanf("%d",&value);
s->data = value;
if(head == NULL)
{
head = s;
s->next = NULL;
}else{
p = head;
while(p->next != NULL)
{
p = p->next;
}
p->next = s;
s->next = NULL;
}
}
void addAll()
{
int count,i;
printf("please enter the count\n");
scanf("%d",&count);
for(i=0;i<count;i++)
{
addtail();
}
}
void display()
{
plink p;
p = head;
while(p)
{
printf("%3d",p->data);
p = p->next;
}
printf("\n");
}
plink search(int data)
{
int i = 0;
plink p;
if(head == NULL)
{
printf("single link is empty\n");
return NULL;
}
p = head;
while(p != NULL)
{
i++;
if(p->data == data)
{
printf("the number %d is at %d\n",data,i);
return p;
}else
{
p = p->next;
}
}
return NULL;
}
void insert(int target,int insertvalue)
{
plink p, q, s;
if(head == NULL)
{
printf("single link is empty\n");
return ;
}
s = (plink)malloc(sizeof(link));
if(s == NULL)
{
printf("memory error!\n");
return ;
}
s->data = insertvalue;
if(head->data == target)
{
s->next = head;
head = s;
return;
}else
{
p = q = head;
while(p != NULL)
{
if(p->data == target)
{
q->next = s;
s->next = p;
printf("insert successful\n");
return ;
}else
{
q = p;
p = p->next;
}
}
}
printf("insert failed!\n");
}
void delete(int data)
{
plink p, q;
if(head == NULL)
{
printf("single link is empty\n");
return ;
}
if(head->data == data)
{
p = head->next;
free(head);
head = p;
printf("delete successful\n");
return;
}else
{
p = q = head;
while(p != NULL)
{
if(p->data == data)
{
q->next = p->next;
free(p);
printf("delete successful\n");
return ;
}else
{
q = p;
p = p->next;
}
}
}
printf("delete failed!\n");
}
void deleteAll(int data)
{
plink p, q;
if(head == NULL)
{
printf("single link is empty\n");
return ;
}
p = q = head;
while(p != NULL)
{
if(head->data == data)
{
p = head->next;
free(head);
head = p;
}else if(p->data == data)
{
q->next = p->next;
free(p);
p = q->next;
}else
{
q = p;
p = p->next;
}
}
}
int main(void)
{
int operation;
printf("please enter your operation\n");
do{
printf("\t1.add a number at the first\n");
printf("\t2.add a number at the last\n");
printf("\t3.add multi number\n");
printf("\t4.display the singlelink\n");
printf("\t5.search a number\n");
printf("\t6.insert the insertvalue at the target\n");
printf("\t7.delete the value\n");
printf("\t8.delete all of the value\n");
printf("\t9.quit\n");
scanf("%d",&operation);
switch(operation)
{
case 1:addfront();break;
case 2:addtail();break;
case 3:addAll();break;
case 4:display();break;
case 5:
{int value;
printf("enter the search number\n");
scanf("%d",&value);
search(value);break;}
case 6:
{int target,insertvalue;
printf("enter the target and insertvalue \n");
scanf("%d%d",&target,&insertvalue);
insert(target,insertvalue);}
break;
case 7:
{int value;
printf("enter the value to delete \n");
scanf("%d",&value);
delete(value);}
break;
case 8:
{int value;
printf("enter the value to delete all \n");
scanf("%d",&value);
deleteAll(value);}
break;
case 9:return;
default:printf("error input\n");
}
}while(1);
}
展开全部
写这段程序的人编程习惯不是很好,希望慢慢改进;在使用指针之前,必须初始化,不然会出现段错误。链表扫描输入建议用getchar()函数,可以正确的处理掉回车符。
按照scanf()输入,建立链表我修改为:
LinkList TCreatList()
{ Lnode *H,*T,*P=NULL; int x;
scanf("%d",&x);
while( 1)
{
P= (Lnode *)malloc(sizeof(Lnode));
P->data=x;
if(H==NULL){
H=P;
}
else
T->next=P;
T=P;
}
else if(x==0)
break;
return H;
}
还有,仔细检查插入函数,a和x的实际意义是什么,我看不懂;
按照scanf()输入,建立链表我修改为:
LinkList TCreatList()
{ Lnode *H,*T,*P=NULL; int x;
scanf("%d",&x);
while( 1)
{
P= (Lnode *)malloc(sizeof(Lnode));
P->data=x;
if(H==NULL){
H=P;
}
else
T->next=P;
T=P;
}
else if(x==0)
break;
return H;
}
还有,仔细检查插入函数,a和x的实际意义是什么,我看不懂;
参考资料: h=p
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询