刚学的,请教一个链表问题

fengzhizi715 2004-10-24 08:23:34
#include<iostream>
#include<stdlib>
using namespace std;

typedef int elemtype;//定义数据类型
typedef struct linknode
{
elemtype data;
struct linknode *next;
}nodetype;//定义节点类型

nodetype *create()//建立单链表,由用户输入节点data域之值,以0表示输入结束
{
elemtype d;
nodetype *h=NULL,*s,*t;
int i=1;
cout<<"建立一个单链表"<<endl;
while(1)
{
cout<<"输入第"<<i<<"节点data域值:";
cin>>d;
if(d==0)
break;
}

if(i==1)
{
h=(nodetype *)malloc(sizeof (nodetype));
h->data=d;
h->next=NULL;
t=h;
}//建立第一个节点
else
{
s=(nodetype *)malloc(sizeof (nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}
i++;
}
return h;
}

void disp(nodetype *h)//输入由h指向的单链表的所有data域之值
{
nodetype *p=h;
cout<<"输入一个单链表:"<<endl<<"";
if(p==NULLL)
cout<<"空表";
while(p!=NULL)
{
cout<<p->data<<"";
p=p->next;
}
cout<<endl;
}

int len(nodetype *h)//返回单链表长度
{
int i=0;
nodetype *p=h;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}

nodetype *find(nodetype *h,int i)//返回第i个节点的指针
{
nodetype *p=h;
int j=1;
if (i>len(h)||i<=0)
return NULL;
else
{
while(p!=NULL&&j<i)//查找第j个节点,并由p指向该节点
{
j++;
p=p->next;
}
return p;
}
}

nodetype *ins(nodetype *h,int i,elemtype x)
//在单链表head中第i个节点(i>=0)之后插入一个data域为x的节点
{
nodetype *p,*s;
s=(nodetype *)malloc(sizeof (nodetype));//创建节点e
s->data=x;
s->next=NULL;
if(i==0)
{
s->next=h;
h=s;
}
else
{
p=find(h,i);
if(p!=NULL)
{
s->next=p->next;
p->next=s;
}
else
cout<<"输入的i值不正确"<<endl;
}
return h;
}

nodetype *del(nodetype *h,int i)//删除第i个节点
{
nodetype *p=h,*s;
int j=1;
if (i==1)
{
h=h->next;
free(p);
}
else
{
p=find(h,i-1);
if(p!=NULL&&p->next!=NULL)
{
s=p->next;
p->next=s->next;
free(s);
}
else
cout<<"输入的i值不正确"<<endl;
}
return h;
}

void dispose(nodetype *h)//释放单链表的所有节点空间
{
nodetype *pa=h,*pb;
if(pa!=NULL)
{
pb=pa->next;
if(pb==NULL)//只有一个节点的情况
free(pa);
else//两个以上节点的情况
{
while(pb!=NULL)
{
free(pa);
pa=pb;
pb=pb->next;
}
free(pa);
}
}
}




编译得到的结果如下:
e:\c++源代码\slink.cpp(2) : fatal error C1083: Cannot open include file: 'stdlib': No such file or directory
Error executing cl.exe.

请问那是错在哪里了?谢谢
...全文
168 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengzhizi715 2004-10-28
  • 打赏
  • 举报
回复
非常谢谢楼上的几位
我再回去研究研究。想通了马上结帖
newegg2002 2004-10-27
  • 打赏
  • 举报
回复
我写了一个简单的测试函数..结果如下:
int main()
{
nodetype *List;
List=create();
disp(List);
cout<<"The length of the List is:"<<len(List)<<endl;
nodetype *pn=find(List,2);
cout<<"The second element is:"<<pn->data<<endl;
List=ins(List,2,43);
disp(List);
dispose(List);
system("pause");

}

测试:
建立一个单链表
输入第1节点data域值:1
输入第2节点data域值:2
输入第3节点data域值:3
输入第4节点data域值:4
输入第5节点data域值:5
输入第6节点data域值:6
输入第7节点data域值:7
输入第8节点data域值:0
输入一个单链表:
1 2 3 4 5 6 7
The length of the List is:7
The second element is:2
输入一个单链表:
1 2 43 3 4 5 6 7
请按任意键继续 . . .
newegg2002 2004-10-27
  • 打赏
  • 举报
回复
其它的代码基本没有什么问题,,我改过之后的全部代码:

#define NULL 0
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

typedef int elemtype;//定义数据类型
typedef struct linknode
{
elemtype data;
struct linknode *next;
}nodetype;//定义节点类型

nodetype *create()//建立单链表,由用户输入节点data域之值,以0表示输入结束
{
elemtype d;
nodetype *h=NULL,*s,*t;
int i=1;
cout<<"建立一个单链表"<<endl;
while(1)
{
cout<<"输入第"<<i<<"节点data域值:";
cin>>d;
if(d==0) //不将0作为链表数据
break;

if(i==1) //这两个if-else语句应该在while循环之内,,
{
h=(nodetype *)malloc(sizeof (nodetype));
h->data=d;
h->next=NULL;
t=h;
}//建立第一个节点
else
{ s=(nodetype *)malloc(sizeof (nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}
i++;

}

return h;
}


void disp(nodetype *h)//输入由h指向的单链表的所有data域之值
{
nodetype *p=h;
cout<<"输入一个单链表:"<<endl<<"";
if(p==NULL)
cout<<"空表";
while(p!=NULL)
{
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}

int len(nodetype *h)//返回单链表长度
{
int i=0;
nodetype *p=h;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}

nodetype *find(nodetype *h,int i)//返回第i个节点的指针
{
nodetype *p=h;
int j=1;
if (i>len(h)||i<=0)
return NULL;
else
{
while(p!=NULL&&j<i)//查找第j个节点,并由p指向该节点
{
j++;
p=p->next;
}
return p;
}
}
//////////////////////////////////////////////////////////////
nodetype *ins(nodetype *h,int i,elemtype x)
//在单链表head中第i个节点(i>=0)之后插入一个data域为x的节点
{
nodetype *p,*s;
s=(nodetype *)malloc(sizeof (nodetype));//创建节点e
s->data=x;
s->next=NULL;
if(i==0)
{
s->next=h;
h=s;
}
else
{
p=find(h,i);
if(p!=NULL)
{
s->next=p->next;
p->next=s;
}
else
cout<<"输入的i值不正确"<<endl;
}
return h;
}

nodetype *del(nodetype *h,int i)//删除第i个节点
{
nodetype *p=h,*s;
int j=1;
if (i==1)
{
h=h->next;
free(p);
}
else
{
p=find(h,i-1);
if(p!=NULL&&p->next!=NULL)
{
s=p->next;
p->next=s->next;
free(s);
}
else
cout<<"输入的i值不正确"<<endl;
}
return h;
}

void dispose(nodetype *h)//释放单链表的所有节点空间
{
nodetype *pa=h,*pb;
if(pa!=NULL)
{
pb=pa->next;
if(pb==NULL)//只有一个节点的情况
free(pa);
else//两个以上节点的情况
{
while(pb!=NULL)
{
free(pa);
pa=pb;
pb=pb->next;
}
free(pa);
}
}
}
newegg2002 2004-10-25
  • 打赏
  • 举报
回复
1.#include<cstdlib> //包含头文件有误..
2.创建链表的函数我觉得应该是这样的:
nodetype *create()//建立单链表,由用户输入节点data域之值,以0表示输入结束
{
elemtype d;
nodetype *h=NULL,*s,*t;
int i=1;
cout<<"建立一个单链表"<<endl;
while(1)
{
cout<<"输入第"<<i<<"节点data域值:";
cin>>d;
if(d==0) //不将0作为链表数据
break;

if(i==1) //这两个if-else语句应该在while循环之内,,
{
h=(nodetype *)malloc(sizeof (nodetype));
h->data=d;
h->next=NULL;
t=h;
}//建立第一个节点
else
{ s=(nodetype *)malloc(sizeof (nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}

}
i++;
return h;
}

其它的顾不上看了:)
goodluckyxl 2004-10-25
  • 打赏
  • 举报
回复
是name space
大家使用.h
要么都用命名头
daylove 2004-10-25
  • 打赏
  • 举报
回复
错误提示包含文件找不到,是你写错了
应该这样:<stdlib.h>或者是<cstdlib>
o1n 2004-10-24
  • 打赏
  • 举报
回复
#include<iostream.h>
//using namespace std;
#include<stdlib.h>
typedef int elemtype;//定义数据类型
typedef struct linknode
{
elemtype data;
struct linknode *next;
}nodetype;//定义节点类型

nodetype *create()//建立单链表,由用户输入节点data域之值,以0表示输入结束
{
elemtype d;
nodetype *h=NULL,*s,*t;
int i=1;
cout<<"建立一个单链表"<<endl;
while(1)
{
cout<<"输入第"<<i<<"节点data域值:";
cin>>d;
if(d==0)
break;
}

if(i==1)
{
h=(nodetype *)malloc(sizeof (nodetype));
h->data=d;
h->next=NULL;
t=h;
}//建立第一个节点
else
{
s=(nodetype *)malloc(sizeof (nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}
i++;

return h;
}

void disp(nodetype *h)//输入由h指向的单链表的所有data域之值
{
nodetype *p=h;
cout<<"输入一个单链表:"<<endl<<"";
if(p==NULL)
cout<<"空表";
while(p!=NULL)
{
cout<<p->data<<"";
p=p->next;
}
cout<<endl;
}

int len(nodetype *h)//返回单链表长度
{
int i=0;
nodetype *p=h;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}

nodetype *find(nodetype *h,int i)//返回第i个节点的指针
{
nodetype *p=h;
int j=1;
if (i>len(h)||i<=0)
return NULL;
else
{
while(p!=NULL&&j<i)//查找第j个节点,并由p指向该节点
{
j++;
p=p->next;
}
return p;
}
}

nodetype *ins(nodetype *h,int i,elemtype x)
//在单链表head中第i个节点(i>=0)之后插入一个data域为x的节点
{
nodetype *p,*s;
s=(nodetype *)malloc(sizeof (nodetype));//创建节点e
s->data=x;
s->next=NULL;
if(i==0)
{
s->next=h;
h=s;
}
else
{
p=find(h,i);
if(p!=NULL)
{
s->next=p->next;
p->next=s;
}
else
cout<<"输入的i值不正确"<<endl;
}
return h;
}

nodetype *del(nodetype *h,int i)//删除第i个节点
{
nodetype *p=h,*s;
int j=1;
if (i==1)
{
h=h->next;
free(p);
}
else
{
p=find(h,i-1);
if(p!=NULL&&p->next!=NULL)
{
s=p->next;
p->next=s->next;
free(s);
}
else
cout<<"输入的i值不正确"<<endl;
}
return h;
}

void dispose(nodetype *h)//释放单链表的所有节点空间
{
nodetype *pa=h,*pb;
if(pa!=NULL)
{
pb=pa->next;
if(pb==NULL)//只有一个节点的情况
free(pa);
else//两个以上节点的情况
{
while(pb!=NULL)
{
free(pa);
pa=pb;
pb=pb->next;
}
free(pa);
}
}
}
dot99 2004-10-24
  • 打赏
  • 举报
回复
while(1)
{
cout<<"输入第"<<i<<"节点data域值:";
cin>>d;
if(d==0)
break;
}
没仔细看,不过晃了一下,这里似乎逻辑有问题
出来的时候d == 0
dot99 2004-10-24
  • 打赏
  • 举报
回复
#include<cstdlib>
李马 2004-10-24
  • 打赏
  • 举报
回复
#include<stdlib> ---> <cstdlib> or <stdlib.h>

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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