单链表的基本操作

来源:互联网 发布:江南七怪 知乎 编辑:程序博客网 时间:2024/06/16 17:06

单链表是最基础的线性表。也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了头,尾元素之外,其它数据元素都是首尾相接的。线性表有两种存储方式,一种是顺序存储结构,另一种是链式存储结构。
在链表中,节点又data(数据域)和next(指针域,指向下一个节点的指针)构成;
类模板实现一个单链表和其基本操作:

#include <iostream>using namespace std;template<class T>class ListNode{    ListNode()//初始化    {        next = NULL;    }    T data;//值    ListNode* next;//指向下一个节点的指针};template <class T>//链表的声明,属性,方法class mylist{private:    ListNode<T>*node;    ListNode<T>*pHead;    ListNode<T>*pTail;public:    mylist();//初始化链表    int Size();//节点个数    void PushFront(T x);//头插    void PushBack(T x);//尾插    void insert(T x, ListNode<T>*p);//在P节点后插入值为x的节点    void PopFront(T x);//头删    void PopBack(T x);//尾删    void Print();//打印    void ReversePrint()//逆向打印,递归实现    bool IsEmpty();//判断是否为空    ListNode<T>*find(T X);//查找第一个值为X的节点,成功返回地址,失败返回NULL};template <class T>//初始化链表mylist<T>::mylist(){    node = NULL;    pHead = NULL;    pTail = NULL;    Listlen = 0;};template<class T>int mylist<T>:: Size(){    int count = 0;    while (pHead)    {        pHead = pHead->next;        count++;    };    return count;};template<class T>void mylist<T>::PushFront(T x)//头插{    node = new ListNode<T>();    if (NULL == (pHead))//链表为空,直接返回该节点    {        pHead = node;        return;    }    else    {        node->next = pHead;        pHead = node;    }};template<class T>//尾插void mylist<T>::PushBack(T x){    node = new ListNode<T>();    if (NULL == (pHead))    {        pHead = node;        return;    }    else    {        pTail->next = node;        pTail = node;    }};template <class T>void mylist<T>::insert(T x, ListNode<T>*p)//p 节点之后插{    if (p == NULL) return;    node = new ListNode <T>();    node->data = x;    node->next = p->next;    p->next = node;    if (node->next == NULL)//node 为尾节点    {        pTail = node;    };};template<class T>void mylist<T>::PopFront(T x)//头删{    if (NULL == pHead) return;    else if (NULL == tmp->next)//有一个节点或多个节点    {        ListNode<T>* tmp = pHead;//保存头节点以便删除        pHead = tmp->next;        free(tmp);    };};template <class T>void mylist<T>::PopBack(T x)//尾删{    ListNode<T>* tmp = pHead;//保存头节点以便删除    if (NULL == pHead) return;//链表为空直接返回    else if (NULL == tmp->next)//只有一个节点时,free该节点,链表置空    {        free(pHead);        pHead = NULL;    };    else(pTail = tmp->next)    {        pTail = tmp;//保存尾节点的上一个节点        free(tmp->next);//删除        tmp->next = NULL;    };};template <class T>void mylist<T>::Print()//打印{    node = pHead;//临时节点指向头节点    while (NULL != node)//遍历链表    {        cout << node->data << endl;        node = node->next;    };    cout << endl;};template<class T>void mylist<T>::ReversePrint()//逆打印{    if (NULL == pHead) return;    ReversePrint(pHead->next);    cout << pHead->next << enddl;};template <class>ListNode<T>* mylist<T>::find(T x){    node = pHead;    while ((NULL != node) && (node->data != x))//遍历链表,遇到值相等时跳出循环    {        node = node->next;    };    return node;};
0 0