}
链表代码如下:
Linkedlist.h
#include
typedefstructLNode
{
chardata;
structLNode*next;
}LNode,*pLinkedList;
classLinkedList
{
private:
pLinkedListm_pList;
intm_listLength;
public:
LinkedList();
~LinkedList();
boolInitList();
boolDestroyList();
boolClearList();
boolIsEmpty();
intGetLength();
boolGetNode(intposition,LNode**node);
intLocateElem(charelem);
boolSetNodeData(intposition,charnewData);
boolGetNodeData(intposition,char&data);
boolInsertNode(intbeforeWhich,chardata);
boolDeleteNode(intposition);
};
Linkedlist.cpp
#include"LinkedList.h"
LinkedList:
:
LinkedList()
{
m_pList=NULL;
m_listLength=0;
InitList();
}
LinkedList:
:
~LinkedList()
{
if(!
DestroyList())
{
DestroyList();
}
}
//初始化,分配一个头节点。
boolLinkedList:
:
InitList()
{
if(!
(m_pList=newLNode))
{
returnfalse;
}
m_pList->next=NULL;
returntrue;
}
//销毁链表。
boolLinkedList:
:
DestroyList()
{
if(!
ClearList())
{
returnfalse;
}
deletem_pList;
returntrue;
}
//判断链表是否为空。
若为空,返回true,否则返回false。
boolLinkedList:
:
IsEmpty()
{
if(m_pList->next==NULL)
{
returntrue;
}
returnfalse;
}
//返回链表的中当前节点数。
intLinkedList:
:
GetLength()
{
returnm_listLength;
}
//将链表清空,释放当前所有节点。
boolLinkedList:
:
ClearList()
{
if(m_pList==NULL)
{
returnfalse;
}
LNode*pTemp=NULL;
while(m_pList->next!
=NULL)
{
pTemp=m_pList->next;
m_pList->next=pTemp->next;
deletepTemp;
}
m_listLength=0;
returntrue;
}
//将position指定的节点内的数据设置为newData。
//第一个有效节点的position为1。
boolLinkedList:
:
SetNodeData(intposition,charnewData)
{
LNode*pTemp=NULL;
if(!
(GetNode(position,&pTemp)))
{
returnfalse;
}
pTemp->data=newData;
returntrue;
}
//得到指定位置节点的数据。
//节点索引从1到listLength。
boolLinkedList:
:
GetNodeData(intposition,char&data)
{
LNode*pTemp=NULL;
if(!
(GetNode(position,&pTemp)))
{
returnfalse;
}
data=pTemp->data;
returntrue;
}
//在链表中插入一个节点。
//插入的位置由beforeWhich指定,新节点插入在beforeWhich之前。
//beforeWhich的取值在1到ListLength+1之间。
boolLinkedList:
:
InsertNode(intbeforeWhich,chardata)
{
LNode*pTemp=NULL;
if(beforeWhich<1||beforeWhich>(m_listLength+1))
{
returnfalse;
}
if(!
(GetNode(beforeWhich-1,&pTemp)))
{
returnfalse;
}
LNode*newNode=newLNode;
newNode->data=data;
newNode->next=pTemp->next;
pTemp->next=newNode;
m_listLength++;
returntrue;
}
//删除一个指定的节点。
//节点位置由position指定。
//positon的值从1到listLength。
//若链表为空或指定的节点不存在则返回false。
boolLinkedList:
:
DeleteNode(intposition)
{
if(position<1||position>m_listLength)
{
returnfalse;
}
LNode*pTemp=NULL;
if(!
(GetNode(position-1,&pTemp)))
{
returnfalse;
}
LNode*pDel=NULL;
pDel=pTemp->next;
pTemp->next=pDel->next;
deletepDel;
m_listLength--;
returntrue;
}
//得到指定位置节点的指针。
boolLinkedList:
:
GetNode(intposition,LNode**node)
{
LNode*pTemp=NULL;
intcurPos=-1;
pTemp=m_pList;
while(pTemp!
=NULL)
{
curPos++;
if(curPos==position)
break;
pTemp=pTemp->next;
}
if(curPos!
=position)
{
returnfalse;
}
*node=pTemp;
returntrue;
}
//定位与指定数据相等的数据节点。
//如果在当前链表中已经存在该数据则返回该数据节点的索引号。
//若不存在这样的节点则返回0。
//节点索引从0开始到listLength。
intLinkedList:
:
LocateElem(charelem)
{
LNode*pTemp=NULL;
intcurIndex=1;
pTemp=m_pList->next;
while((pTemp!
=NULL)&&(pTemp->data!
=elem))
{
pTemp=pTemp->next;
curIndex++;
}
if(pTemp==NULL)
{
return0;
}
returncurIndex;
}
Main.cpp
#include"LinkedList.h"
intmain()
{
LinkedListl;
charx;
cin>>x;
while(x!
='#')
{
l.InsertNode(l.GetLength()+1,x);
cin>>x;
}
/*
l.InsertNode(1,'a');
l.InsertNode(2,'b');
l.InsertNode(3,'c');
l.InsertNode(4,'d');
*/
cout<<"链表长度:
"<if(l.IsEmpty()==false)
cout<<"链表是不为空"<elsecout<<"链表是为空"<chargetData;
l.GetNodeData(3,getData);
cout<<"链表的第三个元素:
"<cout<<"元素a的位置为:
"<chardataTemp=0;
for(inti=1;i<=l.GetLength();i++)
{
l.GetNodeData(i,dataTemp);
cout<}
if(l.SetNodeData(3,'c'))
{
cout<<"DONE\n";
}
else
{
cout<<"Failed\n";
}
for(i=1;i<=l.GetLength();i++)
{
l.GetNodeData(i,dataTemp);
cout<}
if(l.InsertNode(4,'f'))
{
cout<<"在4号为插入元素f:
DONE\n";
}
else
{
cout<<"Failed\n";
}
for(i=1;i<=l.GetLength();i++)
{
l.GetNodeData(i,dataTemp);
cout<}
if(l.DeleteNode(3))
{
cout<<"删除第三号元素:
DONE\n";
}
else
{
cout<<"Failed\n";
}
for(i=1;i<=l.GetLength();i++)
{
l.GetNodeData(i,dataTemp);
cout<}
cout<<"'a'所在的位置为:
"<return0;
}
实验截图: