用c写的一个单链表程序,在VC6.0环境中运行总是出错!

grn1985bear 2009-09-12 06:47:35
/*单链表的各种操作*/

# define null 0

typedef char ElemType; /* 字符型数据*/

typedef struct LNode
{
ElemType data;
struct LNode *next;
};

setnull(struct LNode **p);
int length (struct LNode **p);
ElemType get(struct LNode **p,int i);
void insert(struct LNode **p,ElemType x,int i);
int delete(struct LNode **p,int i);
void display(struct LNode **p);

main()
{
struct LNode *head,*q; /*定义静态变量*/
int select,x1,x2,x3,x4;
int i,n;
int m,g;
char e,y;

head=setnull(&head); /*建议链表并设置为空表*/
printf("请输入数据长度: ");
scanf("%d",&n);
for(i=1;i<n;i++);
{
printf("将数据插入到单链表中: ");
scanf("%d",&y);
insert(&head,y,i);} /*插入数据到链表*/
display(&head); /*显示链表所有数据*/

printf("select 1 求长度 length()\n");
printf("select 2 取结点 get()\n");
printf("select 3 求值查找 locate()\n");
printf("select 4 删除结点 delete()\n");
printf("input your select: ");
scanf("%d",&select);
switch(select)
{
case 1:
{
x1=length(&head);
printf("输出单链表的长度%d ",x1);
display(&head);
}break;

case 2:
{
printf("请输入要取得结点: ");
scanf("%d",&m);
x2=get(&head,m);
printf(x2);
display(&head);
}break;

case 3:
{
printf("请输入要查找的数据: ");
scanf("%d",&e);
x3=locate(&head,e);
printf(x3);
display(&head);
}break;

case 4:
{
printf("请输入要删除的结点: ");
scanf("%d",&g);
x4=delete(&head,g);
printf(x4);
display(&head);
}break;
}
}
}


setnull(struct LNode **p)
{
*p=null;
}

int length (struct LNode **p)
{
int n=0;
struct LNode *q=*p;
while (q!=null)
{
n++;
q=q->next;
}
return(n);
}

ElemType get(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p;
while (j<i&&q!=null)
{
q=q->next;
j++;
}
if(q!=null)
return(q->data);
else
printf("位置参数不正确!\n");
}

int locate(struct LNode **p,ElemType x)
{
int n=0;
struct LNode *q=*p;
while (q!=null&&q->data!=x)
{
q=q->next;
n++;
}
if(q==null)
return(-1);
else
return(n+1);
}

void insert(struct LNode **p,ElemType x,int i)
{
int j=1;
struct LNode *s,*q;
s=(struct LNode *)malloc(sizeof(struct LNode));
s->data=x;
q=*p;
if(i==1)
{
s->next=q;
p=s;
}
else
{
while(j<i-1&&q->next!=null)
{
q=q->next;
j++;
}
if(j==i-1)
{
s->next=q->next;
q->next=s;
}
else
printf("位置参数不正确!\n");
}
}

int delete(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p,*t;
if(i==1)
{
t=q;
*p=q->next;
}
else
{
while(j<i-1&&q->next!=null)
{
q=q->next;
j++;
}
if(q->next!=null&&j==i-1)
{
t=q->next;
q->next=t->next;
}
else
printf("位置参数不正确!\n");
}
if(t=null)
free(t);
}

void display(struct LNode **p)
{
struct LNode *q;
q=*p;
printf("单链表显示: ");
if(q==null)
printf("链表为空!");
else if (q->next==null)
printf("%c\n",q->data);
else
{
while(q->next!=null)
{
printf("%c->",q->data);
q=q->next;
}
printf("%c",q->data);
}
printf("\n");
}

--------------------Configuration: 单链表 - Win32 Debug--------------------
Compiling...
单链表.c
C:\Documents and Settings\guozi\桌面\单链表.c(28) : warning C4047: '=' : 'struct LNode *' differs in levels of indirection from 'int '
C:\Documents and Settings\guozi\桌面\单链表.c(29) : warning C4013: 'printf' undefined; assuming extern returning int
C:\Documents and Settings\guozi\桌面\单链表.c(30) : warning C4013: 'scanf' undefined; assuming extern returning int
C:\Documents and Settings\guozi\桌面\单链表.c(66) : warning C4013: 'locate' undefined; assuming extern returning int
C:\Documents and Settings\guozi\桌面\单链表.c(22) : warning C4101: 'q' : unreferenced local variable
C:\Documents and Settings\guozi\桌面\单链表.c(81) : error C2059: syntax error : '}'
C:\Documents and Settings\guozi\桌面\单链表.c(135) : warning C4013: 'malloc' undefined; assuming extern returning int
C:\Documents and Settings\guozi\桌面\单链表.c(141) : warning C4047: '=' : 'struct LNode ** ' differs in levels of indirection from 'struct LNode *'
C:\Documents and Settings\guozi\桌面\单链表.c(185) : warning C4013: 'free' undefined; assuming extern returning int
执行 cl.exe 时出错.

