插入求解

ranxufeng 2012-09-27 12:48:50
#include "stdafx.h"
#include"stdio.h"
#include"jiguot.h"
#include"malloc.h"
#include"stdlib.h"
#define N 5
Status ListInitList_Sq(SqList &L, int i, ElemType e);
Status InitList_Sq(SqList &L);


int _tmain(int argc, _TCHAR* argv[])
{
int a[N],c,x,i=0;
SqList L;
for(c=1;c<N;c++)
{
scanf("%d",&a[c]);
}
printf("请输入插入的位置,插入的数:\n");
scanf("%d\n%d",&i,&x);
ListInitList_Sq(L, i, x);
printf("%d",a[c]);
int m;
scanf("%d",&m);
return 0;
}



结果 不对 求教
...全文
91 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
图灵狗 2012-09-27
  • 打赏
  • 举报
回复
看不到ListInitList_Sq的作为。
ranxufeng 2012-09-27
  • 打赏
  • 举报
回复
所以要改成上面的样子。????上面????什么样子 没有吧
拜托了
怎么改
图灵狗 2012-09-27
  • 打赏
  • 举报
回复
ListInitList_Sq(L, i, x);
printf("%d",a[c]);
这里的a[c]越界了,因为此时c等于N了,所以要改成上面的样子。

[Quote=引用 6 楼 的回复:]

这儿没有错吧 用循环语句赋值给数组 输入小于N个的数啊
[/Quote]
ranxufeng 2012-09-27
  • 打赏
  • 举报
回复
这儿没有错吧 用循环语句赋值给数组 输入小于N个的数啊
图灵狗 2012-09-27
  • 打赏
  • 举报
回复
for(c = 1; c < N; c++)
{
printf("%d", a[c]);
}
否则c在前一次循环之后已经是N了,这样就越界了。
ranxufeng 2012-09-27
  • 打赏
  • 举报
回复
恩 输出 有问题 printf("%d",a[c]);
a[c]应该怎么改
ranxufeng 2012-09-27
  • 打赏
  • 举报
回复
typedef int Status;
typedef int ElemType;
#define ok 1
#define OVERFLOW -2
#define ERROR 0

#include"stdafx.h"
#include"jiguot.h"
#include"malloc.h"
#include"stdlib.h"


Status ListInitList_Sq(SqList &L, int i, ElemType e)
{

int *q=NULL,*p=NULL;
int *newbase;
if(i<1||i>L.length+1)return ERROR;
if(L.length>=L.listsize)
{
newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)exit(OVERFLOW);
L.elem=newbase;
L.listsize +=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;
*q = e;
++L.length;
return ok;
}

#include"yuchuli.h"
#include"stdafx.h"
#include"jiguot.h"
#include"malloc.h"
#include"stdlib.h"


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;
}


#include"yuchuli.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;

// 2.4.cpp : 定义控制台应用程序的入口点。//

// 123.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include"stdio.h"
#include"jiguot.h"
#include"malloc.h"
#include"stdlib.h"
#define N 5
Status ListInitList_Sq(SqList &L, int i, ElemType e);
Status InitList_Sq(SqList &L);


int _tmain(int argc, _TCHAR* argv[])
{
int a[N],c,x,i=0;
SqList L;
printf("请输入五位数:\n");
for(c=1;c<N;c++)
{
scanf("%d",&a[c]);
}
printf("请输入插入的位置,插入的数:\n");
scanf("%d\n%d",&i,&x);
ListInitList_Sq(L, i, x);
printf("%d",a[c]);
int m;
scanf("%d",&m);
return 0;
}




星星x 2012-09-27
  • 打赏
  • 举报
回复
printf("%d",a[c]); 这个下标越界了吧

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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