[c++]代码库
# include <iostream>
using namespace std;
typedef char ElemType;
typedef struct Node
{ // 结构体节点
ElemType data;
struct Node *next;
}LinkList;
class MyList
{
private:
LinkList* L;
public:
bool InitList() // 初始化链表空间
{
L = new LinkList; // 创建头指针
if ( NULL == L )
{
cout << "Not Have Enough Memory!";
return false;
}
L->next = NULL;
return true;
}
bool CreatList( int site, ElemType e ) // 创建链表
{
int i = 0;
LinkList* p = L;
while ( i<site-1 && p!=NULL )
{
i ++;
p = p->next;
}
if ( NULL == p )
return false;
else
{
LinkList*q = new LinkList;
q->data = e;
p->next = q;
p = q;
q->next = NULL;
}
return true;
}
void DispList() // 输出顺序表
{
LinkList* p = L->next;
while ( p!=NULL )
{
cout << p->data << ' ';
p = p->next;
}
cout << endl;
}
int ListLength() // 求表长
{
int i = 0;
LinkList* p = L->next;
while ( p!=NULL )
{
i ++;
p = p->next;
}
return i;
}
bool ListEmpty() // 判断是否为空表
{
return NULL == L->next;
}
bool GetElem( int site, ElemType &e ) // 求某位置上的元素
{
int i =0;
LinkList* p = L;
while ( i<site && p!=NULL )
{
i ++;
p = p->next;
}
if ( NULL == p || 0 == site)
return false;
else
{
e = p->data;
return true;
}
}
bool LocateElem( int& site, ElemType e ) // 求元素的位置
{
int i = 1;
LinkList* p = L->next;
while ( p!=NULL && p->data!=e )
{
i ++;
p = p->next;
}
if ( NULL == p )
return false;
else
site = i;
return true;
}
bool ListInsert( int site, ElemType e ) // 插入元素
{
int i = 0;
LinkList* p = L;
while ( i<site-1 && p!=NULL )
{
i ++;
p = p->next;
}
if ( NULL == p )
return false;
else
{
LinkList*q = new LinkList;
q->data = e;
q->next = p->next;
p->next = q;
return true;
}
}
bool ListDelete( int site, ElemType &e ) // 删除元素
{
int i = 0;
LinkList* p = L;
while ( i<site-1 && p!=NULL )
{
i++;
p = p->next;
}
if ( NULL == p )
return false;
else
{
LinkList* q = p->next;
if ( NULL == q )
return false;
e = q->data;
p->next = q->next;
delete q;
return true;
}
}
void DestoryList() // 清空链表
{
LinkList*p, *q;
p = L;
while ( p!=NULL )
{
q = p->next;
delete p;
p = q;
}
}
};
int main (void)
{
MyList h;
cout << ">>>>>>>>> InitList Operation <<<<<<<<\n";
h.InitList(); // 初始化链表
int temp;
ElemType e;
cout << "Input The LinkList Length You Want To Creat : ";
cin >> temp;
cout << "Input Element : \n";
cout << ">>>>>>>> CreatList Operation <<<<<<<\n";
for ( int i=1;i <= temp;++i )
{
printf("No.%d : ", i);
cin >> e;
if ( h.CreatList( i, e ) ) // 创建链表
printf("Creat No.%d element success!\n", i);
else
printf("Creat No.%d element failed!\n", i);
cin.sync(); // 清除缓冲区
}
cout << "Now LinkList is : \n";
cout << ">>>>>>>>> DispList Operation <<<<<<<<\n";
h.DispList(); // 输出链表
cout << ">>>>>>>> ListLength Operation <<<<<<<\n";
printf("LinkList Length Is %d.\n", h.ListLength()); // 输出表长
cout << ">>>>>>>> ListEmpty Operation <<<<<<<<\n";
if ( h.ListEmpty() ) // 判断是否是空表
cout << "LinkList Is Empty!\n";
else
cout << "Not a Empty LinkList!\n";
cout << ">>>>>>>>> GetElem Operation <<<<<<<<<\n";
cout << "Input Site To Check Element : ";
cin >> temp;
if ( h.GetElem( temp, e ) ) // 用 e 返回链表中第 temp 个元素
printf("No.%d Element Is %c.\n", temp, e);
else
cout << "Overflow The LinkList!\n";
cout << ">>>>>>>> LocateElem Operation <<<<<<<<\n";
cout << "Input Element To Check Site : ";
cin >> e;
if ( h.LocateElem( temp, e ) ) // 输入元素查看元素所在的位置
printf("%c Exist In No.%d.\n", e, temp);
else
printf("Can't Find %c!\n", e);
cout << ">>>>>>>> ListInsert Operation <<<<<<<<\n";
cout << "Input Site : ";
cin >> temp;
cout << "Input Element : ";
cin >> e;
if ( h.ListInsert( temp, e ) ) // 在 temp 位置插入 e
{
cout << "Insert Success!\nNow LinkList Is : ";
h.DispList();
}
else
cout << "Insert Failed!\n";
cout << ">>>>>>>> ListDelete Operation <<<<<<<<\n";
cout << "Input Delete Site : ";
cin >> temp;
if ( h.ListDelete( temp, e ) ) // 删除 temp 位置的元素
{
printf("Delete %c Success!\nNow LinkList Is : ", e);
h.DispList();
}
else
cout << "Delete Failed!\n";
h.DestoryList(); // 销毁链表
return 0;
}
[代码运行效果截图]
中级设计师
by: 程序猿style 发表于:2012-10-19 12:29:23 顶(0) | 踩(0) 回复
回复评论