VC++创建一个空的线性表,依次插入数据元素构成非空线性表,然后输出该线性表。
答案:1 悬赏:0 手机版
解决时间 2021-11-12 18:56
- 提问者网友:回忆在搜索
- 2021-11-12 01:08
VC++创建一个空的线性表,依次插入数据元素构成非空线性表,然后输出该线性表。
最佳答案
- 五星知识达人网友:狂恋
- 2021-11-12 01:46
#include "stdafx.h"#include
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef char ElemType;
int i=0;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
//创建线性表
void CreateSqList(SqList &L)
{
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem) exit(0);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
}
//插入元素
void InsertSqList(SqList &L,ElemType e)
{
if(L.length>=L.listsize)
{
ElemType * newbase = (ElemType *)realloc(L.elem,(LIST_INIT_SIZE+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(0);
L.elem = newbase;
}
L.elem[i] = e;
L.length++;
i++;
}
int main()
{
SqList L;
CreateSqList(L);
char ch;
do{
ch = getchar();
InsertSqList(L,ch);
}while(ch!='
');
for(int i=0;i {
printf("%c",L.elem[i]);
}
return 0;
}
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef char ElemType;
int i=0;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
//创建线性表
void CreateSqList(SqList &L)
{
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem) exit(0);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
}
//插入元素
void InsertSqList(SqList &L,ElemType e)
{
if(L.length>=L.listsize)
{
ElemType * newbase = (ElemType *)realloc(L.elem,(LIST_INIT_SIZE+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(0);
L.elem = newbase;
}
L.elem[i] = e;
L.length++;
i++;
}
int main()
{
SqList L;
CreateSqList(L);
char ch;
do{
ch = getchar();
InsertSqList(L,ch);
}while(ch!='
');
for(int i=0;i
printf("%c",L.elem[i]);
}
return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