一个关于顺序表插入的问题

Router66 2008-12-05 01:59:37
// t_4.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}


#include <iostream>
using namespace std;

#define N 100

struct Node
{
int data[100];
int length;
}SeqList;

void InsertList(SeqList &L,int x,int i);
{
int j;
if(i<1||i>L.length+1)
{
cout<<"i的取值有误";
getchar();
exit(0);
}
if(L.length>=N)
{
cout<<"溢出错误";
getchar();
return;
}
for(j=L.length-1;j>=i;j--)
L.data[j+1]=L.data[j];
L.data[i]=x;
L.length++;
}

void main()
{
int a,b;
cout<<"请输入你想在增加的位置";
cin>>a;
cout<<"请输入节点的值";
cin>>b;
SeqList *L;
InsertList(L,b,a);

}



调试的时候,系统提示
error C2065: “L”: 未声明的标识符
error C2062: 意外的类型“int”
error C2447: “{”: 缺少函数标题(是否是老式的形式表?)
error C3861: “InsertList”: 找不到标识符

请大家帮我看一下问题到底在哪? 谢谢
...全文
83 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lann64 2008-12-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wei4571711 的回复:]
那如何建立L呢
[/Quote]
你得写个建立L的函数。否则
SeqList L;//这里定义出一个空的L
InsertList(L,b,a);//你在函数里对小于1位置的插入,直接判定错,而L是空的,位置总是小于1的。

用你的程序,空L总是无法插入。
jiww03 2008-12-05
  • 打赏
  • 举报
回复
怎么传的参数是指针,而函数定义的参数是引用啊,直接这样SeqList L; InsertList(L,b,a);
Router66 2008-12-05
  • 打赏
  • 举报
回复
那如何建立L呢
lann64 2008-12-05
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

#define N 100

typedef struct Node
{
int data[100];
int length;
} SeqList;

void InsertList(SeqList& L,int x,int i)
{
int j;
if (i <1||i>L.length+1)
{
cout <<"i的取值有误";
getchar();
exit(0);
}
if (L.length>=N)
{
cout <<"溢出错误";
getchar();
return;
}
for (j=L.length-1;j>=i;j--)
L.data[j+1]=L.data[j];
L.data[i]=x;
L.length++;
}

int main()
{
int a,b;
cout <<"请输入你想在增加的位置";
cin>>a;
cout <<"请输入节点的值";
cin>>b;
SeqList L;
InsertList(L,b,a);
return 0;
}

不过你的L没建立呀。
P_ghost 2008-12-05
  • 打赏
  • 举报
回复
只改了你的语法错误,逻辑是否有错自己测试。
#include <iostream> 
using namespace std;

#define N 100

typedef struct Node
{
int data[100];
int length;
}SeqList;

void InsertList(SeqList &L,int x,int i)
{
int j;
if(i <1||i>L.length+1)
{
cout <<"i的取值有误";
getchar();
exit(0);
}
if(L.length>=N)
{
cout <<"溢出错误";
getchar();
return;
}
for(j=L.length-1;j>=i;j--)
L.data[j+1]=L.data[j];
L.data[i]=x;
L.length++;
}

void main()
{
int a,b;
cout <<"请输入你想在增加的位置";
cin>>a;
cout <<"请输入节点的值";
cin>>b;
SeqList L;
InsertList(L,b,a);

}
OenAuth.Core 2008-12-05
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

#define N 100

typedef struct Node //typedef重命名
{
int data[100];
int length;
} SeqList;

void InsertList(SeqList &L,int x,int i)//不要加分号
{
int j;
if(i <1||i>L.length+1)
{
cout <<"i的取值有误";
getchar();
exit(0);
}
if(L.length>=N)
{
cout <<"溢出错误";
getchar();
return;
}
for(j=L.length-1;j>=i;j--)
L.data[j+1]=L.data[j];
L.data[i]=x;
L.length++;
}

void main()
{
int a,b;
cout <<"请输入你想在增加的位置";
cin>>a;
cout <<"请输入节点的值";
cin>>b;
SeqList L;
InsertList(L,b,a); //传值,而不是指针,第一个参数是引用,不是地址

}


64,666

社区成员

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

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