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

关于C链表的问题,请高手解决

黄昏的王座 发布于 2011-10-25 16:22, 500 次点击
#include <malloc.h>
#include <stdio.h>
#define list_init_size  10
#define listincrement   2
typedef struct {
    int      *elem;
    int      length;
    int      listsize;
}SqList;
int init_list(SqList L)
{
    L.elem=(int *)malloc(sizeof(int)*list_init_size);
    //L.elem=(struct student *)malloc(sizeof(struct)*list_init_size);
    if(!L.elem)
        exit (-1);
    else
    {
        L.listsize=list_init_size;
        return (1);
    }
}

int main()
{
    int j;
    SqList M;
    init_list(M);
    char a[]={1,2,3};
    M.length =3;
    for (j=0;j<M.length;j++)
    {
      M.elem[j]=a[j];
    }
    for (j=0;j<M.length;j++)
    {
        printf("%2d\n",M.elem[j]);
    }
}
1 回复
#2
nomify2011-10-25 18:44
程序代码:
#include <stdlib.h>
#include <stdio.h>
#define list_init_size  10
#define listincrement   2
typedef struct {
    int      *elem;
    int      length;
    int      listsize;
}SqList;
int init_list(SqList & L)
{
    L.elem=(int *)malloc(sizeof(int)*list_init_size);
    //L.elem=(struct student *)malloc(sizeof(struct)*list_init_size);
    if(!L.elem)
        exit (-1);
    else
    {
        L.listsize=list_init_size;
        return (1);
    }
}

int main()
{
    int j;
    SqList M;
    init_list(M);
    char a[]={1,2,3};
    M.length =3;
    for (j=0;j<M.length;j++)
    {
      M.elem[j]=a[j];
    }
    for (j=0;j<M.length;j++)
    {
        printf("%2d\n",M.elem[j]);
    }
    return 0;
}
1