1. 程式人生 > >數據結構之線性表(鏈表)

數據結構之線性表(鏈表)

位置 鏈表 ont 調用 void 刪除鏈表 個數 urn over

鏈表

1.鏈表的定義:線性表的鏈式存儲結構的特點是用一組任意的存儲單元存儲線性表的數據元素(這組存儲單元可以是

連續的,也可以是不連續的)。因此,為了表示每個數據元素ai與其直接後繼數據元素ai+1之間的邏輯關系,對數據元素

ai來說,除了存儲其本身的信息之外,還需存儲一個指示其直接後繼的信息(即直接後繼的存儲位置)。這兩部分信息組

成數據元素ai的存儲映像,稱為結點。它包括兩個域,其中存儲數據元素信息的域稱為數據域;存儲直接後繼存儲位置的

域稱為指針域。指針域中存儲的信息稱做指針或鏈。n個結點(ai(1<=i<=n)的存儲映像)鏈結成一個鏈表,即為線性表的

鏈式存儲結構。又由於此鏈表的每個結點中只包含一個指針域,故又稱線性鏈表或單鏈表。

2.鏈表的數據結構

#define ElemType int 
typedef struct ListNode
{
    ElemType data;
    ListNode *next;
}ListNode;
typedef struct List
{
    ListNode *first;
    ListNode *last;
    size_t size;
}List;

3.在鏈表中有以下操作:

void InitList(List *list);
bool push_back(List *list, ElemType x);
bool push_front(List *list, ElemType x);
void show(List *list); void pop_back(List *list); bool insert_val(List *list, ElemType x); bool delete_val(List *list, ElemType key); ListNode *find_key(List *list, ElemType key); void clear_list(List *list); void reverse_list(List *list); void sort_list(List *list); void destroy_list(List *list);

以上聲明的方法有:(1)初始化一個鏈表.(2)尾插法向鏈表中添加元素.(3)頭插法向鏈表中添加元素.(4)展示

鏈表

內容.(5)尾部刪除鏈表中的數據元素.(6)按值向鏈表中插入數據元素.(7)按值刪除鏈表中的數據元素.(8)按值查找

鏈表中的數據數據元素.(9)清空鏈表操作.(10)逆序排列鏈表中的數據元素.(11)對鏈表中的數據元素進行排序.(12)

銷毀鏈表.

4.將上面所聲明的方法在LinkList.h的頭文件中進行實現:

#ifndef _LINKLIST_H
#define _LINKLIST_H

#include <iostream>
#include <assert.h>
using namespace std;

#define ElemType int 
typedef struct ListNode
{
    ElemType data;
    ListNode *next;
}ListNode;
typedef struct List
{
    ListNode *first;
    ListNode *last;
    size_t size;
}List;

void InitList(List *list);
bool push_back(List *list, ElemType x);
bool push_front(List *list, ElemType x);
void show(List *list);
void pop_back(List *list);
bool insert_val(List *list, ElemType x);
bool delete_val(List *list, ElemType key);
ListNode *find_key(List *list, ElemType key);
void clear_list(List *list);
void reverse_list(List *list);
void sort_list(List *list);
void destroy_list(List *list);

void InitList(List *list)
{
    list->first = list->last = (ListNode*)malloc(sizeof(ListNode));
    list->last->next = NULL;
    assert(list->first != NULL);
    list->size = 0;
}

bool push_back(List *list, ElemType x)
{
    ListNode *s = (ListNode*)malloc(sizeof(ListNode));
    if (s == NULL)
        return false;
    s->data = x;
    s->next = NULL;
    list->last->next = s;
    list->last = s;
    list->size++;
    return true;
}

bool push_front(List *list, ElemType x)
{
    ListNode *s = (ListNode*)malloc(sizeof(ListNode));
    if (s == NULL)
        return false;
    s->data = x;
    s->next = list->first->next;
    list->first->next = s;
    if (list->size == 0)
        list->last = s;
    list->size++;
    return true;
}

void show(List *list)
{
    ListNode *p = list->first->next;
    while (p != NULL)
    {
        cout << p->data << "->";
        p = p->next;
    }
    cout << "Over." << endl;
}

void pop_back(List *list)
{
    if (list->size == 0)
        return;
    ListNode *p = list->first;
    while (p->next != list->last)
        p = p->next;
    p->next = NULL;
    free(list->last);
    list->last = p;
    list->size--;
}

