链表的基本操作大都是按照课本模板,或是上一节的顺序表的基本操作而完成。
根据实验报告上的测试点,进行了简单测试。如果要求完整且有逻辑,还需要进一步改进。
//实验3链表的基本操作
#include<iostream>
using namespace std;
typedef struct LNode
{
int data;
struct LNode *next;
} LNode, *LinkList;
//显示菜单
void xianshi()
{
cout << "可执行操作:" << endl;
cout << "*********************************************" << endl;
cout << "******** 1.初始化或重置链表 ********" << endl;
cout << "******** 2.销毁链表 ********" << endl;
cout << "******** 3.链表中数据元素的个数 ********" << endl;
cout << "******** 4.所指位序的元素值 ********" << endl;
cout << "******** 5.链表已存在元素的位序 ********" << endl;
cout << "******** 6.请输入元素,求直接前驱 ********" << endl;
cout << "******** 7.请输入元素,求直接后继 ********" << endl;
cout << "******** 8.在第i个位置插入元素 ********" << endl;
cout << "******** 9.删除第i个元素 ********" << endl;
cout << "******** 10.输出所输入的链表元素 ********" << endl;
cout << "******** 11.初始化并输入链表元素 ********" << endl;
cout << "******** 12.清空链表 ********" << endl;
cout << "******** 13.判断链表是否为空 ********" << endl;
cout << "******** 14.退出 ********" << endl;
cout << "*********************************************" << endl;
cout << "请输入你的选择:" << endl;
}
//1.初始化或重置链表
bool InitList( LinkList &L )
{
L = new LNode;
L->next = NULL;
return true;
}
//2.销毁链表
bool DestroyList(LinkList &L)
{
LinkList p;
if( !L )
{
return false;
}
while(L)
{
p = L;
L = L->next;
delete p;
}
return true;
}
//3.链表中数据元素的个数
int ListLength( LinkList L )
{
LinkList p;
p = L->next;
int count = 0;
while( p )
{
++count;
p = p->next;
}
return count;
}
//4.所指位序的元素值
bool GetElem( LinkList L, int i, int &e )
{
LinkList p;
p = L->next;
int j = 1;
while( p && j<i )
{
p = p->next;
++j;
}
if( !p || j>i )
{
return false;
}
e = p->data;
return true;
}
//5.链表已存在元素的位序
bool LocateElem( LinkList L, int e )
{
LinkList p;
int j=0;
p = L->next;
while( p && p->data!=e )
{
p = p->next;
j++;
}
j++;
if( j>0 && j <= ListLength(L) )
{
cout << "该元素的位置是:" << j << endl;
return true;
}
else
{
cout << "该元素不存在!" << endl;
return false;
}
}
//6.请输入元素,求直接前驱
bool PriorElem( LinkList L, int e )
{
LinkList p;
p = L->next;
if( p->data == e )
{
cout << "该元素不存在直接前驱!" << endl;
return false;
}
else if( !LocateElem(L, e) )
{
return false;
}
else
{
while( p && p->next->data!=e )
{
p = p->next;
}
cout << "元素" << e << "的直接前驱是:" << p->data << endl;
return true;
}
}
//7.请输入元素,求直接后继
bool NextElem( LinkList L, int e )
{
LinkList p = L->next;
LinkList q = p->next;
while( q )
{
if( p->data == e )
{
cout << "元素" << e << "的直接后继是:" << q->data << endl;
return true;
}
p = p->next;
q = q->next;
}
return false;
}
//8.在第i个位置插入元素
bool ListInsert( LinkList &L, int i, int e )
{
LinkList p;
p = L;
int j = 0;
while( p && (j < i-1) )
{
p = p->next;
j++;
}
if( !p || j >i-1 )
{
return false;
}
LinkList s = new LNode;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
//9.删除第i个元素
bool ListDelete(LinkList &L, int i )
{
if( i<=0 || i>ListLength(L) )
{
cout << "输入位置不合法!" << endl;
return 0;
}
LinkList p = L;
int j = 0;
while( (p->next) && (j<i-1))
{
p = p->next;
++j;
}
if( !(p->next) && (j>i-1))
{
return false;
}
LinkList q;
q = p->next;
p->next = q->next;
delete q;
return true;
}
//10.输出所输入的链表元素
void OutputList( LinkList L )
{
LinkList p = L->next;
while( p->next != NULL )
{
cout << p->data << ' ';
p = p->next;
}
cout << p->data << endl;
cout << endl;
}
//11.初始化并输入链表元素
void CreateList( LinkList &L, int n )
{
L = new LNode;
L->next = NULL;
for( int i=0; i<n; i++ )
{
LinkList p;
p = new LNode;
cin >> p->data;
p->next = L->next;
L->next = p;
}
}
//12.清空链表
bool ClearList( LinkList L )
{
LinkList p, q;
p = L->next;
while( p )
{
q = p->next;
delete p;
p = q;
}
L->next = NULL;
cout << "链表清空成功!" << endl;
return true;
}
//13.判断非空
bool ListEmpty( LinkList L )
{
if( L->next )
{
cout << "链表非空!" << endl;
return false;
}
else
{
cout << "链表为空!" << endl;
return true;
}
}
//14.退出
void exit()
{
cout << "退出成功!" << endl;
}
int main()
{
bool cotinuedo = false;
xianshi();
while( 1 )
{
int n;
cin >> n;
LinkList L;
switch(n)
{
case 1 :
{
if( InitList(L) )
{
cout << "初始化成功!" << endl;
cotinuedo = true;
}
else
{
cout << "初始化失败!" << endl;
}
break;
}//case 1
case 2 :
{
if( cotinuedo )
{
DestroyList( L );
cout << "链表销毁成功!" << endl;
}
else
{
cout << "链表没有初始化,无法进行销毁!" << endl;
}
break;
}//case2
case 3 :
{
if( cotinuedo )
{
cout << "链表中数据元素的个数是:" << ListLength( L ) << endl;
}
else
{
cout << "链表没有初始化,无法输出链表中数据元素的个数!" << endl;
}
break;
}//case 3
case 4 :
{
int i, e;
cout << "请输入所指位置:" << endl;
cin >> i;
if( cotinuedo )
{
if( GetElem( L, i, e) )
{
cout << "所指位序的元素值是:" << e << endl;
}
else
{
cout << "该位置不合法!" << endl;
}
}
else
{
cout << "链表没有初始化,无法求该元素的直接前驱!" << endl;
}
break;
}//case 4
case 5 :
{
int e, j=0;
cout << "请输入想要查找位置的元素:" << endl;
cin >> e;
if( cotinuedo )
{
LocateElem(L, e);
}
else
{
cout << "链表没有初始化,无法查找该元素的位序!" << endl;
}
break;
}//case 5;
case 6 :
{
int e;
cout << "请输入一个元素:" << endl;
cin >> e;
if( cotinuedo )
{
PriorElem( L, e );
}
else
{
cout << "链表没有初始化,无法进行求直接前驱!" << endl;
}
break;
}//case 6
case 7 :
{
int e;
cout << "请输入一个元素:" << endl;
cin >> e;
if( cotinuedo )
{
if ( NextElem( L, e ) )
{
cout << "";
}
else
{
cout << "输入元素没有直接后继或不存在!" << endl;
}
}
else
{
cout << "链表没有初始化,无法进行求直接后继!" << endl;
}
break;
}//case 7
case 8 :
{
int i, e;
cout << "请输入插入的位置:" << endl;
cin >> i;
cout << "请输入插入的元素:" << endl;
cin >> e;
if( cotinuedo )
{
if( ListInsert( L, i, e ) )
{
cout << "元素" << e << "在" << i << "位置插入成功!" << endl;
}
else
{
cout << "元素" << e << "在" << i << "位置插入失败!" << endl;
}
}
else
{
cout << "链表没有初始化,无法进行插入操作!" << endl;
}
break;
}//case 8
case 9 :
{
int i;
cout << "请输入想要删除的元素的位置:" << endl;
cin >> i;
if( cotinuedo )
{
if( ListDelete(L, i) )
{
cout << "位置" << i << "上的元素删除成功!" << endl;
}
else
{
cout << "位置" << i << "上的元素删除失败!" << endl;
}
}
else
{
cout << "链表没有初始化,无法进行删除操作!" << endl;
}
break;
}//case 9
case 10 :
{
if( cotinuedo )
{
cout << "链表中的元素有: " << endl;
OutputList( L );
}
else
{
cout << "链表没有初始化,无法查看链表中的元素!" << endl;
}
break;
}//case 10
case 11 :
{
if( cotinuedo )
{
int n;
cout << "请输入链表元素的个数:" << endl;
cin >> n;
cout << "请输入链表元素:" << endl;
CreateList( L, n );
}
else
{
cout << "链表没有初始化,不能输入链表元素!" << endl;
}
break;
}//case 11
case 12 :
{
if( cotinuedo )
{
ClearList(L);
}
else
{
cout << "链表没有初始化,无法清空!" << endl;
}
break;
}//case 12
case 13 :
{
if(cotinuedo )
{
ListEmpty(L);
}
else
{
cout << "链表没有初始化,无法判空!" << endl;
}
break;
}//case 13
case 14 :
{
if( cotinuedo )
{
exit();
return 0;
}
else
{
cout << "链表没有初始化,无法进行退出操作!" << endl;
}
break;
}//case 14
default:
{
cout << "操作码输入不正确!" << endl;
break;
}//default
}//swithch
}//while
return 0;
}