双向链表

#include<iostream> 
using namespace std; 
template<class T> 
struct Node 

    T data; 
    Node<T> *pNext; 
    Node<T> *pPrior; 
}; 
template<class T> 
class List 

private: 
    Node<T> *pHead; 
    Node<T> *pTail; 
    int length; 
public: 
    List(int length); 
    void traverseList(); 
    void traverseListReturn(); 
    void sortList(); 
    bool insert(int index, T x); 
    bool ChangeList(int postion, int num);  
    void ClearList(); 
    int DeleteList(int postion); 
    int GetList(int postion); 
    ~List(); 
}; 
template<class T> 
List<T>::List(int length) 

    this->length = length; 
    pHead = new Node<T>; 
    pHead->pPrior = NULL; 
    pTail = pHead; 
    for (int i = 0; i < length; i++) 
    { 
        Node<T> *temp = new Node<T>; 
        cout << "Please enter the no." << i + 1 << "Node's data:"; 
        cin >> temp->data; 
        temp->pNext = NULL; 
        temp->pPrior = pTail; 
        pTail->pNext = temp; 
        pTail = temp; 
    } 

template<class T> 
void List<T>::traverseList() 

    Node<T> *p = pHead->pNext; 
    while (p != NULL) 
    { 
        cout << p->data << "  "; 
        p = p->pNext; 
    } 
    cout << endl; 

template<class T> 
void List<T>::traverseListReturn() 

    Node<T>*p = pTail; 
    while (p->pPrior != NULL) 
    { 
        cout << p->data << "  "; 
        p = p->pPrior; 
    } 
    cout << endl; 

template<class T> 
void List<T>::sortList() 

    Node<T> *p = new Node<T>; 
    Node<T> *q = new Node<T>; 
    T temp; 
    for (p = pHead->pNext; p->pNext != NULL; p = p->pNext) 
    { 
        for (q = p->pNext; q != NULL; q = q->pNext) 
        { 
            if (q->data < p->data) 
            { 
                temp = q->data; 
                q->data = p->data; 
                p->data = temp; 
            } 
        } 
    } 

template<class T> 
bool List<T>::insert(int index, T x) 

    if (index<1 || index>length) 
    { 
        return false; 
    } 
    Node<T> *p = new Node<T>; 
    p->data = x; 
    Node<T> *q = pHead; 
    for (int i = 0; i < index; i++) 
    { 
        q = q->pNext; 
    } 
    p->pPrior = q->pPrior; 
    q->pPrior->pNext = p; 
    p->pNext = q; 
    q->pPrior = p; 
    length++; 
    return true; 

template<class T> 
bool List<T>::ChangeList(int postion, int num) 

    Node<T> *p = pHead->pNext; 
    if (postion > length || postion < 1) 
    { 
        return false; 
    } 
    for (int i = 0; i < postion - 1; i++) 
    { 
        p = p->pNext; 
    } 
    p->data = num; 

template<class T> 
void List<T>::ClearList() 

 
    Node<T> *q; 
    Node<T> *p = pHead->pNext; 
    while (p != NULL) 
    { 
        q = p; 
        p = p->pNext; 
        delete q; 
    } 
    p = NULL; 
    q = NULL; 

template<class T> 
int List<T>::DeleteList(int postion) 

    Node<T> *p = pHead->pNext; 
    if (postion > length || postion < 1) 
    { 
        return -1; 
    } 
    for (int i = 0; i < postion - 1; i++) 
    { 
        p = p->pNext; 
    } 
    p->pPrior->pNext = p->pNext; 
    p->pNext->pPrior = p->pPrior; 
    delete p; 
    length--; 

template<class T> 
int List<T>::GetList(int postion) 

    Node<T> *p = pHead->pNext; 
    if (postion > length || postion < 1) 
    { 
        return -1; 
    } 
    for (int i = 0; i < postion - 1; i++) 
    { 
        p = p->pNext; 
    } 
    return p->data; 

template<class T> 
List<T>::~List() 

    Node<T> *q; 
    Node<T> *p = pHead->pNext; 
    while (p != NULL) 
    { 
        q = p; 
        p = p->pNext; 
        delete q; 
    } 
    p = NULL; 
    q = NULL; 

#include"DoubleList.h" 
#include<iostream> 
using namespace std; 
int main(void) 

    List<float> Score(4); 
    Score.traverseList();  
    Score.traverseListReturn();  
    Score.sortList(); 
    Score.traverseList();   
    Score.insert(3, 93); 
    Score.traverseList();   
    Score.ChangeList(2, 99);
    Score.traverseList(); 
    Score.sortList();
    Score.traverseList();
    cout << Score.GetList(2) << endl;
    Score.traverseList(); 
    system("pause"); 
    return 0; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值