數據結構——鏈表(頭指針、頭結點)

頭指針鏈表:

#include <stdio.h>
#include <stdlib.h>

#define TRUE   1
#define FALSE  0


typedef int LinkData;    // 鏈表的數據類型
typedef struct _node
{
    LinkData data;       // 鏈表的數據
    struct _node *next;  // 指向鏈表下一個結點的指針
}Node;

// 鏈表的頭插
int Insert_Head(Node **h, LinkData data)
{
    if (h == NULL)
        return FALSE;

    // 創建新節點
    Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }

    // 給結點成員變量賦值
    node->data = data;
    node->next = *h;

    // 讓新節點變爲鏈表的第一個結點
    *h = node;

    return TRUE;
}


// 尾插
int Insert_Last(Node **h, LinkData data)
{
    if (h == NULL)
    {
        return FALSE;
    }

    // 創建新節點
    Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }

    // 給結點成員變量賦值
    node->data = data;
    node->next = NULL;

    // 找最後一個結點
    Node * tmp = *h;  // 指向第一個結點
    if (tmp == NULL)  // 空表
    {
        *h = node;
    }
    else
    {
        while (tmp->next)
        {
            tmp = tmp->next;
        }

        tmp->next = node;
    }

    return TRUE;
}


// 在第 pos 個節點處插入數據,鏈表結點從1開始,沒有第0個結點
int Insert_Pos (Node** h, int pos, LinkData data)
{
    if (h == NULL || pos < 1)
        return FALSE;

    // 創建新節點
    Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }

    // 給結點成員變量賦值
    node->data = data;


    // 空表的狀態下,只能插入在第一個結點處
    if (*h == NULL)  
    {
        if (pos != 1)
        {
            printf ("當前爲空表,無法在第 %d 結點處插入數據\n", pos);
            free(node);
            return FALSE;
        }
        node->next = NULL;
        *h = node;
    }
    else  // 非空表,需要找到插入位置的前一個結點
    {
        if (pos == 1)
        {
            node->next = *h;
            *h = node;
        }
        else
        {
            int i;
            Node *tmp = *h;     // tmp 開始的時候指向第一個結點
            for (i = 0; i < pos-2; i++)
            {
                if (tmp == NULL)  // 如果 pos 太大,會造成越界
                    break;
                tmp = tmp->next;
            }

            if (tmp == NULL)
            {
                printf ("插入位置越界\n");
                free(node);
                return FALSE;
            }

            node->next = tmp->next;
            tmp->next = node;
        }
    }

    return TRUE;
}

int Delete_Pos(Node** h, int pos)
{
    if (h == NULL || *h == NULL || pos < 1)
        return FALSE;

    Node *tmp = *h;
    if (pos == 1)
    {
        *h = tmp->next;
        free(tmp);
    }
    else
    {
        int i;
        for (i = 0; i < pos-2; i++)
        {
            if (tmp->next == NULL)  // 如果 pos 太大,會造成越界
                break;
            tmp = tmp->next;
        }

        if (tmp->next == NULL)
        {
            printf ("刪除位置越界\n");
            return FALSE;
        }
        Node* p = tmp->next;
        tmp->next = p->next;
        free(p);
    }


    return TRUE;
}

int Reverse_List(Node **h)
{
    // *h == NULL 空表 (*h)->next == NULL 只有一個元素
    if (h == NULL || *h == NULL || (*h)->next == NULL)
        return FALSE;

    Node *pre = *h;
    Node *cur = (*h)->next;
    Node *tmp;

    while (cur)
    {
        tmp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = tmp;
    }

    (*h)->next = NULL;
    *h = pre;

    return TRUE;
}


void Display(Node *h)
{
    if (h == NULL)
        return;
    int count = 0;
    while (h)
    {
        if (count++ % 4 == 0)
            printf ("\n");
        printf ("%8d", h->data);

        h = h->next;
    }
    printf ("\n");
}

int main()
{
    Node * head = NULL;   // 指向鏈表第一個結點的指針(頭指針)

    // 插入元素
    int i;

    for (i = 0; i < 10; i++)
    {
        //Insert_Head(&head, i);
        Insert_Last(&head, i);
    }
#if 0
    Insert_Pos(&head, 1, 1000);
    Insert_Pos(&head, 10, 2000);
    Insert_Pos(&head, 13, 3000);
    Insert_Pos(&head, 15, 3000);
    Insert_Pos(&head, 0, 1000); 
#endif
    //Delete_Pos(&head, 11);
    Display(head);

    Reverse_List(&head);
    Display(head);

    return 0;
}


頭結點鏈表:

頭文件:

#ifndef __LINKLIST_H__
#define __LINKLIST_H__

#define FALSE 0
#define TRUE  1

typedef int LinkData;
typedef struct _node
{
    LinkData data;
    struct _node * next;
}Node;

// 創建鏈表
Node * Create_List();

// 尾插
int Insert_Last(Node *h, LinkData data);

// 頭插
int Insert_Head(Node *h, LinkData data);

// 在第 pos 個結點處插入數據
int Insert_Pos(Node *h, int pos, LinkData data);

//刪除 第 pos 個結點
int Delete_Pos(Node* h, int pos);

// 逆序
int Reverse_List(Node *head);

// 刪除指定數據
int Delete_Data(Node* h, LinkData data);

// 查找元素:如果有, 返回元素的位置
int Find_Element(Node* h, LinkData data, int *x);

// 獲取順序表中的元素:通過位置獲取
int Get_Element(Node* s, int pos, int *x);