bool insert_val(List *list, ElemType x)
{
    ListNode *p = list->first;
    while (p->next != NULL && p->next->data < x)
        p = p->next;
    ListNode *s = (ListNode*)malloc(sizeof(ListNode));
    if (s == NULL)
        return false;
    s->data = x;
    s->next = NULL;
    s->next = p->next;
    p->next = s;
    if (p == list->last)
        list->last = s;
    list->size++;
    return true;
}

bool delete_val(List *list, ElemType key)
{
    if (list->size == 0)
        return false;
    ListNode *p = list->first;
    while (p->next != NULL && p->next->data != key)
        p = p->next;
    if (p->next == NULL)
        return false;
    ListNode *q = p->next;
    p->next = q->next;
    if (q == list->last)
        list->last = p;
    free(q);
    list->size--;
    return true;
}

ListNode *find_key(List *list, ElemType key)
{
    if (list->size == 0)
        return false;
    ListNode *p = list->first->next;
    while (p != NULL &&p->data != key)
        p = p->next;
    return p;
}

void clear_list(List *list)
{
    if (list->size == 0)
        return;
    ListNode *p = list->first->next;
    while (p != NULL)
    {
        list->first->next = p->next;
        free(p);
        p = list->first->next;
    }
    list->last = list->first;
    list->size = 0;
}

void reverse_list(List *list)
{
    if (list->size <= 1)
        return;
    ListNode *p = list->first->next;
    ListNode *q = p->next;
    list->last = p;
    list->last->next = NULL;
    while (q != NULL)
    {
        p = q;
        q = p->next;
        p->next = list->first->next;
        list->first->next = p;
    }
}

void sort_list(List *list)
{
    if (list->size <= 1)
        return;
    ListNode *p = list->first->next;
    ListNode *q = p->next;
    list->last = p;
    list->last->next = NULL;
    ListNode *t;
    while (q != NULL)
    {
        p = q;
        q = q->next;
        t = list->first;
        while (t->next != NULL && p->data > t->next->data)
            t = t->next;
        if (t->next = NULL)
            list->last = p;
        p->next = t->next;
        t->next = p;
    }
}

void destroy_list(List *list)
{
    clear_list(list);
    free(list->first);
    list->first = list->last = NULL;
}
#endif

5.將上面所實現的方法在主函數中調用實現:

#include <iostream>
#include "LinkList.h"
using namespace std;

int main()
{
    List mylist;
    InitList(&mylist);

    ListNode *p = NULL;
    ElemType item;
    int pos;
    int select = 1;
    while (select)
    {
        cout << "******************************************" << endl;
        cout << "*[1] push_back       [2] push_front      *" << endl;
        cout << "*[3] Show            [4] pop_back        *" << endl;
        cout << "*[5] insert_val      [6] delete_val      *" << endl;
        cout << "*[7] find_key        [8] reverse_list    *" << endl;
        cout << "*[9] sort_list       [10] clear_list     *" << endl;
        cout << "*[0] quit_system                         *" << endl;
        cout << "******************************************" << endl;
        cout << "請選擇:>";
        cin >> select;
        switch (select)
        {
        case 1:
            cout << "請輸入要插入的數據(-1結束):>";
            while (cin >> item, item != -1)     //逗號表達式
            {
                push_back(&mylist, item);
            }
            break;
        case 2:
            cout << "請輸入要插入的數據(-1結束):>";
            while (cin >> item, item != -1) 
            {
                push_front(&mylist, item);
            }
            break;
        case 3:
            show(&mylist);
            break;
        case 4:
            pop_back(&mylist);
            break;
        case 5:
            cout << "請輸入要插入的值:>";
            cin >> item;
            insert_val(&mylist, item);
            break;
        case 6:
            cout << "請輸入要刪除的值:>";
            cin >> item;
            delete_val(&mylist, item);
            break;
        case 7:
            cout << "請輸入要查找的值:>";
            cin >> item;
            p = find_key(&mylist, item);
            if (p == NULL)
            {
                cout << "要查找的值:" << item << "不存在!" << endl;
            }
            break;
        case 8:
            reverse_list(&mylist);
            break;
        case 9:
            sort_list(&mylist);
            break;
        case 10:
            clear_list(&mylist);
            break;
        }
        system("pause");
        system("cls");
    }
    destroy_list(&mylist);

    return 0;
}

數據結構之線性表(鏈表)