编译无错,运行时出现错误

honbaa 2011-08-03 08:31:07
源程序:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
int *elem;
int length;
int listsize;
}Sqlist;

//建立一个空的线性表
void InitList_Sq(Sqlist *L){
L->elem=(int *)malloc(sizeof(int));
if(!L->elem)exit(-1);
L->length=0;
L->listsize=LIST_INIT_SIZE;
}

//线性表顺序映像的插入操作
void InsertList_Sq(Sqlist *L,int i,int e){
int j;
if(i<1||i>L->length+1)
{
printf("input error");
exit(-2);
}
if(L->length>=L->listsize){
int *newbase=(int *)realloc(L->elem,(LIST_INIT_SIZE+LISTINCREMENT)*sizeof(int));
if(!newbase)exit(-3);
L->elem=newbase;
L->listsize=L->listsize+LISTINCREMENT;
}
for(j=L->length;j>=i;j--){
L->elem[j]=L->elem[j-1];
}
L->elem[i-1]=e;
L->length++;
}
//线性表的顺序映像删除操作
void DeleteList_Sq(Sqlist *L,int i){
int j;
if(i<1||i>L->length) {
printf("the position is wrong");
exit(-4);
}
for(j=i+1;j<=L->length;j++)
{
L->elem[j-2]=L->elem[j-1];
}
L->length--;
}
int main()
{
int i,j,k,n;
Sqlist L1;
InitList_Sq(&L1);
printf("input the total you want to input into the linear list:\n");
scanf("%d",&i);

for(k=1;k<=i;k++){

InsertList_Sq(&L1,k,k);
}
printf("the linear you have created :\n");//打印出来刚才建立的线性表
for(n=1;n<=L1.length;n++)
printf("%d ",L1.elem[n-1]);
printf("\nselet the operation you want :\n");
printf("*****************************************\n");
printf("1.在表中插入一个整数。\n");
printf("2.删除表中的某个整数。\n");
printf("*****************************************\n");
printf("choose:");
scanf("%d",n);
switch(n){
case 1 :{
printf("\n输入要插入元素的位置和元素的值,两者用空格隔开:");
scanf("%d%d",i,j);
InsertList_Sq(&L1,i,j);
}break;
case 2 :{
printf("\n输入要删除元素的位置:");
scanf("%d",i);
DeleteList_Sq(&L1,i);
}break;
default:printf("输入错误\n");

}
return 0;


}
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
awsqsh 2011-08-03
  • 打赏
  • 举报
回复
还差内存释放。
honbaa 2011-08-03
  • 打赏
  • 举报
回复
我知道了,呵呵就是scanf("%d",&n),没有&,呵呵,谢谢了
honbaa 2011-08-03
  • 打赏
  • 举报
回复
运行后,操作系统提示错误的对话框。。不知道是哪里错了,也不知为什么错
无限生机 2011-08-03
  • 打赏
  • 举报
回复
没怎么仔细看,但是发现一个很明显的错误
scanf("%d",&n);
你的scanf都没有用取地址符。
恨天低 2011-08-03
  • 打赏
  • 举报
回复
贴出错误输出?LINK错误?

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