不使用結構體實現單鏈表

題目

利用《數據結構》知識,定義一元素爲整數的單鏈表類,並在主函數中調用各個功能:
數據成員:指向頭結點的指針、鏈表中當前元素的個數;
成員函數:初始化、在尾部增加一元素、查詢指定數據的元素、在指定元素前插入一新元素、刪除指定元素、遍歷鏈表中的元素、輸出所有鏈表中的元素、將一個單鏈表逆序(選作)。

原理

類的成員可以是其他類的對象,稱爲類的組合。不能以類自身的對象作爲本類的成員。但是可以是本身的指針或者引用。

#include<iostream>
using namespace std;
class Link
{
private:
    Link* next;
    Link* head;
    int x;
    int n;
public:
    void init();
    void setLink(int a[], int n);
    int getNum();
    void insertLast(int data);
    void findByValue(int value);
    void findByLocation(int location);
    void insertValue(int i, int data);
    void deleteByValue(int value);
    void deleteByLocation(int location);
    int traverse();
    void print();
    void reverseLink();
};
void Link::init()
{
    head=new Link;
    head->next=NULL;
}
void Link::setLink(int a[], int n)
{
    Link* r, * s;
    head = new Link;
    r = head;
    for (int i = 0; i < n; i++)
    {
        s = new Link;
        s->x = a[i];
        r->next = s;
        r = s;
    }
    r->next = NULL;
    this->n=n;
}
int Link::getNum()
{
    return n;
}
void Link::insertLast(int data)
{
    Link* r, * s, * p;
    p = head;
    while (p->next != NULL)
    {
        p = p->next;
    }
    r = p;
    s = new Link;
    s->x = data;
    r->next = s;
    r = s;
    r->next = NULL;
    n++;
}
void Link::insertValue(int i, int data)
{
    Link* p;
    int j;
    p = head;
    j = 0;
    while (p && j < i - 1)
    {
        p = p->next;
        j++;
    }
    if (!p)
        cout << "error";
    else
    {
        Link* s;
        s = new Link;
        s->x = data;
        s->next = p->next;
        p->next = s;
    }
    n++;
}
void Link::findByValue(int value)
{
    Link* p, * q;
    p = head->next;
    int k = 1;
    while (p != NULL && value != p->x)
    {
        p = p->next;
        k++;
    }
    if (p == NULL )
        cout << "error";
    else
    {
        cout << k<<" ";
        cout << endl;
    }
}
void Link::findByLocation(int location)
{
    Link* p, * q;
    p = head;
    int j = 0;
    while (p != NULL && j < location - 1)
    {
        p = p->next;
        j++;
    }
    if (location <= 0 || p == NULL)
        cout << "error";
    else
    {
        cout << p->next->x<<" ";
        cout << endl;
    }
}
void Link::deleteByLocation(int location)
{
    Link* p, * q;
    p = head;
    int j = 0;
    while (p != NULL && j < location - 1)
    {
        p = p->next;
        j++;
    }
    if (location <= 0 || p == NULL)
        cout << "error";
    else
    {
        q = p->next;
        p->next = q->next;
        delete q;
        n--;
        print();
    }
}
void Link::deleteByValue(int value)
{
    Link* p, * q;
    p = head->next;
    int k = 1;
    while (p != NULL && value != p->x)
    {
        p = p->next;
        k++;
    }
    if (p == NULL || p->next == NULL)
        cout << "error";
    else
    {
        int j = 0;
        p = head;
        while (p != NULL && j < k - 1)
        {
            p = p->next;
            j++;
        }
        q = p->next;
        p->next = q->next;
        delete q;
        n--;
        print();
    }
}
int Link::traverse()
{
    Link* p;
    int count = 0;
    p = head->next;
    while (p)
    {
        count++;
        p = p->next;
    }
    n=count;
}
void Link::print()
{
    Link* p;
    p = head->next;
    while (p)
    {
        cout << p->x << " ";
        p = p->next;
    }
    cout << endl;
}
void Link::reverseLink()
{
    Link* p, * q;
    p = head->next;
    if (!p)
        cout<<"error";
    else
    {
        head->next = NULL;
        while (p)
        {
            q = p->next;
            p->next =head->next;
            head->next = p;
            p = q;
        }
    }
}
int main()
{
    int array[10];
    for (int i = 0; i < 10; i++)
    {
        array[i] = 2 * i - 1;
    }
    Link* l = new Link;
    cout<<"初始化鏈表,輸出元素個數"<<endl;
    l->init();
    cout<<l->getNum()<<endl;
    cout<<"利用自己生成的數組來給鏈表賦值"<<endl;
    l->setLink(array, 10);
    l->print();
    cout<<"在鏈表的最後插入100"<<endl;
    l->insertLast(100);
    l->print();
    cout<<"按位置查找,查詢第四個元素,返回值"<<endl;
    l->findByLocation(4);
    l->print();
    cout<<"按值查找,查詢100元素的位置,返回位置"<<endl;
    l->findByValue(100);
    l->print();
    cout<<"在第五個位置插入200"<<endl;
    l->insertValue(5,200);
    l->print();
    cout<<"輸出當前數據成員n的值"<<endl;
    cout<<l->getNum()<<endl;
    cout<<"按位置刪除,刪除第二個元素"<<endl;
    l->deleteByLocation(2);
    cout<<"按值刪除,刪除值爲200的元素"<<endl;
    l->deleteByValue(200);
    cout<<"利用get函數獲取數據成員"<<endl;
    cout<<l->getNum()<<endl;
    cout << "------逆序------"<<endl;
    l->reverseLink();
    cout<<"遍歷鏈表,輸出整個鏈表的元素個數"<<endl;
    cout<<l->getNum()<<endl;
    l->print();
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章