求解一个关于线性表的问题

CaiNiao20102010 2011-10-25 11:49:44
这是我写的一个线性表的简单操作:

#include<stdio.h>
#include<stdlib.h>

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef struct {
long *elem;
int length;
int listSize;
}List;

bool InitList(List L){
L.elem = (long *)malloc(LIST_INIT_SIZE * sizeof(long));
if(!L.elem){
printf("线性表创建失败!");

return false;
}
L.length = 0;
L.listSize = LIST_INIT_SIZE;
return true;
}

bool ListPosInsert(List L,int pos,int x){
if((pos < 1) || (pos > L.length + 1)){
return false;
}
if(L.length >= L.listSize){
long *newbase = (long *)realloc(L.elem,(L.listSize+LISTINCREMENT)*sizeof(long));
if(!newbase) return false;
L.elem = newbase;
L.listSize += LISTINCREMENT;
}
long *q = &(L.elem[pos -1]);
for(long *p = &(L.elem[L.length - 1]);p >= q;--p){
*(p + 1) = *p;
}
*q = x;
L.length++;
return true;

}

void ListTraverse(List L)
{
for(int i = 0;i < L.length;i++){
printf("%d ",L.elem[i]);

}

}

int _tmain(int argc, _TCHAR* argv[]){
List La = {NULL,0,0};
bool b = InitList(La);
if(b) {printf("YES");}
else {printf("NO");}
ListPosInsert(La,1,11);
ListPosInsert(La,2,22);
ListPosInsert(La,3,33);
ListTraverse(La);
system("pause");
return 0;
}

我想插入三个元素,然后遍历出来,可是运行结果却没有,只输出了一个表示初始化成功的YES。。。
...全文
64 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
愤怒的豆沙包 2011-10-26
  • 打赏
  • 举报
回复

typedef struct {
long *elem;
int length;
int listSize;
}List;


也许你开始的时候表的类型没确定好啊,如果是链式的 elem指针格式就不对了,如果是顺序的


bool InitList(List L){
L.elem = (long *)malloc(LIST_INIT_SIZE * sizeof(long));
if(!L.elem){
printf("线性表创建失败!");


额 貌似还是L.elem的格式不对,应该是简单的线性表吧,我也是刚刚学的 不对见谅哈!!
孤独小剑 2011-10-26
  • 打赏
  • 举报
回复
是了一下是C++的,结构体声明的貌似就不对
typedef struct 
{
long*elem ;
int length ;
int listSize ;
}List;
elem是结构体的元素值吧,那这样就没指向下个节点的指针了如何构成链表呢,所以接下来楼主的
int InitList(List L)
{
L.elem=(long*)malloc(LIST_INIT_SIZE*sizeof(long));
if(!L.elem)
{
printf("线性表创建失败!");

return 0 ;
}
L.length=0 ;
L.listSize=LIST_INIT_SIZE ;
return 1 ;
}
中的malloc那句没看出来楼主想做什么。
一开始的结构体定义就不对,所以楼主还是推翻重做吧。
ccnunlp 2011-10-26
  • 打赏
  • 举报
回复
参数换成引用或者指针
ccnunlp 2011-10-26
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef struct {
long *elem;
int length;
int listSize;
}List;

bool InitList(List &L){
L.elem = (long *)malloc(LIST_INIT_SIZE * sizeof(long));
if(!L.elem){
printf("线性表创建失败!");

return false;
}
L.length = 0;
L.listSize = LIST_INIT_SIZE;
return true;
}

bool ListPosInsert(List &L,int pos,int x){
if((pos < 1) || (pos > L.length + 1)){
return false;
}
if(L.length >= L.listSize){
long *newbase = (long *)realloc(L.elem,(L.listSize+LISTINCREMENT)*sizeof(long));
if(!newbase) return false;
L.elem = newbase;
L.listSize += LISTINCREMENT;
}
long *q = &(L.elem[pos -1]);
for(long *p = &(L.elem[L.length - 1]);p >= q;--p){
*(p + 1) = *p;
}
*q = x;
L.length++;
return true;

}

void ListTraverse(List &L)
{
for(int i = 0;i < L.length;i++){
printf("%d ",L.elem[i]);

}

}

int _tmain(int argc, _TCHAR* argv[]){
List La = {NULL,0,0};
bool b = InitList(La);
if(b) {printf("YES");}
else {printf("NO");}
ListPosInsert(La,1,11);
ListPosInsert(La,2,22);
ListPosInsert(La,3,33);
ListTraverse(La);
system("pause");
return 0;
}
  • 打赏
  • 举报
回复
1.函数参数用二级指针
2.参数用指针,参数返回指针
  • 打赏
  • 举报
回复
1.参数用结构体指针,函数结束返回指针
2.用二级指针做参数
编程点滴 2011-10-26
  • 打赏
  • 举报
回复
因为你那是值传递,而不是引用传递或指针传递,就像

void swap(int a, int )
{
int c;
c = a;
a = b;
b = c;
}

void swap(int &a, int b)
{
int c;
c = a;
a = b;
b = c;
}

void swap(int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
}
就像上面的三个函数,第一个是不能满足交换两个值的要求的,而后面两个可以,你可以改成引用传递或指针传递,但引用传递是用C++编译的
bool InitList(List &L)
bool ListPosInsert(List &L,int pos,int x)
这是引用传递的修改

69,373

社区成员

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

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