功能较大的线性表出现几个小问题!

wangliuqing 2009-03-22 03:41:18
//LnLick.h

#ifndef LinkList_H
#define LinkList_H

//LineList.h
#include<iostream>
using namespace std;

template<class T>
//typedef Student ElemType;
class student
{
//初始化线性表,即置个void InitLnList( LnList & LL );
//初始化线性表,分配ms大小的空间
public:
void InitLnList( LnList & LL, int ms );
//得到线性表中指定位置的值
//ElemType GetElemLnList( LnList &LL, int pos );
//线性表顺序查找某元素,返回该元素的位置
int SeqFindLnList( LnList &LL, ElemType item );
//二分查找,返回该元素的位置
int BinFindLnList( LnList &LL, ElemType item );
//修改线性表中指定的元素
bool ModifyLnList( LnList &LL, ElemType item );
//从线性表末插入元素(无序表)
bool TailInsLnList( LnList &LL, ElemType item );
//有序表中插入元素后,线性表仍然有序
void OrderInLnList( LnList &LL, ElemType item );
//删除一个元素
bool DeleteLnList( LnList &LL, ElemType item );
//线性表长度
int LengthLnList( LnList &LL );
//线性表是否为空
bool EmptyLnList( LnList &LL );
//遍历输出线性表
void OutLnList( LnList &LL );
//按关键字对线性表排序
void SortLnList_1( LnList &LL );
//对线性表排序
LnList SortLnList_2( LnList &LL );
//清除线性表动态分配的空间
void ClearLnList( LnList &LL );
private:
int num,
double grade;
T *data;
int len;
const int MS = 80;
};
#endif



template< typename T >
static bool operator==( const ElemType &item1, const ElemType &item2 )
{
return item1.num == item2.num;
}
template< typename T >
static bool operator < ( const ElemType &item1, const ElemType &item2 )
{
return item1.num < item2.num;
}
template< typename T >

static ostream & operator << ( ostream & out, ElemType & item )
{
out << item.num << ' ' << item.grade << endl;
return out;
}
template< typename T >
void InitLnList( LnList &LL )//初始化线性表,即置个域为空
{
LL.data = NULL;
LL.len = LL.MaxSize = 0;
}
template< typename T >
void InitLnList( LnList &LL, int ms )
{
if( ms < 0 )
{
cout << "SIZE CANN'T BE ZERO!" << endl;
exit(1);
}
LL.MaxSize = ms;
LL.data = new ElemType[ms];
if( !LL.data )
{
cerr << "ALLOCATE FAILED!" << endl;
exit(1);
}
LL.len = 0;
}
template< typename T >
T GetElemLnList( LnList &LL, int pos )
{
if( pos < 0 || pos > LL.len )
{
cout << "POS OUT RANGE!" << endl;
exit(1);
}
return LL.data[pos-1];//线性表顺序查找某元素,返回该元素的位置
}

template< typename T >
int SeqFindLnList( LnList &LL, ElemType item )
{
for( int i = 0; i < LL.len; ++i )
if( LL.data[i] == item )
return i;//二分查找,返回该元素的位置
return -1;
}
template< typename T >
int BinFindLnList( LnList &LL, ElemType item )
{
int left = 0, right = LL.len-1;
int middle = 0;
while( left <= right )
{
middle = (right - left) / 2;
if( LL.data[middle] == item )
return middle;
else if( item < LL.data[middle] )
right = middle - 1;
else
left = middle + 1;
}
return -1;
}//修改线性表中指定的元素

template< typename T >
bool ModifyLnList( LnList &LL, ElemType item )
{
int i = 0;
i = SeqFindLnList( LL, item );//查找函数
if( i < LL.len )
{
LL.data[i] = item;
return true;
}
return false;
}//从线性表末插入元素

template< typename T >
bool TailInsLnList( LnList &LL, ElemType item )
{
if( LL.len == LL.MaxSize )
{
cout << "Is Full" << endl;
exit(1);
}
for( int i = 0; i < LL.len; ++i )
if( item == LL.data[i] )
{
cout << "重复了" << endl;
return false;
}
LL.data[LL.len] = item;
LL.len++;
return true;
}//从线性表末插入元素


//void OrderInsLnList( LnList &LL, ElemType item )
//{
// if( LL.len == LL.MaxSize )
//{
//cout << "Is Full" << endl;
//exit(1);
//}
//for( int i = 0; i < LL.len; ++i )error:using obsolete binding at'i'
// if( item < LL.data[i] )
// break;
// for( int j = LL.len - 1; j >= i; --j )error:name lookup of 'i' changed for new ISO 'for'scoping
// LL.data[j+1] = LL.data[j];
// LL.data[j] = item;
// LL.len++;
//}有序表中插入元素
template< typename T >
void OrderInsLnList(LnList &LL,ElemType item)
{
int i=0;
if(LL.len>=LL.MaxSize) throw"上溢";
if(i<1||i>LL.len+1)throw"下溢";
for(int i;j=LL.len;j=i;j--)
data[j]=data[j-1];
data[j-1]=x;
LL.len++;
}


template< typename T >
bool DeleteLnList(LnList &LL, ElemType item )
{
for( int i = 0; i < LL.len; ++i )//error:using obsolete binding at'i'
if( item == LL.data[i] ) break;
if( i == LL.len ) return false; //error:name lookup of 'i'changed for new ISO 'for' scoping
for( int j = i + 1; j < LL.len; ++j )
LL.data[j-1] = LL.data[j];
LL.len--;
return true;
}//删除一个元素
template< typename T >
int LengthLnList( LnList &LL )
{
return LL.len;
}//线性表的长度
bool EmptyLnList( LnList &LL )
{
return LL.len == 0;
}//线性表是否为空

template< typename T >
void OutLnList( LnList &LL )
{ //error:in passing argumkent 1 of 'ovid' OutLnList(LnList&)'
for( int i = 0; i < LL.len; ++i )
cout << LL.data[i] ;
}//遍历输出线性表
template< typename T >
void SortLnList_1( LnList &LL )
{
ElemType x;
for( int i = 1; i < LL.len; ++i )
{
x = LL.data[i];
for( int j = i-1; j >= 0; --j ) //error:using obsolete binding at'i'
{
if( x < LL.data[j] )
LL.data[j+1] = LL.data[j];
else
break;
}
LL.data[j+1] = x;//error:name lookup of 'i'changed for new ISO'for'scoping
}
}//按关键字对线性表进行排序

//对线性表排序
template< typename T >
LnList SortLnList_2( LnList &LL )
{
ElemType x;
LnList LL_1;
InitLnList( LL_1, MS );
for( int i = 0; i < LL.len; ++i )
{
x = LL.data[i];
OrderInsLnList( LL_1, x );
}
return LL_1;
}
template< typename T >
void ClearLnList( LnList &LL )
{
delete [] LL.data;
LL.len = LL.MaxSize = 0;
cout << "Cleared!" << endl;
}//清除线性表动态分配空间
//LnList.cpp






#include "LnList.h"
int main()
{
LnList LL;
InitLnList( LL, MS );//初始化线性表
Student a[6] = {{ 4001, 100 },{ 4003, 89 },{ 4005, 98 },{ 4002, 69 },{ 4007, 79 },{ 4004, 99 }};
for( int i = 0; i < 6; ++i )
TailInsLnList( LL, a[i] );//从线性表末插入元素
cout << "初始线性表:"<< endl;
OutLnList( LL );
cout << endl;
SortLnList_1( LL );//按关键字对线性表进行排序
OutLnList( LL );//遍历输入线性表
Student b = { 2000, 100 };
OrderInsLnList( LL, b );//有序表中插入元素
OutLnList( LL );//按关键字对线性表进行排序
cout << endl;
TailInsLnList( LL, b );//从线性表末插入元素
OutLnList( LL );
cout << endl;
int x = SeqFindLnList( LL, b );//顺序查找某元素
cout << x << endl;//error:expected','or';'before "cout'
DeleteLnList( LL, b );//某元素
OutLnList( LL );
cout << endl;
Student c = { 4003 };
x = BinFindLnList( LL, c );
cout << x << endl;
ModifyLnList( LL, c );
OutLnList( LL );
Student d = { 4006, 97 };
OrderInsLnList( LL,d );
OutLnList( LL );
OutLnList( SortLnList_2( LL ) );//error:invalid initializaqtion of non-const reference of type
//'LnList&' from a temporary of type 'LnList'
in passing ar
ClearLnList( LL ); return 0;
}


这个线性表出现几个问题,主要实现注释中所述的功能,但可能还有好多处错误。望各位调试一下。能帮我指出!



...全文
201 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
downmooner 2009-03-28
  • 打赏
  • 举报
回复
好几处少了大括号。DeleteLnList()SortLnList_1()
bool DeleteLnList(LnList &LL, ElemType item )
{
for( int i = 0; i < LL.len; ++i )//error:using obsolete binding at'i'
{
if( item == LL.data[i] ) break;
if( i == LL.len ) return false; //error:name lookup of 'i'changed for new ISO 'for' scoping
for( int j = i + 1; j < LL.len; ++j )
LL.data[j-1] = LL.data[j];
LL.len--;
}
return true;
}


主函数内的这个语句写错分号
int x = SeqFindLnList( LL, b );//顺序查找某元素


主函数最后一个提示错误的地方
OutLnList( SortLnList_2( LL ) );//这里sortlnlist返回是的临时变量,不可以作为Outlnlist函数的参数
mengde007 2009-03-28
  • 打赏
  • 举报
回复
帮顶。
wangliuqing 2009-03-28
  • 打赏
  • 举报
回复
错误我都在后面标出来了,只要能减少错误就有分加哦!
n_yHHy_n 2009-03-22
  • 打赏
  • 举报
回复
up
crond123 2009-03-22
  • 打赏
  • 举报
回复
友情帮顶!!
帅得不敢出门 2009-03-22
  • 打赏
  • 举报
回复
帮顶

65,211

社区成员

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

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