资源简介
1.有序顺序表的元素按照从小到大有序存储;
2.实现有序顺序表的类模板,它的操作如下:
a)构造函数;b)拷贝构造函数;c)析构函数; d)计算表长度,并输出; e)定位函数:查找x在表中位置;
f)判断x是否在表中;g) 向表中插入x; h) 删除表的第i个元素;i) 寻找x的后继;
j) 寻找 x 的前驱;k) 判断顺序表空否;l) 判断顺序表满否; m) 重载=;n) 重载下标运算[];
3.用有序顺序表表示集合,实现两个有序顺序表的并和交(并和交仍是有序顺序表)并分析它们的时间复杂度;
代码片段和文件信息
#include
#include
#include
#include
#include
//#include “jd.h“
using namespace std;
string strInPutError = “数据错误请重新输入!“;
template
struct Node
{
Node(T1 valNode* linkf = NULLNode* linkp = NULL)
:_val(val)
{
next = linkf;
back = linkp;
}
T1 _val;
Node * next;
Node * back;
};
template
class CList
{
public:
CList()
{
count = 0;
head = NULL;
}
CList( Node &node )
{
head = &node;
count = 1;
}
CList( CList &li )
{
if ( this != &li )
{
head = li.head;
count = li.count;
}
}
bool is_elem_in( T2 elem );
bool insert( T2 elem );
bool remove( int pos );
bool empty() const { return ( count == 0 );}
T2 back( const T2 val );
T2 next( const T2 val );
int size() const { return (count);};
T2& operator []( int pos );
CList& combine(CList& rhs);
CList& meet(CList& rhs);
protected:
Node* find_pos(T2 val);
Node* find_Node( T2 elem );
Node* elem( int pos );
bool is_size_ok(int pos)
{
return (pos >= 0 && pos < count);
}
private:
Node* head;
int count;
};
template
Node* CList::elem(int pos)
{
if ( !is_size_ok(pos) )
{
return NULL;
}
Node * curr = head;
for ( int i = 0; i < pos; ++i )
{
curr = curr->next;
}
return curr;
}
template
Node* CList::find_Node(T2 elem)
{
Node* curr = head;
for (int i = 0; i < count; ++i )
{
if (curr->_val == elem)
{
return curr;
}
curr = curr->next;
}
return NULL;
};
template
bool CList::is_elem_in(T2 elem)
{
if ( find_Node( elem ))
{
return ( true );
}
else
{
return ( false );
}
}
template
T2 CList::next(T2 val )
{
Node* curr = find_Node(val);
if (curr)
{
return (curr->next)->_val;
}
return val;
}
template
T2 CList::back( T2 val )
{
Node* curr = find_Node(val);
if (curr)
{
return (curr->back)->_val;
}
return val;
}
template
T2& CList::operator []( int pos )
{
Node* curr = elem(pos);
if (curr)
{
return (curr->_val);
}
return head->_val;
}
template
Node* CList::find_pos( T2 val)
{
Node* curr = head;
for (int i = 0; i < count; i++)
{
if ( val > curr->_val && (curr->next == NULL || val < curr->next->_val) )
{
return curr;
}
curr = curr->next;
}
return NULL;
}
template
bool CList::insert( T2 elem )
{
Node* node = new Node(elem);
if (head != NULL && elem < head->_val)
{
node->next = head;
head->back = node;
head = node;
count ++;
return true;
}
if (!count)
{
head = node;
node->back = NULL;
node->next = NULL;
count++;
return true;
}
else
{
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 518 2008-09-21 22:49 clist1\clist1.dsw
文件 9221 2008-09-21 22:49 clist1\clist.cpp
文件 638 2008-09-21 22:51 clist1\clist1.plg
文件 48640 2008-09-21 22:50 clist1\clist1.opt
文件 4283 2008-09-21 22:50 clist1\clist1.dsp
目录 0 2008-09-21 22:49 clist1
----------- --------- ---------- ----- ----
63300 6
评论
共有 条评论