首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

新手 数据结构线性表

2013-03-12 
新手求助 数据结构线性表各位前辈们 我实在是弄不懂了 也抱歉问了这么低级的问题 还请各位前辈不吝赐教 谢

新手求助 数据结构线性表
各位前辈们 我实在是弄不懂了 也抱歉问了这么低级的问题 还请各位前辈不吝赐教 谢谢了

#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define LIST_INIT_SIZE 100;
#define LISTINCREMENT 10;
#define OVERFLOW 0;
struct sqlist
{
int *elem;
int length;
int listsize;
};
struct sqlist SqList;
int InitList()
{
SqList *p;
p->elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(p->elem==0)
exit(OVERFLOW);
p->length=0;
p->listsize=LIST_INIT_SIZE;
return 1;
}
int InserList(SqList *q,int i,int x)
{
int j;
int *newspace;
if(i<1||i>q->length+1) 
return -1;
if(q->length==q->listsize)
{
newspace=(int *)realloc(q->elem,(q->listsize+LISTINCREMENT)*sizeof(int));
if(newspace==0)
exit(OVERFLOW);
q->elem=newspace;
q->listsize=LIST_INIT_SIZE+LISTINCREMENT;
}
for(j=q->length-1;j>=i-1;j--)
q->elem[j+1]=q->elem[j];
q->elem[i-1]=&x;
q->length++;
return 1;
}
void main()
{
SqList L;
int i,j;
i=InitList(&L);
printf("%d\n",i);
j=InserList(&L,1,5);
printf("%d\n%d\n",j,L->elem[0]);
}

[解决办法]

#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
SqList InitList()
{
SqList L;
L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(L.elem==0)
exit(0);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return L;
}
void InserList(SqList *p,int i,int x)
{
if(i<1
[解决办法]
i>(p->length+1))
exit(0);
if(p->length>=p->listsize)
{
int *newspace = NULL;
newspace=(int *)realloc(p->elem,(LIST_INIT_SIZE+LISTINCREMENT)*sizeof(int));
if(newspace==0)
exit(0);
p->elem = newspace;
}
for(int j=p->length;j>=i;j--)
p->elem[j]=p->elem[j-1];
p->elem[i-1]=x;
p->length++;
}
void main()
{
int i,j,n,m;
SqList L;
L=InitList();
printf("input the length\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("input number %d\n", i+1);
scanf("%d",&m);
InserList(&L, i + 1, m);
}
scanf("%d%d", &j, &m);
InserList(&L,j,m);
}

[解决办法]
为什么你定义的结果没有结构体本身的指针呢,这样怎么把两个元素连接起来。
typedef struct{
    int *elem;
    int length;
    int listsize;
    SqList* next;
}SqList;
[解决办法]

scanf里面不要加'\n'

热点排行