您的位置:首页 > 编程语言 > C语言/C++

c++实现链表的基本操作

2017-06-22 23:43 525 查看
头文件:(新加结点类)

#ifndef LIST_H
#define LIST_H

class Node{
public:
int data;
Node *next;
void printNode();
};

class List{
public:
List();
~List();
void clearList();
bool isEmpty();
int ListLength();
bool getElem(int i,Node *pNode);
int locateElem(Node *pNode);
bool priorElem(Node *pCurrentNode,Node *pPreNode);
bool nextElem(Node *pCurrentNode,Node *pNextNode);
void ListTraverse();
bool listInsert(int i,Node *pNode);
bool listDelete(int i,Node *pNode);
bool listInsertHead(Node *pNode);
bool listInsertTail(Node *pNode);
private:
Node *m_pList;
int m_iLength;
};
#endif


基本代码实现:

#include "List.h"
#include <iostream>
using namespace std;

void Node::printNode(){
cout<<data<<endl;
}

List::List(){
m_pList=new Node;
m_pList->data=0;
m_pList->next=NULL;
m_iLength=0;
}
bool List::isEmpty(){
return m_iLength==0?true:false;
}
int List::ListLength(){
return m_iLength;
}
void List::clearList(){
Node *currentNode=m_pList->next;
while(currentNode->next!=NULL){
Node *temp=currentNode->next;
delete currentNode;
currentNode=temp;
}
m_pList->next=NULL;
m_iLength=0;
}
List::~List(){
clearList();
delete m_pList;
m_pList=NULL;
}
bool List::listInsertHead(Node *pNode){
Node *temp=m_pList->next;
Node *newNode=new Node;
if (newNode== NULL)
{return false;
}
newNode->data=pNode->data;
m_pList->next=newNode;
newNode->next=temp;
m_iLength++;
return true;
}
bool List::listInsertTail(Node *pNode){
Node *currentNode=m_pList;
while(currentNode->next!=NULL){
currentNode=currentNode->next;
}
Node *newNode=new Node;
if (newNode==NULL)
{
return false;
}
newNode->data=pNode->data;
newNode->next=NULL;
currentNode->next=newNode;
m_iLength++;
return true;
}
bool List::listInsert(int i,Node *pNode){
if (i<0||i>m_iLength)
{
return false;
}
Node *currentNode=m_pList;
for (int k=0;k<i;k++)
{
currentNode=currentNode->next;
}
Node *newNode=new Node;
if (newNode==NULL)
{
return false;
}
newNode->data=pNode->data;
newNode->next=currentNode->next;
currentNode->next=newNode;
m_iLength++;
return true;
}
bool List::listDelete(int i,Node *pNode){
if (i<0||i>=m_iLength)
{
return false;
}
Node *currentNode=m_pList;
Node *currentNodeBef=NULL;
for (int k=0;k<=i;k++)
{
currentNodeBef=currentNode;
currentNode=currentNode->next;
}
currentNodeBef->next=currentNode->next;
pNode->data=currentNode->data;
delete currentNode;
currentNode=NULL;
m_iLength--;
return true;
}
int List::locateElem(Node *pNode){
Node *currentNode=m_pList;
int loc=0;
while(currentNode->next!=NULL){
currentNode=currentNode->next;
if (currentNode->data==pNode->data)
{
return loc;
}
loc++;
}
return -1;

}
bool List::getElem(int i,Node *pNode){
if (i<0||i>=m_iLength)
{
return false;
}
Node *currentNode=m_pList;
for (int k=0;k<=i;k++)
{
currentNode=currentNode->next;
}
pNode->data=currentNode->data;
return true;
}
bool List::priorElem(Node *pCurrentNode,Node *pPreNode){
Node *currentNode=m_pList;
Node *tempNode=NULL;
while(currentNode->next!=NULL){
tempNode=currentNode;
currentNode=currentNode->next;
if (currentNode->data==pCurrentNode->data)
{
if (tempNode==m_pList)
{return false;
}
pPreNode->data=tempNode->data;
return true;
}
}
return false;
}
bool List::nextElem(Node *pCurrentNode,Node *pNextNode){
Node *currentNode=m_pList;
while(currentNode->next!=NULL){
currentNode=currentNode->next;
if (currentNode->data==pCurrentNode->data)
{
if (currentNode->next==NULL)
{
return false;
}
pNextNode->data=currentNode->next->data;
return true;
}
}
return false;
}
void List::ListTraverse(){
Node *currentNode=m_pList;
while(currentNode->next!=NULL){
currentNode=currentNode->next;
currentNode->printNode();
}
}


效果:

#include <iostream>
#include "List.h"

using namespace std;
int main(){
Node e1;
Node e2;
Node e3;
Node e4;
Node e5;
List *p=new List();
e1.data=5;
e2.data=9;
e3.data=4;
e4.data=6;
e5.data=8;
Node e;
e.data=24;

p->listInsertHead(&e1);
p->listInsertHead(&e2);
p->listInsertTail(&e3);
p->listInsertTail(&e4);
p->listInsertHead(&e5);
p->ListTraverse();
cout<<endl;
p->listInsert(3,&e);
p->ListTraverse();
cout<<endl;
p->listDelete(2,&e);
cout<<"删除:"<<e.data<<endl;
p->ListTraverse();

cout<<"表元素个数:"<<p->ListLength()<<endl;
cout<<endl;
p->getElem(5,&e);
e.printNode();
cout<<endl;

cout<<p->locateElem(&e1)<<endl;
cout<<p->locateElem(&e2)<<endl;
p->nextElem(&e2,&e);
cout<<"e2的后继"<<e.data<<endl;
p->priorElem(&e2,&e);
cout<<"e2的前驱"<<e.data<<endl<<endl;

p->clearList();
if(p->isEmpty())
cout<<"Empty"<<endl;
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息