| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 692 人关注过本帖
标题:大家好,我是c++新手,正在学习《数据结构》有些不懂的地方想请教一下
只看楼主 加入收藏
spzb11b
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-9-20
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:4 
大家好,我是c++新手,正在学习《数据结构》有些不懂的地方想请教一下
创建一个顺序表,有a-z 26个小写字母。怎么填入数据?#define LIST_INIT_SIZE 26
#define LISTINCREMENT 1  //这里不知道对吗?
typedef struct{
    ElemType *elem;//这里用c++进行调试的时候,总是说ElemType没有定义,还有*elem前面没有加‘;’符号,很奇怪
    int lengtg;
    int listsize;
}SqList;


Status InitList_Sq(SqList &L)
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));//分配空间
if(!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}//InitList_Sq

  然后用自创ListInsert_Sq(SqList &L,int i,ElemType)在任意位置插入一个字母


求高手请教
搜索更多相关主题的帖子: 空间 字母 return 
2011-09-20 18:57
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:2 
你自己在前面添加typedef ElemType char ??定义啊。

我们都在路上。。。。。
2011-09-21 11:53
spzb11b
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-9-20
收藏
得分:0 
回复 2楼 hellovfp
typedef char ElemType 是把ElemType定义为char型吗?
可是指针是char型的?
2011-09-22 13:14
Toomj
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:257
专家分:1826
注 册:2011-5-17
收藏
得分:18 
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef int Status;
typedef char ElemType;

typedef struct{
    ElemType *elem;
    int length;
    int listsize;
}SqList;

Status InitList(SqList &L){
    //构造一个空的线性表L.
    L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem) exit(OVERFLOW);
    L.length=0;
    L.listsize=LIST_INIT_SIZE;
    return OK;
}

void DestoryList(SqList &L){
    //销毁线性表
    free(L.elem);
    L.elem=NULL;
    L.length=0;
    L.listsize=0;
}

void ClearList(SqList &L){
    //清空线性表
    L.length=0;
}

Status ListEmpty(SqList L){
    //判断线性表是否为空
    if(L.length==0)  return TRUE;
    else  return FALSE;
}

int ListLength(SqList L){
    //返回线性表数据个数
    return L.length;
}

Status GetList(SqList L,int i,ElemType &e){
    //用e返回线性表第i个数据元素的值
    if(i<1||i>L.length)  return ERROR;
    e=L.elem[i-1];
    return OK;
}

int LocateElem(SqList L,ElemType e,Status(*compare)(ElemType,ElemType)){
    //在顺序线性表中查找第一个值与e满足compare()的元素的位序
    //若找到则返回位序,否则返回0
    int i=1;
    ElemType *p=L.elem;
    while(i<=L.length&&!(*compare)(*p++,e))
    ++i;
    if(i<=L.length)  return i;
    else return 0;
}

Status ListInsert(SqList &L,int i,ElemType e){
    //在顺序线性表L中第i个位置之前插入新的元素e
    ElemType *p,*q,*newbase;
    if(i<1||i>L.length+1) return ERROR;
    if(L.length>=L.listsize){
        newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
        if(!newbase) exit(OVERFLOW);
        L.elem=newbase;
        L.listsize+=LISTINCREMENT;
    }
    q=&(L.elem[i-1]);
    for(p=&(L.elem[L.length-1]);p>=q;--p)
    *(p+1)=*p;
    *q=e;
    ++L.length;
    return OK;
}

Status ListDelete(SqList &L,int i,ElemType &e){
    //在顺序线性表L中删除第i个元素,并用e返回其值
    ElemType *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;
}
2011-09-23 11:16
spzb11b
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-9-20
收藏
得分:0 
c:\users\lin\desktop\123.c(21) : error C2143: syntax error : missing ')' before '&'
c:\users\lin\desktop\123.c(21) : error C2143: syntax error : missing '{' before '&'
c:\users\lin\desktop\123.c(21) : error C2059: syntax error : '&'
c:\users\lin\desktop\123.c(21) : error C2059: syntax error : ')'
c:\users\lin\desktop\123.c(30) : error C2143: syntax error : missing ')' before '&'
c:\users\lin\desktop\123.c(30) : error C2143: syntax error : missing '{' before '&'
c:\users\lin\desktop\123.c(30) : error C2059: syntax error : '&'
c:\users\lin\desktop\123.c(30) : error C2059: syntax error : ')'
c:\users\lin\desktop\123.c(38) : error C2143: syntax error : missing ')' before '&'
c:\users\lin\desktop\123.c(38) : error C2143: syntax error : missing '{' before '&'
c:\users\lin\desktop\123.c(38) : error C2059: syntax error : '&'
c:\users\lin\desktop\123.c(38) : error C2059: syntax error : ')'
c:\users\lin\desktop\123.c(54) : error C2143: syntax error : missing ')' before '&'
c:\users\lin\desktop\123.c(54) : error C2143: syntax error : missing '{' before '&'
c:\users\lin\desktop\123.c(54) : error C2059: syntax error : '&'
c:\users\lin\desktop\123.c(54) : error C2059: syntax error : ')'
c:\users\lin\desktop\123.c(72) : error C2143: syntax error : missing ')' before '&'
c:\users\lin\desktop\123.c(72) : error C2143: syntax error : missing '{' before '&'
c:\users\lin\desktop\123.c(72) : error C2059: syntax error : '&'
c:\users\lin\desktop\123.c(72) : error C2059: syntax error : ')'
c:\users\lin\desktop\123.c(90) : error C2143: syntax error : missing ')' before '&'
c:\users\lin\desktop\123.c(90) : error C2143: syntax error : missing '{' before '&'
c:\users\lin\desktop\123.c(90) : error C2059: syntax error : '&'
c:\users\lin\desktop\123.c(90) : error C2059: syntax error : ')'
2011-09-23 20:50
快速回复:大家好,我是c++新手,正在学习《数据结构》有些不懂的地方想请教一下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017655 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved