如何动态初始化顺序表

xiaha3 2003-11-22 04:18:09
如何动态初始化顺序表

struct SeqList{
DataType data[ListSize];//向量data用于存放表结点
int length;//当前的表长度
};
我想在程序运行是确定data的长度,也就是ListSize,怎么办.
...全文
139 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaha3 2003-11-22
  • 打赏
  • 举报
回复
plainsong(短歌) 说的很对,实际上我见过的都是这样,越简单却越慢(很多人这样的),因为该优化的地方没优化,导致计算机做了很多没有意义的事情
短歌如风 2003-11-22
  • 打赏
  • 举报
回复
>现在的c++程序员真是很奇怪,一个本来需要速度的问题却用了这么一段低效率的代码

楼上大概是在说我了。

不过如果你是做底层的,我想你也应该明白:
1:重分配内存(增长)是一个代价很大的操作,应该减少它的次数;
2:空间问题同样需要考虑,不能一次就分配可能用到的最大长度。

很明显,一个动态数组在增长时我们不能每增长一个元素就调用一次realloc,如果这样实现,无论你的优化能力有多强,它不会很快——虽然它的代码很短;也不能一上来就分配几十M,这样虽然也许用不着再去用realloc处理动态增长问题,可抢劫了动态数组的意义——也许你只用了几个元素的空间。事实上我的代码还不够复杂,真正有用的策略是在数组较短和较长时使用不同的增长/缩减策略。

代码简单不代表快,大家应该都明白吧。冒泡排序多么简单啊,Intro Sort让人看着都头疼。
gladiatorcn 2003-11-22
  • 打赏
  • 举报
回复
楼上说得很对,其实就是一个动态数组的问题。基本的思路是指针引导的动态内存分配。现在的c++程序员真是很奇怪,一个本来需要速度的问题却用了这么一段低效率的代码,编译后的损失更大,而且代码冗长没有优化。对不起哦!我是做底层的,见不得那么啰嗦的事情。
leyt 2003-11-22
  • 打赏
  • 举报
回复
用指针是个不错的方法
短歌如风 2003-11-22
  • 打赏
  • 举报
回复
你需要修改结构:
struct SeqList{
DataType *data;//向量data用于存放表结点
int length;//当前的表长度
int datasize;
};
void SeqList_Init(SeqList* list)
{
list->data = NULL;
list->length = 0;
list->datasize = 0;
}
bool SeqList_SetLength(SeqList* list, int new_len)
//这里面维护你的内存增长策略,由你自己决定。我给的只是一个例子
{
DataType* Temp;
if(new_len > list->datasize)
{
if(list->data == NULL)
{
Temp = list->data = (DataType*)malloc(sizeof(DataType)*(new_len + new_len / 2));
if (Temp != NULL)
list->datasize = new_len + new_len / 2;
}
else
{
Temp = (DataType*)ralloc(list->data, sizeof(DataType)*(new_len + new_len / 2);
if (Temp != NULL)
{
list->data = Temp;
list->datasize = new_len + new_len / 2;
}
}
if (Temp != NULL)
{
list->length = new_len;
return true;
}
else
return false;
}
else if (new_len < list->datasize)
{
list->data = realloc(list->data, new_len*sizeof(DataType));
list-> datasize = new_len;
list->length = new_len;
return true;
}
}


oopig 2003-11-22
  • 打赏
  • 举报
回复
用指针不就可以了
struct SeqList{
DataType *data;//向量data用于存放表结点
int length;//当前的表长度
};
使用的时候:
struct SeqList node;
int ListSize = 10;
node.data = new DataType[ListSize];
...
释放:
struct SeqList node;
if (node.data)
{
delete[] data;
data = 0;
}
liuleilover 2003-11-22
  • 打赏
  • 举报
回复
不需要这么长吧!,关注!
wansong 2003-11-22
  • 打赏
  • 举报
回复
这是我的程序,不知道对不对,希望大家提一下意见!谢谢了!

//动态分配存储的线性表顺序结构

#ifndef SqList_CLASS
#define SqList_CLASS

#include <iostream>
using namespace std;

//--------------------------------------------------------------------
template <class T>
class SqList
{
public:
SqList();//构造函数
~SqList();//析构函数
void ClearList();//清空线性表
int ListSize();//返回线性表长度
bool ListEmpty();//若线性表为空,则返回真,否则返回假
T GetElem(int);//返回L中第pos个元素的值,1<=pos<=n,若超出范围,则出错并停止程序
void TraverseList();//遍历L
bool Find(const T&);//从L中查找与item值相等的元素,成功返回真,否则返回假
bool Insert(const T&);//把item的值插满足条件的一定位置
bool Delete(const T&);//删除表L中其值等于item的值,成功返回1,否则返回0
void Sort();//将L中元素按值的升序重新排列
private:
static const int LIST_INIT_SIZE;
static const int LISTINCREMENT;
T *list;
int length;
int listsize;
};

//--------------------------------------------------------------------
template <class T>//定义静态成员变量
const int SqList<T>::LIST_INIT_SIZE = 100;

template <class T>//定义静态成员变量
const int SqList<T>::LISTINCREMENT = 10;

template <class T>//初始化
SqList<T>::SqList()
{
list = new T[LIST_INIT_SIZE];// 分配失败会抛出 std::bad_alloc 异常。或者这样:
/*
if (!(list = new(nothrow) T[LIST_INIT_SIZE]))
throw "Allocation failed";
在构造函数中抛出异常不会造成资源泄露。一个分配失败就 exit 是不是太野蛮了?^_^
*/
length = 0;
listsize = LIST_INIT_SIZE;
}

template <class T>//析构函数
SqList<T>::~SqList()
{
delete[] list;
}

template <class T>//清空线性表
void SqList<T>::ClearList()
{
this->length = 0;
}

template <class T>//返回线性表长度
int SqList<T>::ListSize()
{
return length;
}

template <class T>//若线性表为空,则返回1,否则返回0
bool SqList<T>::ListEmpty()
{
return(length == 0);
}

template <class T>//返回L中第pos个元素的值,1<=pos<=n,若超出范围,则抛出异常
T SqList<T>::GetElem(int pos)
{
if((pos<1) || (pos>length))
{
throw "Index out of bounds"; // 用异常代替野蛮的 exit
}
return list[pos-1];
}

template <class T>//遍历L
void SqList<T>::TraverseList()
{
for(int i = 0;i < length;i++)
cout << list[i] <<" ";
cout << endl;
}

template <class T>//从L中查找与item值相等的元素,成功返回真,否则返回假
bool SqList<T>::Find(const T& item)
{
for(int i = 0 ; i < length; i++)
if(list[i] == item)
return true;
return false;
}

template <class T>//把item的值插入满足条件的一定位置
bool SqList<T>::Insert(const T& item)
{
if(length >= listsize)
{
int *Newbase;
Newbase = (int *)realloc(list,(listsize + LISTINCREMENT) * sizeof(int));
if(!Newbase)
{
cout << "Overflow!" << endl;
getchar();
return false;
}
list = Newbase;
listsize += LISTINCREMENT;
}
int i;
for(i = 0;i < length; i++)
if(item < list[i])
break;
for(int j = length-1; j >= i ;j--)
list[j+1] = list[j];
list[i] = item;
length++;
return true;
}

template <class T> //删除表L中其值等于item的值
bool SqList<T>::Delete(const T& item)
{
int i = 0;
if (length == 0)
{
return false;
}
if (!Find(item))
{
cerr << "Deleted element is not exist!" << endl;
return false;
}
for(; i < length; i++)
if(list[i] == item)
break;
for(int j = i+1; j < length; j++)
list[j-1] = list[j];
length--;
return true;
}

template <class T> //将L中元素按值的升序重新排列
void SqList<T>::Sort()
{
T x;
for(int i = 0 ; i < length-1; i++)
for(int j = 0; j <= i; j++)
if(list[j] > list[j+1])
{
x = list[j+1];
list[j+1] = list[j];
list[j] = x;
}
}

#endif //SqList_CLASS
shifind 2003-11-22
  • 打赏
  • 举报
回复
关注

64,649

社区成员

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

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