1. 程式人生 > >C語言 單向連結串列

C語言 單向連結串列

1、單向連結串列的定義

struct student
{
    char name[10];
    float score;
    struct student *next;
};

next作為同類型指標,指向與它所在節點一樣的節點。

2、單向連結串列的基本操作

1)建立連結串列

int main()
{
    /*定義結構指標,pnew指向新節點,head指向頭節點,tail指向尾節點*/
    struct student *pnew, *head, * ptail;
    /*
            動態分配庫函式malloc,分配長度為sizeof(struct student)的儲存空間,函式返回分配到空間的起始地址,
            指向的型別為強制型別轉換後的struct student*.
            malloc的標頭檔案stdlib.h
     
*/ pnew = (struct student*) malloc(sizeof(struct student)); /* 空連結串列中建立頭節點操作。 */ scanf("%s%f",pnew->name,&pnew->score); head = pnew; ptail = pnew; /* 向現有連結串列中新增新節點。 */ pnew = (struct student*) malloc(sizeof(struct student)); scanf(
"%s%f",pnew->name,&pnew->score); ptail->next = pnew; ptail = pnew; /* 將末節點指向下一節點的成員賦值為NULL */ ptail->next = NULL; }

 

/*定義建立函式create,建立一個有n個節點的單向連結串列*/

struct student *create(int n)
{
    struct student *pnew, *head, *ptail;
    int i;
    pnew 
= (struct student*) malloc(sizeof(struct student)); scanf("%s%f",pnew->name, &pnew->score); head = ptail = pnew; for (i = 1; i < n; i++) { pnew = (struct student*) malloc(sizeof(struct student)); scanf("%s%f",pnew->name, &pnew->score); ptail->next = pnew; ptail = pnew; } ptail->next = NULL; return head; }

 

2)遍歷連結串列

/*定義輸出連結串列節點資訊函式print,形參head為連結串列頭指標*/

void print(struct student *head)
{
    struct student *p = head;
    while (p != NULL)
    {
        printf("%s  %.1f\n",p->name, p->score);
        p = p->next;
    }
}

 

3)在連結串列中插入節點

 

/*定義函式insert,在有序連結串列中插入一個節點,使連結串列按score成員從大到小排列節點*/

struct student* insert(struct student *head)
{
    struct student *p = head, *pnew, *pold = head;
    pnew = (struct student*) malloc(sizeof(struct student));
    scanf("%s%f",pnew->name,&pnew->score);
    if (pnew->score > head->score)  //當新結點score值大於頭結點時,將新結點指向頭節點,再作為頭節點
    {
        pnew->next = head;
        head = pnew;
    }
    else
    {
        while(p != NULL && pnew->score < p->score)
        {
            pold = p;
            p = p->next;
        }
        
        pold->next = pnew;
        pnew->next = p;
    }
    return head;
}

 

4)在連結串列中刪除節點

/*定義函式pdelete,在連結串列中刪除所有成員score值大於等於grade值的節點。*/

struct student *pdelete(struct student *head, int grade)
{
    struct student *p,*pold;
    
    p = head;
    while (head != NULL && head->score >= grade)    //當頭結點為所刪除節點時,將頭結點指向下一節點,並釋放其空間。
    {
        head = head->next;
        free(p);
        p = head;
    }
    if (head == NULL) return head;
    p = head->next;
    pold = head;        //pold指向剛才已檢查過的結點。
    while (p != NULL)
    {
        if(p->score >= grade)
        {
            pold->next = p->next;
            free(p);
            p = pold->next;
        }
        else
        {
            pold = p;
            p = p->next;
        }
    }
    return head;
}

 

題目1

 

/*輸入n個學生的資訊(姓名,成績),根據成績資料建立一個連結串列,使連結串列中的節點按成績從高到低連線起來。*/
#include <stdio.h>
#include <stdlib.h>

struct student
{
    char name[10];
    float score;
    struct student *next;
};

struct student* insert(struct student *);
void print(struct student *);

int main()
{
    struct student *head;
    int i, n;
    scanf("%d",&n);
    head = (struct student*) malloc(sizeof(struct student));
    scanf("%s%f",head->name, &head->score);
    head->next = NULL;
    for (i = 1;i < n; i++)
    {
        head = insert(head);
    }
    print(head);
    return 0;
}

 

 

題目2

 

/*輸入n個學生的資訊(姓名、成績),輸出所有學生的節點資訊,刪除連結串列中所有不參加補考同學的節點,最後再輸出要補考學生的節點資訊*/
#include <stdio.h>
#include <stdlib.h>

struct student
{
    char name[10];
    float score;
    struct student *next;
};

struct student* insert(struct student *);
void print(struct student *);
struct student *create(int );
struct student *pdelete(struct student *, int );

int main()
{
    struct student *head;
    int n;
    scanf("%d",&n);
    head = create(n);
    print(head);
    head = pdelete(head,60);
    print(head);
    return 0;
}

 

單鏈表氣泡排序

修改資料,或者 先全部修改,再還原指標(這裡用第二種方法)

 

    pi = head;
    while (pi->next != NULL)
    {
        pj = pi->next;
        while (pj != NULL)
        {
            if (pi->date > pj->date)
            {
                t = *pi;
                *pi = *pj;
                *pj = t;
                t.next = pi->next;
                pi->next = pj->next;
                pj->next = t.next;
            }
            pj = pj->next;
        }
        pi = pi->next;
    }