int Get_Len(Node * head);

// 清空所有結點
int Clean_List(Node * head);

// 銷燬鏈表
int Destroy(Node *);

void Display(Node *h);
#endif 

功能函數:

#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
Node * Create_List()
{
    Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (list == NULL)
        return NULL;

    list->next = NULL;   // 空表

    return list;
}

int Insert_Head(Node *h, LinkData data)
{
    if (h == NULL)
        return FALSE;
    Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }

    node->data = data;
    node->next = h->next;

    h->next = node;

    return TRUE;
}

int Insert_Last(Node *h, LinkData data)
{
    if (h == NULL)
        return FALSE;

    Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }
    node->data = data;
    node->next = NULL;

    Node* tmp = h;
    while (tmp->next)
    {
        tmp = tmp->next;
    }

    tmp->next = node;

    return TRUE;
}

int Insert_Pos(Node *h, int pos, LinkData data)
{
    if (h == NULL || pos < 1)
        return FALSE;

    // 找要插入位置的前一個結點
    Node *tmp = h;
    int i;
    for (i = 0; i < pos-1; i++)
    {
        if (tmp == NULL)
            break;
        tmp = tmp->next;
    }

    if (tmp == NULL)   // 越界
    {
        printf("插入位置越界\n");
        return FALSE;
    }

    Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
    if (node == NULL)
    {
        return FALSE;
    }
    node->data = data;
    node->next = tmp->next;
    tmp->next  = node;

    return TRUE;
}

int Delete_Pos(Node* h, int pos)
{
    if (h == NULL || pos < 1)
        return FALSE;

    // 找要插入位置的前一個結點
    Node *tmp = h;
    int i;
    for (i = 0; i < pos-1; i++)
    {
        if (tmp->next == NULL)
            break;
        tmp = tmp->next;
    }

    if (tmp->next == NULL)   // 越界
    {
        printf("刪除位置越界\n");
        return FALSE;
    }

    Node *p = tmp->next;
    tmp->next = p->next;
    free(p);

    return TRUE;
}

int Reverse_List(Node *h)
{
    // h->next 空表
    // h->next->next 只有一個結點
    if (h == NULL || h->next == NULL || h->next->next == NULL)
        return FALSE;

    Node *pre = h->next;
    Node *cur = h->next->next;
    Node *tmp;

    while (cur)
    {
        tmp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = tmp;
    }

    h->next->next = NULL;
    h->next = pre;

    return TRUE;
}

int Delete_Data(Node* h, LinkData data)
{
    if (h == NULL)
        return FALSE;

    Node *tmp = h;
    while (tmp->next)
    {
        if (tmp->next->data == data)
            break;
        tmp = tmp->next;
    }

    if (tmp->next == NULL)
        return FALSE;

    Node *p = tmp->next;
    tmp->next = p->next;
    free(p);

    return TRUE;
}

int Find_Element(Node* h, LinkData data, int *x)
{
    if (h == NULL)
        return FALSE;

    Node *tmp = h->next;
    int k = 1;
    while (tmp)
    {
        if (tmp->data == data)
        {
            *x = k;
            return TRUE;
        }
        k++;
        tmp = tmp->next;
    }

    return FALSE;
}

int Get_Element(Node* h, int pos, int *x)
{
    if (h == NULL || pos < 1)
        return FALSE;

    int i;
    Node *tmp = h;
    for (i = 0; i < pos; i++)
    {
        if (tmp == NULL)
            break;
        tmp = tmp->next;
    }

    if (tmp == NULL)
        return FALSE;
    else
        *x = tmp->data;

    return TRUE;
}

int Get_Len(Node * h)
{
    if (h == NULL)
        return 0;

    Node *tmp = h;
    int count = 0;
    while (tmp->next)
    {
        count++;
        tmp = tmp->next;
    }

    return count;
}

int Clean_List(Node * h)
{
    if (h == NULL)
        return FALSE;

    Node *tmp = h;
    while (tmp->next)
    {
        Delete_Pos(h, 1);
    }

    return 0;
}


void Display(Node *h)
{
    if (h == NULL)
        return;

    int count = 0;
    Node *tmp = h->next;
    while (tmp)
    {
        if (count++ % 4 == 0)
            printf ("\n");
        printf ("%8d", tmp->data);

        tmp = tmp->next;
    }
    printf ("\n");
}



int Destroy(Node *h)
{
    if (h == NULL)
        return FALSE;
    Clean_List(h);
    free(h);

    return TRUE;
}

main函數:

#include <stdio.h>
#include "LinkList.h"

int main()
{
    // 創建鏈表
    Node* head = Create_List();
    if (head == NULL)
    {
        printf("創建鏈表失敗\n");
        return -1;
    }


    int i;
    for (i = 0; i < 10; i++)
    {
        Insert_Head(head, i);
    }
#if 0       
    for (i = 0; i < 10; i++)
    {
        Insert_Head(head, i);
    }

    Insert_Pos(head, 5, 1000);
    Insert_Pos(head, 1, 2000);
    Insert_Pos(head, 22, 2000);
    Insert_Pos(head, 24, 2000);
    Insert_Pos(head, 26, 2000);
    Delete_Pos (head, 21);

    Reverse_List(head);
#endif
    Delete_Data(head, 0);

    int x;
    Find_Element(head, 7, &x);
    Get_Element(head, 8, &x);
    printf ("%d\n", x);

    printf ("%d\n", Get_Len(head));
    Display(head);
    Clean_List(head);
    printf ("清空後:\n");
    Display(head);

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