1. 程式人生 > >好記性不如爛筆頭的專欄

好記性不如爛筆頭的專欄

節點定義:

typedef struct node {
    int data;
    struct node *next;
}NODE;

類定義

class LinkList
{
private:
    NODE *head;
public:
    LinkList() { head = NULL; }
    ~LinkList();
    bool clearSqList();
    bool isEmpty() { return head == NULL; }
    int Length();
    void GetElem(int i, int *e);
    int
LocateElem(int e); bool PriorElem(int cur_e, int *pre_e); bool NextElem(int cur_e, int *next_e); bool Insert(int i, int e); bool Delete(int i, int *e); NODE * Reverse(); };

各個函式的作用參見下面的實現註釋

實現

解構函式

//解構函式
LinkList::~LinkList()//和清空一樣
{
    NODE *p = head;
    while (head)
    {
        p = head;
        head = head->next;
        delete
(p); } }

清空函式

bool LinkList::clearSqList()//清空函式,和析構一樣
{
    NODE *p = head;
    while (head)
    {
        p = head;
        head = head->next;
        delete(p);
    }
}

獲取連結串列長度

//獲取連結串列長度
int LinkList::Length()
{
    NODE *p = head;
    int len = 0;
    while (p != NULL)
    {
        len++;
        p = p->next;
    }
    return
len; }

獲取指定位置的元素

bool LinkList::GetElem(int i, int *e)//*e是返回的元素
{
    NODE *p = head;
    int j = 0;
    while (p&&j < i)
    {
        p = p->next;
        j++;
    }
    if (p == NULL) return false;
    *e = p->data;
    return true;
}

查詢元素e在連結串列什麼位置(下標位置,從0開始)

int LinkList::LocateElem(int e)
{
    int i = 0;
    NODE *p = head;
    while (p != NULL)
    {
        if (p->data == e)
            return i;
        else p = p->next;
        i++;
    }
    std::cout << "表中不存在指定元素" << std::endl;
    exit(1);
}

取上一個元素

bool LinkList::PriorElem(int cur_e, int *pre_e)
{
    NODE *p = head;
    if (p->data == cur_e) return false;//是頭結點,不存在上一個元素
    while (p->next != NULL)
    {
        if (p->next->data == cur_e)
        {
            *pre_e = p->data;
            return true;
        }
        else
            p = p->next;
    }
    return false;//遍歷完不存在或者只有一個頭結點

}

取下一個元素

bool LinkList::NextElem(int cur_e, int *next_e)
{
    NODE *p = head;
    if (head == NULL || head->next == NULL) return false;
    while (p->next != NULL)
    {
        if (p->data == cur_e)
        {
            *next_e = p->next->data;
            return true;
        }
        else
            p = p->next;
    }
    return false;
}

在指定位置插入元素e

void LinkList::Insert(int i, int e)
{
    NODE *p = head,*s;
    int j = 0;
    if (i == 0)
    {
        s = (NODE *)new NODE[1];
        s->data = e;
        s->next = p;
        head = s;
        return true;
    }
    while (p&&j < i - 1)
    {
        p = p->next;
        j++;
    }
    if (p == NULL)
        return false;//到隊尾了
    s= (NODE *)new NODE[1];
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
}

//刪除指定位置的元素,並把刪除的元素賦給*e

bool LinkList::Delete(int i, int *e)
{
    NODE *p = head, *s;
    if (p == NULL) return false;
    int j = 0;
    if (i == 0)
    {
        head = head->next;
        *e = p->data;
        delete p;
        p = NULL;
        return true;
    }
    while (p&&j < i - 1)
    {
        j++;
        p = p->next;
    }
    if (p == NULL)
        return false;
    s = p->next;
    p->next = p->next->next;
    *e = s->data;
    delete s;
    s = NULL;
    return true;
}

//反轉一個連結串列

NODE* LinkList::Reverse()
{
    if (head == NULL || head->next == NULL) return head;
    NODE *p = head,*q=head->next,*r;
    head->next = NULL;
    while (q)
    {
        r = q->next;
        q->next = p;
        p = q;
        q = r;
    }
    head = p;
    return head;
}

驗證

#include<iostream>
#include"LinkList.h"
using namespace std;
int main()
{
    int a = 0;
    int *p = &a;
    LinkList li;
    li.Insert(0, 5);
    li.Insert(1, 4);
    li.Insert(2, 12);
    li.Insert(3, 5);
    li.Insert(3, 6);
    li.Insert(1, 7);
    cout <<"連結串列長度"<< li.Length()<<endl;
    cout << "各個元素的值是:";
    for (int i = 0;i < li.Length();i++)//遍歷該連結串列
    {

        if (li.GetElem(i, p))
            cout << *p<<"   ";
    }
    cout << endl;
    cout << "反轉後各個元素的值是:";
    NODE* re_li=li.Reverse();
    while (re_li)
    {
        cout << re_li->data << "   ";
        re_li = re_li->next;
    }
    cout << endl;
}

這裡寫圖片描述