单链表.obj - 1 error(s), 0 warning(s)
...全文
376 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
norahsmile 2009-09-12
  • 打赏
  • 举报
回复
改了改程序。运行好着了,但还是感觉传参方面有点混乱。
改动的地方有加注释
/*单链表的各种操作*/
#include <stdio.h>
#include <malloc.h>
#include <process.h>//包含exit函数

# define null 0

typedef char ElemType; /* 字符型数据*/

struct LNode
{
ElemType data;
struct LNode *next;
};


void setnull(struct LNode **p);
int length (struct LNode **p);
ElemType get(struct LNode **p,int i);
void insert(struct LNode **p,ElemType x,int i);
int delete(struct LNode **p,int i);
void display(struct LNode **p);
int locate(struct LNode **p,ElemType x);

main()
{
struct LNode *head; /*定义静态变量*/ //不是静态变量
int select,x1,x2,x3,x4;
int i,n;
int m,g;
char e,y;

setnull(&head); /*建议链表并设置为空表*/
printf("请输入数据长度: ");
scanf("%d",&n);
for(i=1;i <=n;i++)
{
printf("将数据插入到单链表中: ");

scanf("%d",&y); //建立链表的时候,不知道传参i的用意何在
insert(&head,y,i); //结构体中数据类型是char,这里插入的值是整型吧
} /*插入数据到链表*/

display(&head); /*显示链表所有数据*/

printf("select 1 求长度 length()\n");
printf("select 2 取结点 get()\n");
printf("select 3 求值查找 locate()\n");
printf("select 4 删除结点 delete()\n");
printf("input your select: ");
scanf("%d",&select);
switch(select)
{
case 1:
{
x1=length(&head);
printf("输出单链表的长度%d ",x1);
display(&head);
}break;

case 2:
{
printf("请输入要取得结点: ");
scanf("%d",&m);
x2=get(&head,m);
printf("%d ",x2);
display(&head);
}break;

case 3:
{
printf("请输入要查找的数据: ");
scanf("%d",&e);
x3=locate(&head,e);
printf("%d ",x3);
display(&head);
}break;

case 4:
{
printf("请输入要删除的结点: ");
scanf("%d",&g);
x4=delete(&head,g);
printf("%d ",x4);
display(&head);
}break;
}
}



void setnull(struct LNode **p)
{
*p=null;
}

int length (struct LNode **p)
{
int n=0;
struct LNode *q=*p;
while (q!=null)
{
n++;
q=q->next;
}
return(n);
}

ElemType get(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p;
while (j <i&&q!=null)
{
q=q->next;
j++;
}
if(q!=null)
return(q->data);
else
printf("位置参数不正确!\n");
exit(1);//只有if时有返回值,exit(1)是位置参数不正确时退出
}

int locate(struct LNode **p,ElemType x)
{
int n=0;
struct LNode *q=*p;
while (q!=null&&q->data!=x)
{
q=q->next;
n++;
}
if(q==null)
return(-1);
else
return(n+1);
}

void insert(struct LNode **p,ElemType x,int i) //建立链表完全可以不用i的值
{
int j=1;
struct LNode *s,*q;

if(*p==null)//第一次要插入节点的值
{
*p=(struct LNode *)malloc(sizeof(struct LNode));
(*p)->data = x;
(*p)->next = null;
return ;
}

s=(struct LNode *)malloc(sizeof(struct LNode));
s->data=x;
q=*p; //此处类型的匹配


while(q->next!=null) //应该是想用尾插法
{
q=q->next;
}

s->next=q->next;
q->next=s;


// else
// printf("位置参数不正确!\n");

}

int delete(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p,*t;
if(i==1)
{
t=q;
*p=q->next;
}
else
{
while(j <i-1&&q->next!=null)
{
q=q->next;
j++;
}
if(q->next!=null&&j==i-1)
{
t=q->next;
q->next=t->next;
}
else
printf("位置参数不正确!\n");
}
if(t=null)
free(t);

return i;//返回要删除的值。其实没必要,i一直都没改变
}

void display(struct LNode **p)
{
struct LNode *q;
q=*p;
printf("单链表显示: ");
if(q==null)
printf("链表为空!");
else if (q->next==null)
printf("%d\n",q->data); //数据插入的时候是整型的,输出时改成了%d
else
{
while(q->next!=null)
{
printf("%d->",q->data);
q=q->next;
}
printf("%d",q->data);
}
printf("\n");
}

norahsmile 2009-09-12
  • 打赏
  • 举报
