数据结构开课了,这道题不知道怎么错了~~关于链表问题

scy251147 2008-04-08 09:21:40
#define list_size 5
#define increment 2
#define ok 1
#define overflow -1
#define error -2
#include<iostream.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct{ int *elem;
int length;
int listsize;}sqlist;
int initlist(sqlist &L)
{
L.elem=(int *)malloc(list_size*sizeof(int));
if(!L.elem) exit(overflow);
L.length=0;
L.listsize=list_size;
return ok;
}
int listinsert(sqlist &L,int i,int e)
{int *p,*q,*newbase;
if(i<1||i>L.length+1) return error;
if(L.length>=L.listsize)
{newbase=(int *)realloc(L.elem,(L.listsize+increment)*sizeof(int));
if(!newbase)exit(overflow);
L.listsize+=increment;}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p;
*q=e;
++L.length;
return ok;}
int listdelete(sqlist &L,int i,int &e)
{int *p,*q;
if((i<1)||(i>L.length))return error;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p) *(p-1)=*p;
--L.length;
return ok;
}
void printlist(sqlist &L)
{int i;
cout<<"***************************\n";
cout<<"The element of the sqlist are:"<<endl;
for(i=0;i<L.length;i++);
cout<<"***************************\n";
return;
}
void main()
{ int i,j,e,m,*n;
sqlist L1;
initlist(&L1);
cout<<"Please output the length of the sqlist:\n";
cin>>L1.length;
cout<<"Please insert the element of the sqlist:\n";
for(i=0;i<L1.length;i++)
cin>>L1.elem[i];
cout<<"Please insert the value of j and e :\n";
listinsert(&L1,j,e);
cin>>j;
cin>>e;
cout<<"please delete the value of m and n:\n";
listdelete(&L1,m,&n);
cin>>m;
cin>>n;
printlist(&L1);
}


程序就是这样,但是我在编译的时候,一直提示出错,哪位能看下什么地方出错了么?谢了先
...全文
74 8 打赏 收藏 举报
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
scy251147 2008-04-08
  • 打赏
  • 举报
回复
谢谢了,能运行了
Kratos 2008-04-08
  • 打赏
  • 举报
回复
LZ的空格也太多了点吧~~

sqlist L1;
initlist( &L1);
把&去掉。这里是取L1的地址;
因为你定义就是int initlist(sqlist &L)
难道你要引用地址?

还有这个改为
listdelete(L1,m,*n);
cin的那个似乎也有点问题,不过都一样。
wangya55 2008-04-08
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <malloc.h>
#include <stdlib.h>


#define list_size 5
#define increment 2
#define ok 1
#define overflow -1
#define error -2


typedef struct
{ int *elem;
int length;
int listsize;
}sqlist;


int initlist(sqlist &L)
{
L.elem=(int *)malloc(list_size*sizeof(int));
if(!L.elem) exit(overflow);
L.length=0;
L.listsize=list_size;
return ok;
}


int listinsert(sqlist &L,int i,int e)
{
int *p,*q,*newbase;
if(i <1||i> L.length+1) return error;
if(L.length>=L.listsize)
{
newbase=(int *)realloc(L.elem,(L.listsize+increment)*sizeof(int));
if(!newbase)exit(overflow);
L.listsize+=increment;}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p;
*q=e;
++L.length;
return ok;
}


int listdelete(sqlist &L,int i,int &e)
{
int *p,*q;
if((i <1)||(i> L.length))return error;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p <=q;++p) *(p-1)=*p;
--L.length;
return ok;
}


void printlist(sqlist &L)
{
int i;
cout << "***************************\n ";
cout << "The element of the sqlist are: " <<endl;
for(i=0;i <L.length;i++);
cout << "***************************\n ";
return;
}


void main()
{
int i,j,e,m,n;
sqlist L1;
initlist(L1);
cout << "Please output the length of the sqlist:\n ";
cin>> L1.length;
cout << "Please insert the element of the sqlist:\n ";
for(i=0;i <L1.length;i++)
cin>> L1.elem[i];
cout << "Please insert the value of j and e :\n ";
listinsert(L1,j,e);
cin>> j;
cin>> e;
cout << "please delete the value of m and n:\n ";
listdelete(L1,m,n);
cin>> m;
cin>> n;
printlist(L1);
}

语法错误全部帮你改正好了
你自己运行一下看看
ryfdizuo 2008-04-08
  • 打赏
  • 举报
回复
#define list_size 5 
#define increment 2
#define ok 1
#define overflow -1
#define error -2
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
using namespace std;


typedef struct
{
int *elem;
int length;
int listsize;
}sqlist;

int initlist(sqlist &L)
{
L.elem=(int *)malloc(list_size*sizeof(int));
if(!L.elem) exit(overflow);
L.length=0;
L.listsize=list_size;
return ok;
}
int listinsert(sqlist &L,int i,int e)
{
int *p,*q,*newbase;
if(i <1 || i>L.length+1) return error;
if(L.length>=L.listsize)
{
newbase=(int *)realloc(L.elem,(L.listsize+increment)*sizeof(int));
if(!newbase)exit(overflow);
L.listsize+=increment;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]); p>=q; --p)
*(p+1)=*p;
*q=e;
++L.length;
return ok;
}
int listdelete(sqlist &L,int i,int &e)
{
int *p,*q;
if((i <1) || (i>L.length))return error;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p <=q;++p)
*(p-1)=*p;
--L.length;
return ok;
}

void printlist(sqlist &L)
{
int i;
cout <<"***************************\n";
cout <<"The element of the sqlist are:" <<endl;
for(i=0;i <L.length;i++)
cout<<L.elem[i]<<" "; //链表里面的输出元素;
cout <<"\n***************************\n";
return;
}

void main()
{
int i,j,e,m,n; //n的类型是什么,
sqlist L1;
initlist(L1);
cout <<"Please output the length of the sqlist:\n";
cin>>L1.length;
cout <<"Please insert the element of the sqlist:\n";
for(i=0;i <L1.length;i++)
cin>>L1.elem[i];
cout <<"Please insert the value of j and e :\n";

cin>>j;
cin>>e;
listinsert(L1,j,e); //j表示的是下标,位置吧,
cout <<"please delete the value of m and n:\n";

cin>>m;
cin>>n; //m一样表示的是下标,位置吧,
listdelete(L1, m, n);
//printlist(&L1);
printlist(L1);
}
Kratos 2008-04-08
  • 打赏
  • 举报
回复
我的VS2005,要把#define放到#include的后面

其次的都是语法错误。
scy251147 2008-04-08
  • 打赏
  • 举报
回复
呵呵·~~~不好意思,重新发来~~

#include<iostream.h>
#include<malloc.h>
#include<stdlib.h>


#define list_size 5
#define increment 2
#define ok 1
#define overflow -1
#define error -2



typedef struct
{ int *elem;
int length;
int listsize;
}sqlist;



int initlist(sqlist &L)
{
L.elem=(int *)malloc(list_size*sizeof(int));
if(!L.elem) exit(overflow);
L.length=0;
L.listsize=list_size;
return ok;
}



int listinsert(sqlist &L,int i,int e)
{
int *p,*q,*newbase;
if(i<1||i>L.length+1) return error;
if(L.length>=L.listsize)
{
newbase=(int *)realloc(L.elem,(L.listsize+increment)*sizeof(int));
if(!newbase)exit(overflow);
L.listsize+=increment;}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p;
*q=e;
++L.length;
return ok;
}



int listdelete(sqlist &L,int i,int &e)
{
int *p,*q;
if((i<1)||(i>L.length))return error;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p) *(p-1)=*p;
--L.length;
return ok;
}



void printlist(sqlist &L)
{
int i;
cout<<"***************************\n";
cout<<"The element of the sqlist are:"<<endl;
for(i=0;i<L.length;i++);
cout<<"***************************\n";
return;
}



void main()
{
int i,j,e,m,*n;
sqlist L1;
initlist(&L1);
cout<<"Please output the length of the sqlist:\n";
cin>>L1.length;
cout<<"Please insert the element of the sqlist:\n";
for(i=0;i<L1.length;i++)
cin>>L1.elem[i];
cout<<"Please insert the value of j and e :\n";
listinsert(&L1,j,e);
cin>>j;
cin>>e;
cout<<"please delete the value of m and n:\n";
listdelete(&L1,m,&n);
cin>>m;
cin>>n;
printlist(&L1);
}
ryfdizuo 2008-04-08
  • 打赏
  • 举报
回复
void  main() 
{
int i,j,e,m,*n;
sqlist L1;
//initlist(&L1);
initlist(L1); //函数传参有问题,引用,
cout <<"Please output the length of the sqlist:\n";
cin>>L1.length;
cout <<"Please insert the element of the sqlist:\n";
for(i=0;i <L1.length;i++)
cin>>L1.elem[i];
cout <<"Please insert the value of j and e :\n";
//istinsert(&L1,j,e);
listinsert(L1,j,e);
cin>>j;
cin>>e;
cout <<"please delete the value of m and n:\n";
//istdelete(&L1,m,&n);
listdelete(L1,m,n);
cin>>m;
cin>>n;
//printlist(&L1);
printlist(L1);
}
独孤过儿 2008-04-08
  • 打赏
  • 举报
回复
大哥,你要打印啊?用不着这么省纸张吧...

我视力不好,楼下的来解答吧...
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-04-08 09:21
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下