1. 程式人生 > >[領卓教育]單鏈表(linklist)的建立(增刪改查)與輸出

[領卓教育]單鏈表(linklist)的建立(增刪改查)與輸出

1.連結串列的建立與輸出
執行結果 : 9 8 7 6 5 4 3 2 1 0

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

typedef struct node
{
    int data ;
    struct node * next ;
}linklist_t;
/*********************建立一個空連結串列***********************/
linklist_t * create_empty_linklist(void)
{
    linklist_t * h = (linklist_t *)malloc(sizeof(linklist_t));
    h->data =  0;
    h->next = NULL;
    return h ;
}

/************************連結串列輸入************************/
int insert_list(linklist_t * h,int value)
{
    linklist_t * p = (linklist_t *)malloc(sizeof(linklist_t)) ;
    p->data = value ;
    p->next = h->next ;
    h->next = p ;//將q接到p的後面
}

/************************連結串列輸出************************/
int show_list(linklist_t * h)
{
    linklist_t * p = h->next ;//表頭不存放資料
    /****************遍歷整個連結串列並列印資料****************/
    while(p!=NULL)
    {
        printf("%4d",p->data) ;
        p = p->next ;
    }
    printf("\n");
    return 0 ;
}

int main(int argc, const char *argv[])
{
    int i ;
    //建立一個空連結串列
    linklist_t * H = create_empty_linklist();
    //迴圈向連結串列寫入資料 0~9
    for(i=0;i<10;i++)
    {
        insert_list(H,i);
    }
    //連結串列的輸出
    show_list(H);// 9 8 7 6 5 4 3 2 1 0
    return 0;
}

2.連結串列增刪改查

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

typedef struct node
{
    int data ;
    struct node * next ;
}linklist_t;
/*********************建立一個空連結串列***********************/
linklist_t * create_empty_linklist(void)
{
    linklist_t * h = (linklist_t *)malloc(sizeof(linklist_t));
    h->data =  0;
    h->next = NULL;
    return h ;
}

/************************連結串列輸入************************/
int insert_list(linklist_t * h,int value)
{
    linklist_t * p = (linklist_t *)malloc(sizeof(linklist_t)) ;
    p->data = value ;
    p->next = h->next ;
    h->next = p ;//將q接到p的後面
}

/************************連結串列輸出************************/
int show_list(linklist_t * h)
{
    linklist_t * p = h->next ;//表頭不存放資料
    /****************遍歷整個連結串列並列印資料****************/
    while(p!=NULL)
    {
        printf("%4d",p->data) ;
        p = p->next ;
    }
    printf("\n");
    return 0 ;
}
/*******************必須找到要刪除節點的前一個節點*********************/
int remove_list(linklist_t * h,int value)
{
    linklist_t * p = h ;
    //linklist_t * q = NULL ;
    /***********遍歷整個連結串列找到要刪除的數***********/
    while(p->next != NULL)
    {
        if(p->next->data == value)
        {
            // 將要刪除節點的前後接到一起
            //q = p->next ; 
            p->next = p->next->next ;
            //free(q);
            break ;
        }
        p = p->next ;
    }
}

/*****改*******/
int modify_list(linklist_t * h ,int old,int new)
{
    linklist_t * p = h->next ;
    while(p != NULL)
    {
        if(p->data == old)
        {
            p->data = new;
            break ;
        }
        p = p->next ;
    }
    return 0 ;
}

/************查詢連結串列中某個元素是否存在***********/
/***************成功返回1,失敗返回0**************/
int search_list(linklist_t * h,int value)
{
    linklist_t * p = h->next ;
    while(p != NULL)
    {
        if(p->data == value)
        {
            return 1 ;
        }
        p = p->next ;
    }
    return 0 ;
}


int main(int argc, const char *argv[])
{
    int i ;
    //建立一個空連結串列
    linklist_t * H = create_empty_linklist();
    //迴圈向連結串列寫入資料 0~9
    for(i=0;i<10;i++)
    {
        insert_list(H,i);
    }
    //連結串列的輸出
    show_list(H);// 9 8 7 6 5 4 3 2 1
    remove_list(H,5);
    show_list(H);

    modify_list(H,0,65);
    show_list(H);
    if(search_list(H,5))
    {
        printf("5 is found\n") ;
    }
    else
    {
        printf("5 is not found\n");
    }

    if(search_list(H,65))
    {
        printf("65 is found\n") ;
    }
    else
    {
        printf("65 is not found\n");
    }

    return 0;
}

3.連結串列逆序

/*******************連結串列逆序*********************/
int reverse_list(linklist_t * h)
{
    linklist_t * p = h->next ;
    linklist_t * q ;
    h->next = NULL ; //斷開與後面連結串列的連線
    while(p != NULL)
    {
        q = p ;
        p = p->next ;
        q->next = h->next ;// 把q->next 賦值為空
        h->next = q ;//讓h->next 儲存q的地址
    }
    return 0 ;
}

/*********逆序輸出*********/
reverse_list(H);
show_list(H);