溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言中單鏈表的示例分析

發(fā)布時間:2022-03-04 11:25:11 來源:億速云 閱讀:134 作者:小新 欄目:開發(fā)技術

這篇文章將為大家詳細講解有關C語言中單鏈表的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

    一、思路步驟

    1. 定義結構體

    a.數(shù)據(jù)域:用來存放數(shù)據(jù)

    b.指針域:用來存放下一個數(shù)據(jù)的位置

    2.初始化

    申請頭結點,并將其初始化為空

    3.求當前數(shù)據(jù)元素的個數(shù)

    C語言中單鏈表的示例分析

    a.設置一個指針變量p指向頭結點和計數(shù)變量size等于0

    b.循環(huán)判斷p->next是否為空,如果不為空,就讓指針p指向它的直接后繼結點,并讓size自增

    c.返回size

    4.插入

    C語言中單鏈表的示例分析

    a.設置兩個指針,一個指向頭結點,另一個要動態(tài)申請內(nèi)存空間存放要插入的數(shù)
    b.找到要插入位置的前一位,并判斷插入位置是否正確
    c.生成新結點,給新結點數(shù)據(jù)域賦值,執(zhí)行步驟①,在執(zhí)行步驟②

    5.刪除

    C語言中單鏈表的示例分析

    a.設置兩個指針p、q,p指向頭結點,q指向要被刪除的結點

    b.找到要刪除位置的前一位,并判斷刪除位置是否正確、存在

    c.q指向被刪除的結點,將被刪除結點的數(shù)據(jù)域賦值給x,p指向被刪除結點的下一個結點,釋放q的內(nèi)存空間

    6.釋放內(nèi)存空間

    最后記得將頭結點置空哦!要不然容易出現(xiàn)野指針。

    二、代碼

    #include <stdio.h>
    #include <stdlib.h>
    typedef int DataType;//給int起個別名,方便以后修改
    typedef struct Node
    {
    	DataType data;//數(shù)據(jù)域
    	struct Node *next;//指針域
    }SLNode;
    //初始化
    void ListInit(SLNode **head)
    {
    	*head = (SLNode *)malloc(sizeof(SLNode));//申請頭結點
    	(*head)->next = NULL;
    }
    //求當前數(shù)據(jù)元素個數(shù)
    int ListLength(SLNode *head)
    {
    	SLNode *p = head;
    	int size = 0;
    	while (p->next != NULL)
    	{
    		p = p->next;
    		size++;
    	}
    	return size;
    }
    //插入
    int ListInsert(SLNode *head, int i, DataType x)
    {
    	SLNode *p, *q;
    	int j;
    	p = head;
    	j = -1;
    	while (p->next != NULL && j < i - 1)
    	{
    		p = p->next;
    		j++;
    	}
    	if (j != i - 1)
    	{
    		printf("插入?yún)?shù)位置錯誤?。。n");
    		return 0;
    	}
    	q = (SLNode *)malloc(sizeof(SLNode));//生成新結點
    	q->data = x;
    	q->next = p->next;
    	p->next = q;
    	return 1;
    }
    //刪除
    int ListDelete(SLNode *head, int i, DataType *x)
    {
    	SLNode *p, *q;
    	int j;
    	p = head;
    	j = -1;
    	while (p->next != NULL && p->next->next != NULL && j < i - 1)
    	{
    		p = p->next;
    		j++;
    	}
    	if (j != i - 1)
    	{
    		printf("刪除位置參數(shù)錯誤?。?!\n");
    		return 0;
    	}
    	q = p->next;
    	*x = q->data;
    	p->next = p->next->next;
    	free(q);//釋放被刪除結點的內(nèi)存空間
    	return 1;
    }
    //按位取
    int ListGet(SLNode *head, int i, DataType *x)
    {
    	SLNode *p;
    	int j;
    	p = head;
    	j = -1;
    	while (p->next != NULL && j < i)
    	{
    		p = p->next;
    		j++;
    	}
    	if (j != i)
    	{
    		printf("取出位置參數(shù)錯誤!??!\n");
    		return 0;
    	}
    	*x = p->data;
    	return 1;
    }
    //釋放
    void ListDestroy(SLNode **head)
    {
    	SLNode *p, *q;
    	p = *head;
    	while (p != NULL)
    	{
    		q = p;
    		p = p->next;
    		free(q);
    	}
    	*head = NULL;
    }
    
    int main()
    {
    	SLNode *head;
    	int i, x;
    	ListInit(&head);
    	for (i = 0; i < 10; i++)
    		ListInsert(head, i, i + 10);
    	ListDelete(head, 9, &x);
    	for (i = 0; i < ListLength(head); i++)
    	{
    		ListGet(head, i, &x);
    		printf("%d ", x);
    	}
    	ListDestroy(&head);
    	system("pause");
    	return 0;
    }

    關于“C語言中單鏈表的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

    AI