1. 程式人生 > >單向連結串列的基本操作(頭插,尾插,刪除)

單向連結串列的基本操作(頭插,尾插,刪除)

由於最近剛寫完火車票的管理系統,裡面大量的用到了連結串列的部分,所以在這裡總結一下連結串列的幾種基本操作。

連結串列是什麼

要用連結串列首先要知道連結串列是什麼。簡單的說連結串列就是一串儲存資料的結構。說到這我們一定都會想到陣列,陣列也可以儲存資料,但是它儲存的資料在記憶體中是連續的,而連結串列儲存的資料是分散的,它由每一個結點構成,並由指標將其連線起來,所以每個結點中就包含了資料域和指標域。

連結串列的建立

建立一個連結串列我們需要一個頭結點,後要為頭結點申請記憶體空間,這樣就方便了連結串列的操作,以後在查詢或者增加等等操作時,都不用再判斷哪個結點是連結串列的第一個結點。下面是建立連結串列的程式碼。

struct node *creat()
{
    struct node *phead;
    phead=(struct node *)malloc(sizeof(struct node));
    phead->next=NULL;
    return phead;
}

連結串列的操作需要結構體,下面是講解例子的結構體

struct node
{
    int num;//構成連結串列的結點的資料(資料域)
    struct node *next;//連結各個結點的指標(指標域)
};

頭插法

頭插法顧名思義就是從連結串列的頭開始放,即有新的結點就把此結點放在頭結點的後面,下面是頭插法的程式碼:

void head_insert(struct node *phead)
{
    struct node *p;
    p=(struct node *phead)malloc(sizeof(struct node));
    p->next=phead->next;
    p=phead->next;
}

尾插法

尾插法顧名思義就是將新生成的結點發到最尾部,下面是程式碼例子:

void tail_insert(struct node *phead)
{
    struct node *p,*t;
    t=phead;
    p=(struct
node *)malloc(sizeof(struct node)); while(t->next!=NULL) t=t->next;//找到最後一個結點 t->next=p; p->next=NULL; }

刪除

下面是程式碼例子:

void delete(struct node *phead,int value)
{
    struct *p,*t;
    t=phead;
    while(t->next!=NULL)
    {
        if(t->next->num==value)
        {
            p=t->next;
            t->next=p->next;
            free(p);
        }
        else
            t=t->next;
    }

以上就是連結串列的頭插 尾插 刪除的操作,下面是一整段的程式碼,將三者寫到一個程式中

#include <stdio.h>
typedef struct node
{
    int data;
    struct node *next;
}Linklist;

Linklist *creat()
{
    Linklist *phead;
    phead=(Linklist *)malloc(sizeof(Linklist));
    phead->next=NULL;
    return phead;
}
void head_insert(Linklist *phead,int n)//頭插法
{
    Linklist *p;
    int i;

    for(i=0;i<n;i++)
    {
        p=(Linklist *)malloc(sizeof(Linklist));
        scanf("%d",&p->data);

        p->next=phead->next;
        phead->next=p;
    }
}

void tail_insert(Linklist *phead,int value)//尾插法
{
    Linklist *p,*t;
    int i;
    t=phead;

    p=(Linklist *)malloc(sizeof(Linklist));
    p->data=value;
    while(t->next!=NULL)
        t=t->next;

    t->next=p;
    p->next=NULL; 
}

void print(Linklist *phead)//列印連結串列
{
    Linklist *p;
    p=phead->next;
    if(p==NULL)
        printf("此連結串列為空.\n");
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}

void delete(Linklist *phead,int value)//刪除結點
{
    Linklist *p,*t;
    t=phead;
    while(t->next!=NULL)
    {
        if(t->next->data==value)
        {
            p=t->next;
            t->next=p->next;
            free(p);
        }
        else
            t=t->next;
    }
}
int main(void)
{
    Linklist *phead;
    int value,num;
    phead=creat();

    head_insert(phead,5);
    print(phead);

    printf("請輸入你想插入的數字:\n");
    scanf("%d",&value);
    tail_insert(phead,value);
    print(phead);

    printf("請輸入你想刪除的數字:\n");
    scanf("%d",&num);
    delete(phead,num);
    print(phead);
}

連結串列這個東西剛開始學習的時候是好理解的,但是程式碼就需要多敲多練,才能熟練掌握與應用。連結串列的操作還有翻轉和將其資料排序,將在我的 下一篇部落格中寫道,第一篇部落格一定存在很多問題看到的朋友有自己見解的隨時提出,我一定會虛心學習並且改進的☺☺☺