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

C++实现 简单 单链表

2017-09-29 15:03 369 查看
转自: http://blog.csdn.net/wonggonghong/article/details/21527577
  我们首先建立一个<List.h>头文件,声明一个单链表结构:

#include "List.h"

[cpp] view
plain copy

//创建一个单链表结构,包含一些常见的操作  

#ifndef _List_H_  

#define _List_H_  

  

#include <iostream>  

  

struct Node{  

    int element;  //节点存储信息可以根据需要修改!  

    Node* next;  

};  

  

Node* CreateLists(); //创建一个空表,并返回表头  

void DeleteLists(Node* head); //删除表头为head的该链表  

bool IsLast(Node* P);  

Node* Find(int X, Node* head);  

Node* FindPrevious(int X, Node* head);  

void Delete(int X, Node* head);  

void Insert(Node* P, int X); //在节点P后面插入X  

void OutputLists(Node* head); //输出链表中所有元素的值  

  

#endif      

    然后在<List.cpp>文件里实现头文件中的链表操作:

#include "List.cpp"

[cpp] view
plain copy

#include "list.h"  

  

Node* CreateLists()  

{  

    Node* head = new Node;  

    head->next = NULL;  

    return head;  

}  

  

void DeleteLists(Node* head)  

{  

    Node* P = head->next, *temp;  

    head->next = NULL;  

    while(P)  

    {  

        temp = P->next;  

        delete P;  

        P = temp;  

    }  

}  

  

bool IsLast(Node* P)  

{  

    return P->next == NULL;  

}  

  

Node* Find(int X, Node* head)  

{  

    Node* P = head->next;  

    while(P && P->element!=X)  

        P = P->next;  

    return P;  

}  

  

Node* FindPrevious(int X, Node* head)  

{  

    Node* P=head;  

    while(P->next && P->next->element!=X)  

        P=P->next;  

    return P;  

}  

  

void Delete(int X, Node* head)  

{  

    Node* P = FindPrevious(X,head), *temp; //如果没找到X,则返回的是链表最后一项  

    if(P->next)  

    {  

        temp = P->next;  

        P->next = temp->next;  

        delete temp;  

    }  

}  

  

void Insert(Node* P, int X)  

{  

    Node* tempX = new Node;  

    tempX->element = X;  

    tempX->next = P->next;  

    P->next = tempX;  

}  

  

void OutputLists(Node* head)  

{  

    Node* P = head->next;  

    while(P)  

    {  

        std::cout<<P->element<<"   ";  

        P = P->next;  

    }  

    std::cout<<std::endl;  

}  

        最后,我们用一段main代码验证一下正确性:

[cpp] view
plain copy

#include <iostream>  

#include <assert.h>  

#include "list.h"  

  

using namespace std;  

  

int main()  

{  

    int Date[8] = {2,9,5,8,15,32,7,4};  

    Node *head = CreateLists();  

    Node *P = head;  

    for(int i=0;i<8;i++)  

    {  

        Insert(P,Date[i]);  

        P = P->next;  

    }  

    cout<<"打印出链表中所有元素:\n";  

    OutputLists(head);  

    if(IsLast(P))  

        cout<<"该Lists的最后一个节点为:"<<P->element<<endl;  

    else  

        cout<<"P不是最后一个节点!!P等于:"<<P->element<<endl;  

      

    if(Find(15,head))  

        cout<<"Find函数在该Lists中找到了值为15的节点!!!\n";  

    else  

        cout<<"Find函数没找到值为15的节点!!\n";  

  

    cout<<"FindPrevious函数找到节点15的前一个节点为:"<<FindPrevious(15,head)->element<<endl;  

    cout<<"而节点15的前一个节点应该为“8”!\n";  

  

    Delete(8, head);  

    if(Find(8,head))  

        cout<<"Delete(8, head)后,在该Lists中找到了值为8的节点!!!\n";  

    else  

        cout<<"Delete(8, head)后,Find函数没找到值为8的节点!!\n";  

  

    DeleteLists(head);   

    if(head->next)  

        cout<<"DeleteLists函数未成功删除链表!!\n";  

    else  

        cout<<"链表已经为空!DeleteLists函数没有问题!!!\n";  

    return 0;  

}  

结果如下:

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