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

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);

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
• 打赏
• 举报

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);
} ``````

• 打赏
• 举报

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-04-08 09:21

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下