注册 登录
编程论坛 C++教室

一个关于线性表很菜的问题。知道怎么对线性表输入输出的救救俺

zfhb110 发布于 2008-06-30 16:54, 2224 次点击
下面是我写的一个关于线性表操作的函数(要很多操作,比如排序等,但是在线性表数字输入和输出时就有问题,所以就不写排序的函数了,高手只要教我怎么把他的输入输出问题解决就行)

#include "stdafx.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 int ElemType;

typedef struct
{
    ElemType *elem;
    int length;
    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


void intput(SqList &L)
{
    for(int i=0;L.length++;i++)
    scanf("%d",&L.elem[i]);
    

}

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

int main()
{
    SqList L;
    InitList_Sq(L);
    printf("请输入待排序的整数:\n");
    intput(L);
    display(L);


    printf("Hello World!\n");
    return 0;
}

最后执行的时候输入许多数,输出始终只有第一个,拜托大家帮帮忙,谢谢


[[it] 本帖最后由 zfhb110 于 2008-6-30 17:01 编辑 [/it]]
11 回复
#2
很远的那颗星2008-06-30 16:59
错误提示发上来看一下.
#3
zfhb1102008-06-30 17:03
错误也没有,就是我输入几个数比如 2 3 45 62 42 36时,他总只显示第一个数字2
#4
zfhb1102008-06-30 17:05
我觉得应该是输入那里有问题,可我又不知道怎么改
#5
很远的那颗星2008-06-30 17:08
void intput(SqList &L)
{
    for(int i=0;L.length++;i++)      //这里什么意思?
    scanf("%d",&L.elem[i]);
   

}
#6
zfhb1102008-06-30 17:11
就是想把数字输入线性表
#7
很远的那颗星2008-06-30 17:18
就你这个程序来说,我给你改到可以运行吧,具体优化你自已去弄.首先你应该给length一个大于零的初值.然后输入那里改为:
void intput(SqList &L)
{
    for(int i=0;i<L.length;i++)      
    scanf("%d",&L.elem[i]);
   

}
你应该知道,你的输入函数设计的很不好,你好好改良一下吧.
#8
很远的那颗星2008-06-30 17:22
我要去吃饭了,随便弄一下,不行回来再看了.
//#include "stdafx.h"
#include <stdlib.h>
#include<stdio.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 int ElemType;

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

Status InitList_Sq(SqList &L)
{
    L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)exit(OVERFLOW);
    L.length=5;
    L.listsize=LIST_INIT_SIZE;
    return OK;

}//InitList_Sq


void intput(SqList &L)
{
    for(int i=0;i<L.length;i++)
    scanf("%d",&L.elem[i]);
   

}

void display(SqList &L)
{
    for(int i=0;i<L.length;i++)
       printf("%3d",L.elem[i]);   //注意输出格式
}

int main()
{
    SqList L;
    InitList_Sq(L);
    printf("请输入待排序的整数:\n");
    intput(L);
    display(L);


    printf("Hello World!\n");
    system("pause");
    return 0;
}
#9
zfhb1102008-07-01 17:31
不管怎么说 谢谢大家
#10
zfhb1102008-07-01 17:35
那能不能使那个长度实现动态,就是我输入多少就是多少那种?
#11
很远的那颗星2008-07-01 18:26
当然可以.
Status InitList_Sq(SqList &L)
{
    L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)exit(OVERFLOW);

    L.length=0;  //你在这里改成输入L.lenrth不就行了.
    L.listsize=LIST_INIT_SIZE;
    return OK;
#12
Rand2008-07-02 15:10
[bo][un]很远的那颗星[/un] 在 2008-7-1 18:26 的发言:[/bo]

当然可以.
Status InitList_Sq(SqList &L)
{
    L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)exit(OVERFLOW);

    L.length=0;  //你在这里改成输入L.lenrth不就行了.
   ...

或者你多加个count来统计输入的值,L.length=count就可以了。
1