回复
简单总结了一下你这个程序的几处错误
1.首先在vc6.0中要加入头文件stdio.h(此头文件是包含了printf和scanf函数)
2.setnull函数无返回值,不能用head去接收,如果要用head去接收,那么返回值应该是head
另外当无返回值的时候,setnull函数的参数应当是指针的指针。
3.locate函数在未声明前使用
4.printf函数的错用,x1-x4是整型变量,printf时格式出错 printf("%d",x1)
5.main函数多了一个右括号,因此编写程序的时候注意缩进
6.malloc 函数包含在malloc.h的头文件中,要包含头文件
7.delete函数需返回一个int型的值
8.get函数需返回一个char型的值
9.在insert函数中,p=s;等号两边的类型不匹配,p是结构体指针的指针,而s只是结构体的指针

虽然给你改了改程序,是没有什么编译错误了,但是程序运行肯定还是会出问题的
mstlq 2009-09-12
  • 打赏
  • 举报
回复
只求编译通过,逻辑错误不管

/*单链表的各种操作*/
#include<stdlib.h>
#include<stdio.h>
#define null 0

typedef char ElemType; /* 字符型数据*/

typedef struct LNode
{
ElemType data;

struct LNode *next;
};

void setnull(struct LNode **p);
int length(struct LNode **p);
ElemType get(struct LNode **p,int i);
void insert(struct LNode **p,ElemType x,int i);
int delete(struct LNode **p,int i);
void display(struct LNode **p);
int locate(struct LNode **p,ElemType x);

int main()
{

struct LNode *head,*q; /*定义静态变量*/
int select,x1,x2,x3,x4;
int i,n;
int m,g;
char e,y;

setnull(&head); /*建议链表并设置为空表*/
printf("请输入数据长度: ");
scanf("%d",&n);

for (i=1;i <n;i++);

{
printf("将数据插入到单链表中: ");
scanf("%d",&y);
insert(&head,y,i);
} /*插入数据到链表*/

display(&head); /*显示链表所有数据*/

printf("select 1 求长度 length()\n");

printf("select 2 取结点 get()\n");

printf("select 3 求值查找 locate()\n");

printf("select 4 删除结点 delete()\n");

printf("input your select: ");

scanf("%d",&select);

switch (select)
{

case 1:
{
x1=length(&head);
printf("输出单链表的长度%d ",x1);
display(&head);
}

break;

case 2:
{
printf("请输入要取得结点: ");
scanf("%d",&m);
x2=get(&head,m);
printf(x2);
display(&head);
}

break;

case 3:
{
printf("请输入要查找的数据: ");
scanf("%d",&e);
x3=locate(&head,e);
printf(x3);
display(&head);
}

break;

case 4:
{
printf("请输入要删除的结点: ");
scanf("%d",&g);
x4=delete(&head,g);
printf(x4);
display(&head);
}

break;
}

}


void setnull(struct LNode **p)
{
*p=null;
}

int length(struct LNode **p)
{
int n=0;

struct LNode *q=*p;

while (q!=null)
{
n++;
q=q->next;
}

return(n);
}

ElemType get(struct LNode **p,int i)
{
int j=1;

struct LNode *q=*p;

while (j <i&&q!=null)
{
q=q->next;
j++;
}

if (q!=null)
return(q->data);
else
printf("位置参数不正确!\n");
}

int locate(struct LNode **p,ElemType x)
{
int n=0;

struct LNode *q=*p;

while (q!=null&&q->data!=x)
{
q=q->next;
n++;
}

if (q==null)
return(-1);
else
return(n+1);
}

void insert(struct LNode **p,ElemType x,int i)
{
int j=1;

struct LNode *s,*q;
s=(struct LNode *)malloc(sizeof(struct LNode));
s->data=x;
q=*p;

if (i==1)
{
s->next=q;
p=s;
}
else
{
while (j <i-1&&q->next!=null)
{
q=q->next;
j++;
}

if (j==i-1)
{
s->next=q->next;
q->next=s;
}
else
printf("位置参数不正确!\n");
}
}

int delete(struct LNode **p,int i)
{
int j=1;

struct LNode *q=*p,*t;

if (i==1)
{
t=q;
*p=q->next;
}
else
{
while (j <i-1&&q->next!=null)
{
q=q->next;
j++;
}

if (q->next!=null&&j==i-1)
{
t=q->next;
q->next=t->next;
}
else
printf("位置参数不正确!\n");
}

if (t=null)
free(t);
}

void display(struct LNode **p)
{

struct LNode *q;
q=*p;
printf("单链表显示: ");

if (q==null)
printf("链表为空!");
else if (q->next==null)
printf("%c\n",q->data);
else
{
while (q->next!=null)
{
printf("%c->",q->data);
q=q->next;
}

printf("%c",q->data);
}

printf("\n");
}
勤奋的沉沦 2009-09-12
  • 打赏
  • 举报
回复
找个翻译软件翻译下错误

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