1. 程式人生 > >帶頭結點的連結串列和不帶頭結點的連結串列

帶頭結點的連結串列和不帶頭結點的連結串列

    湊合著看著吧。當初大二上課的時候老師講的時候糊弄過去了,現在想複習一下。看點別人的合成這一個部落格。 

  • 帶頭結點的
#include <stdio.h>
#include <stdlib.h>
typedef struct stu
{
    int data;  //存放資料
    struct stu* next; //下一個節點地址
}Node;
typedef struct
{
    int count;  // 節點個數
    Node* head; // 頭結點
}List;


//建立新節點功能
Node* Create_node(int data)
{
    Node* pnew = (Node*)malloc(sizeof(Node));//為新節點開闢空間
    pnew->next = NULL;     //新節點指向null;
    pnew->data = data;
    return pnew;
}


//頭部插入資料
void push_head(List* p, int data)
{
    //建立一個新的節點
    Node*pn = Create_node(data);
    //插入新節點到表頭
    pn->next = p->head;
    p->head = pn;
    p->count++;
}


//尾插
void push_tail(List* p, int data)
{
    //找到最後一個節點
    Node* pn = p->head;
    while (pn->next != NULL)
    {
        pn = pn->next;
    }
    //插入元素
    Node*pm = Create_node(data);
    pn->next = pm;
    p->count++;
}


//頭刪節點
void del_head(List* p)
{
    Node* pn = p->head;//把要刪除的節點儲存起來
    p->head = p->head->next;//連線它的前節點與後節點
    free(pn);//釋放並置為空
    pn = NULL;
    p->count--;
}


//尾刪節點
void del_tail(List* p)
{
    Node *pn = p->head;//第一個節點
    while (pn->next->next != NULL)
    {
        pn = pn->next;
    }
    Node*pm = pn->next->next;
    pn->next = NULL;
    free(pm);
    pm = NULL;
    p->count--;
}


//任一位置節點插入
void push_arb(List* p, int data, int pos)
{
    //判斷插入位置是否合法
    if (pos<0 || pos>p->count)
    {
        printf("插入位置不合法,插入到尾部\n");
        pos = p->count;
    }
    //頭插
    if (pos == 0)
    {
        Node*pn = Create_node(data);
        pn->next = p->head;
        p->head = pn;
        p->count++;
        return;
    }
    //中間任意位置插入
    Node*pn = Create_node(data);
    Node*pm = p->head;
    for (int i = 1; i < pos; i++)
    {
        pm = pm->next;
    }
    pn->next = pm->next;
    pm->next = pn;
    p->count++;
}


//任一節點刪除
void del_arb(List* p, int pos)
{
    //判斷刪除位置是否合法
    if (pos<0 || pos >= p->count)//刪除的是當前位置後面的一個節點
    {
        printf("刪除位置不合法,刪除失敗\n");
        return;
    }
    Node*pn = p->head;
    if (pos == 0)//頭刪
    {
        p->head = p->head->next;
        free(pn);
        p->count--;
        return;
    }
    //任意位置刪除
    for (int i = 1; i < pos; i++)
    {
        pn = pn->next;
    }
    Node*pm = pn->next;
    pn->next = pn->next->next;
    free(pm);
    pm = NULL;
    p->count--;
}


//從尾到頭列印連結串列遞迴呼叫
void tail_head(Node*head)//遞迴方法
{
    if (head == NULL)
    {
        return;
    }
    tail_head(head->next);
    printf("%d  ", head->data);
}

//連結串列長度
void long_list(List* p)
{
    printf("%d\n", p->count);
}
//清空連結串列
void clear_list(List* p)//從頭開始清空
{
    printf("清空所有的節點\n");
    //第一個資料

    while (p->head != NULL)
    {
        Node*pn = p->head;
        p->head = p->head->next;
        free(pn);
        pn = NULL;
        p->count--;
    }

}


//遍歷連結串列
void travel_list(List* p)
{
    Node* ptemp = p->head;
    if (ptemp == NULL)
    {
        printf("連結串列已空\n");
        return;
    }
    printf("連結串列中元素有: ");
    while (ptemp != NULL)
    {
        printf("%d   ", ptemp->data);
        ptemp = ptemp->next;
    }
    printf("\n");
}
int main()
{
    List list;
    list.head = NULL;
    list.count = 0;
    push_head(&list, 1);
    push_head(&list, 2);
    push_head(&list, 3);
    travel_list(&list);
    push_tail(&list, 4);
    travel_list(&list);
    del_head(&list);
    travel_list(&list);
    del_tail(&list);
    travel_list(&list);
    long_list(&list);
    clear_list(&list);
    long_list(&list);
    push_arb(&list, 6, 9);
    push_arb(&list, 6, 1);
    travel_list(&list);
    tail_head(list.head);
    travel_list(&list);
    return 0;
}
  •  不帶頭結點的
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct link_node
{
    datatype info;
    struct link_node *next;
} node;
node *creat1()
{

    node *head=NULL;
    node *s;
    datatype x;
    printf("輸入一個數構建一個連結串列 -1表示輸入結束\n");
    scanf("%d",&x);
    while (x!=-1)
    {
        printf("輸入一個數構建一個連結串列 -1表示輸入結束\n");
        s=(node *)malloc(sizeof(node));
        s->info=x;
        s->next=head;
        head=s;
        scanf("%d",&x);
    }
    return head;
}
void print1(node *head)
{
    node *p;
    if(head==NULL)
    {
        printf("The list is empty!\n");
        return ;
    }
    p=head;
    while(p)
    {
        printf("%4d",p->info);
        p=p->next;
    }
    printf("\n");
}
void ListLength (node *head)
{
    int n=0;
    node *p=head;
    while (p)
    {
        n++;
        p=p->next;
    }
    printf("節點的個數為:%d\n",n);
}
int main()
{
    node *head;
    head=creat1();
    printf("List:");
    print1(head);
    ListLength(head);
    return 0;
}