主题:[讨论]c语言问题
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX 100
#define INCREASE 10
typedef struct Node{
int *elem;
int length;//当前表长度
int listsize;//当前分配的存储容量
}SqList;
void IntList(SqList &);//初始化
void print(SqList );//打印表
void BuildList(SqList &,int ,int );//建立数据表
int InsertList(SqList &,int ,int );//插入数据
int main()
{
SqList L,L1[1],L3;
int i,j,count;
int new_num;
int delete_num;
int getnum;
int insert_num;
int mark;
IntList(L);
printf("请输入要插入多少数据\n");
scanf("%d",&count);
printf("请输入数据\n");
for(i = 0;i < count;i++)
{
scanf("%d",&new_num);
BuildList(L,i,new_num);
}
print(L);
printf("请输入要插入第几个数据的后面,并输入插入的数据\n");
printf("若插在开头,则请输入插入的数据为0\n");
scanf("%d%d",count,insert_num);
InsertList(L,count,insert_num);
print(L);
system("pause");
return 0;
}
void IntList(SqList &L)
{
L.elem=(int *)malloc(MAX * sizeof(int));
if(!L.elem)
{ printf("!!!ERROR!!!!\n"); exit(0);}
L.length = 0;
L.listsize = MAX;
}
void BuildList(SqList &L,int i,int num1)
{
int *newbase;//一段空间
if( L.length >= L.listsize)
{
newbase = (int *)realloc(L.elem ,(L.listsize + INCREASE) * sizeof(int));
if(!newbase) { printf("!!!!ERROR!!!!!!\n"); exit(0);}
L.elem = newbase;
L.listsize += INCREASE;
}
L.elem[i] = num1;
L.length++;
}
int InsertList(SqList &L,int count,int figure)
{
int *p = L.elem + L.length - 1;
int *q = L.elem + count;
int *newbase;
if(count < 0 || count > L.length) return 0;
if(L.length >= L.listsize)
{
newbase = (int *)realloc(L.elem,(L.listsize + INCREASE) * sizeof(int));
if(!newbase) { printf("!!!!ERROR!!!!!!\n"); exit(0);}
L.elem = newbase;
L.listsize += INCREASE;
}
for( ;p >= q;p--)
*(p + 1) = *p;
*q = figure;
L.length++;
}
void print(SqList L)
{
int i;
int *p = L.elem;
for(i = 1;i < L.length;i++)
printf("%d ",*p++);
printf("%d\n",*p);
}
插入表的时候出现了一点问题,,求指教[em18][em18][em18][em18]
#include<stdlib.h>
#include<malloc.h>
#define MAX 100
#define INCREASE 10
typedef struct Node{
int *elem;
int length;//当前表长度
int listsize;//当前分配的存储容量
}SqList;
void IntList(SqList &);//初始化
void print(SqList );//打印表
void BuildList(SqList &,int ,int );//建立数据表
int InsertList(SqList &,int ,int );//插入数据
int main()
{
SqList L,L1[1],L3;
int i,j,count;
int new_num;
int delete_num;
int getnum;
int insert_num;
int mark;
IntList(L);
printf("请输入要插入多少数据\n");
scanf("%d",&count);
printf("请输入数据\n");
for(i = 0;i < count;i++)
{
scanf("%d",&new_num);
BuildList(L,i,new_num);
}
print(L);
printf("请输入要插入第几个数据的后面,并输入插入的数据\n");
printf("若插在开头,则请输入插入的数据为0\n");
scanf("%d%d",count,insert_num);
InsertList(L,count,insert_num);
print(L);
system("pause");
return 0;
}
void IntList(SqList &L)
{
L.elem=(int *)malloc(MAX * sizeof(int));
if(!L.elem)
{ printf("!!!ERROR!!!!\n"); exit(0);}
L.length = 0;
L.listsize = MAX;
}
void BuildList(SqList &L,int i,int num1)
{
int *newbase;//一段空间
if( L.length >= L.listsize)
{
newbase = (int *)realloc(L.elem ,(L.listsize + INCREASE) * sizeof(int));
if(!newbase) { printf("!!!!ERROR!!!!!!\n"); exit(0);}
L.elem = newbase;
L.listsize += INCREASE;
}
L.elem[i] = num1;
L.length++;
}
int InsertList(SqList &L,int count,int figure)
{
int *p = L.elem + L.length - 1;
int *q = L.elem + count;
int *newbase;
if(count < 0 || count > L.length) return 0;
if(L.length >= L.listsize)
{
newbase = (int *)realloc(L.elem,(L.listsize + INCREASE) * sizeof(int));
if(!newbase) { printf("!!!!ERROR!!!!!!\n"); exit(0);}
L.elem = newbase;
L.listsize += INCREASE;
}
for( ;p >= q;p--)
*(p + 1) = *p;
*q = figure;
L.length++;
}
void print(SqList L)
{
int i;
int *p = L.elem;
for(i = 1;i < L.length;i++)
printf("%d ",*p++);
printf("%d\n",*p);
}
插入表的时候出现了一点问题,,求指教[em18][em18][em18][em18]