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

头文件:(新加结点类)

#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;
}

c++链表基本操作

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值