7.23數據結構——單鏈表

文章目錄

  • 一、思維導圖
  • 二、單鏈表代碼
    • head.h
    • text.c
    • main.c
    • 現象

一、思維導圖

在這里插入圖片描述

二、單鏈表代碼

head.h

#ifndef __HEAD_H__
#define __HEAD_H__#include <stdlib.h>
#include <stdio.h>
#include <string.h>enum A
{FAULSE=-1,//失敗返回SUCCESS//成功返回};//給普通節點的數據域類型起別名
typedef int datatype;//定義節點的結構體
typedef struct Node
{//數據域union{datatype data;//普通元素的數據域int len;//頭結點的數據元素:數據長度};//指針域:存儲下一個節點的地址struct Node *next;
}*linklist;//單鏈表的節點創建
linklist creat_node(int flag);
//頭插
int insert_head(linklist head,datatype element);
//輸出
int output(linklist head);
//尾插
int insert_end(linklist head,datatype element);
//頭刪
int delate_head(linklist head);
//尾刪
int delate_end(linklist head);
//按位置查找
int search_place(linklist head,int place);
//按位置刪除
int delate_place(linklist head,int place);
//按位置插入
int insert_place(linklist head,int place,datatype element);
//按位置修改
int change_place(linklist head,int place,datatype element);
//按元素查找
int search_number(linklist head,datatype element);
//按元素修改
int change_number(linklist head,datatype element,datatype number);
//按元素刪除
int delate_number(linklist head,datatype element);
//逆置
int reverse(linklist head);
//找倒數第n個節點
int find_n(linklist head,int n);
//單鏈表釋放內存
int my_free(linklist head);
//單鏈表排序
int bubble(linklist head);#endif

text.c

#include "head.h"//單鏈表的節點創建
linklist creat_node(int flag)
{linklist s=(linklist)malloc(sizeof(struct Node));if(s==NULL){return NULL;}//內存申請成功//flag==1給頭結點初始化if(flag==1){s->len=0;}//flag==0給普通節點初始化else if(flag==0){s->data==0;}//初始化指針域s->next=NULL;return s;
}//頭插
int insert_head(linklist head,datatype element)
{//判斷頭結點是否存在if(head==NULL){return FAULSE;}//創建節點linklist s=creat_node(0);if(s==NULL){return FAULSE;}//節點創建成功s->data=element;//對新節點的數據域賦值//插入新節點s->next=head->next;head->next=s;//鏈表長度加1head->len++;return SUCCESS;
}//輸出
int output(linklist head)
{//判斷head是否為null//判斷內容是否為空if(head==NULL||head->len==0){return FAULSE;}linklist L=head;/*for(int i=0;i<head->len;i++){printf("%d  ",L->next->data);L=L->next;}*/while(L->next!=NULL){printf("%d  ",L->next->data);L=L->next;}putchar(10);return SUCCESS;
}//尾插
int insert_end(linklist head,datatype element)
{//判斷頭結點是否存在if(head==NULL){return FAULSE;}linklist end=head;while(end->next!=NULL)//尋找尾節點{end=end->next;}//創建尾節點linklist s=creat_node(0);//輸入尾節點的值s->data=element;//讓尾節點指向新的尾節點end->next=s;head->len++;return SUCCESS;}//頭刪
int delate_head(linklist head)
{//判斷頭結點是否存在//判斷頭結點是否為空if(head==NULL||head->next==NULL){return FAULSE;}//標記要刪除的節點linklist index=head->next;//讓頭結點指向要刪除節點的下一個節點head->next=index->next;//釋放刪除節點的內存free(index);//防止野指針index=NULL;head->len--;return SUCCESS;
}//尾刪
int delate_end(linklist head)
{if(head==NULL||head->next==NULL){return FAULSE;}//定義尾節點的上一個節點linklist index=head;while(index->next->next!=NULL){index=index->next;}//刪除尾節點free(index->next);index->next=NULL;head->len--;return SUCCESS;
}//按位置查找
int search_place(linklist head,int place)
{//判斷頭結點是否存在//判斷位置是否合法if(head==NULL||place<1||place>head->len){return FAULSE;}linklist p=head;for(int i=0;i<place;i++){p=p->next;}printf("第%d個元素的數值為%d\n",place,p->data);return SUCCESS;
}//按位置刪除
int delate_place(linklist head,int place)
{//判斷頭結點是否存在//判斷位置是否合法if(head==NULL||place<1||place>head->len){return FAULSE;}linklist p=head;for(int i=0;i<place-1;i++){p=p->next;}linklist L=p->next;p->next=L->next;free(L);L->next=NULL;head->len--;return SUCCESS;}//按位置插入
int insert_place(linklist head,int place,datatype element)
{//判斷頭結點是否存在//判斷位置是否合法if(head==NULL||place<1||place>head->len+1){return FAULSE;}//尋找插入節點前一個節點linklist p=head;for(int i=0;i<place-1;i++){p=p->next;}//創建節點linklist L=creat_node(0);if(L==NULL)return FAULSE;//傳值L->data=element;L->next=p->next;p->next=L;head->len++;return SUCCESS;}//按位置修改
int change_place(linklist head,int place,datatype element)
{//判斷頭結點是否存在//判斷位置是否合法if(head==NULL||place<1||place>head->len){return FAULSE;}linklist p=head;for(int i=0;i<place;i++){p=p->next;}p->data=element;return SUCCESS;
}//按元素查找
int search_number(linklist head,datatype element)
{//判斷節點是否存在if(head==NULL){return FAULSE;}linklist p=head;int pos=0;while(p!=NULL){if(p->data==element){return pos;}pos++;p=p->next;}return FAULSE;
}//按元素修改
int change_number(linklist head,datatype element,datatype number)
{int pos=search_number(head,element);if(pos==FAULSE)return FAULSE;change_place(head,pos,number);return SUCCESS;
}//按元素刪除
int delate_number(linklist head,datatype element)
{int pos=search_number(head,element);if(pos==FAULSE)return FAULSE;delate_place(head,pos);return SUCCESS;}
//逆置
int reverse(linklist head)
{//判斷頭結點是否存在//判斷其他節點是0個或1個if(NULL==head||head->len<=1){return FAULSE;}//逆置linklist p=head->next;head->next=NULL;for(int i=0;i<head->len;i++){linklist temp=p;p=p->next;//把temp頭插temp->next=head->next;head->next=temp;}return SUCCESS;
}//找倒數第n個節點
int find_n(linklist head,int n)
{if(head==NULL||n<1||n>head->len){return FAULSE;}linklist p=head;linklist q=head;for(int i=0;i<n;i++){q=q->next;}while(q!=NULL){p=p->next;q=q->next;}printf("倒數第%d個節點的值為%d\n",n,p->data);return SUCCESS;
}//單鏈表釋放內存
int my_free(linklist head)
{if(head==NULL){return FAULSE;}int len=head->len;for(int i=0;i<len;i++){delate_end(head);}free(head);
}//單鏈表排序
int bubble(linklist head)
{if(NULL==head||head->len<=1){return FAULSE;}for(int i=0;i<head->len-1;i++){int flag=0;linklist p=head->next;for(int j=0;j<head->len-1-i;j++){if(p->data>p->next->data){datatype temp=p->data;p->data=p->next->data;p->next->data=temp;flag=1;}p=p->next;}if(flag==0){break;}}return SUCCESS;
}

main.c

#include "head.h"
int main(int argc, const char *argv[])
{//創建頭結點linklist head=creat_node(1);//頭插insert_head(head,15);insert_head(head,13);insert_head(head,12);insert_head(head,21);insert_head(head,19);insert_head(head,12);insert_head(head,54);insert_head(head,14);printf("頭插后的結果:\n");output(head);//尾插insert_end(head,11);printf("尾插后的結果:\n");output(head);//按位置刪除delate_place(head,3);printf("按位置刪除后的結果:\n");output(head);//按位置查找search_place(head,2);//頭刪delate_head(head);printf("頭刪后的結果:\n");output(head);//按位置插入insert_place(head,2,14);printf("按位置插入的結果:\n");output(head);//按位置修改change_place(head,2,98);printf("按位置修改后的結果:\n");output(head);//尾刪delate_end(head);printf("按尾刪后的結果:\n");output(head);//逆置reverse(head);printf("逆置后的結果:\n");output(head);//按元素查找int pos=search_number(head,13);printf("查找到元素為第%d個\n",pos);//按元素修改change_number(head,15,99);printf("按元素修改后的結果:\n");output(head);//按元素刪除delate_number(head,54);printf("按元素刪除后的結果:\n");output(head);//找倒數第n個節點find_n(head,2);//單鏈表排序bubble(head);printf("排序之后的結果:\n");//輸出output(head);//單鏈表釋放內存my_free(head);head=NULL;return 0;
}

現象

頭插后的結果:
14 54 12 19 21 12 13 15
尾插后的結果:
14 54 12 19 21 12 13 15 11
按位置刪除后的結果:
14 54 19 21 12 13 15 11
第2個元素的數值為54
頭刪后的結果:
54 19 21 12 13 15 11
按位置插入的結果:
54 14 19 21 12 13 15 11
按位置修改后的結果:
54 98 19 21 12 13 15 11
按尾刪后的結果:
54 98 19 21 12 13 15
逆置后的結果:
15 13 12 21 19 98 54
查找到元素為第2個
按元素修改后的結果:
99 13 12 21 19 98 54
按元素刪除后的結果:
99 13 12 21 19 98
倒數第2個節點的值為19
排序之后的結果:
12 13 19 21 98 99

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/90669.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/90669.shtml
英文地址,請注明出處:http://en.pswp.cn/web/90669.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

某種物聯網SIM卡流量查詢方法

說起流量卡,很多人可能還停留在營業廳辦理的常規套餐里。但其實在 2016 年,三大運營商就推出了一種資費更為劃算的正規流量卡 —— 物聯卡。當年,當不少人還在用 50 元 1G 的流量時,第一批體驗物聯卡的用戶已經享受到了 53 元 6G 的全國流量,徹底擺脫了流量焦慮。不過,至…

XTTS實現語音克隆:精確控制音頻格式與生成流程【TTS的實戰指南】

言簡意賅的講解XTTS解決的痛點 &#x1f4ce; 前置操作&#xff1a;如何使用 OBS Studio 錄制高質量 WAV 語音&#xff08;建議先閱讀并準備錄音樣本&#xff09; 本教程介紹如何使用 Coqui TTS 的 XTTS v2 模型 實現中文語音克隆&#xff0c;支持直接傳入 .wav 文件&#xff0…

C/C++中常量放置在比較操作符左側

目錄 介紹 原因詳解 避免誤用賦值運算符 示例對比 結論 介紹 在編程中&#xff0c;將常量放在比較操作符&#xff08;如 或 !&#xff09;的左側&#xff08;例如 if (42 value)&#xff09;&#xff0c;是一種被稱為 "Yoda 條件"&#xff08;Yoda Conditions…

Node.js 模擬 Linux 環境

&#x1f9e9; 項目介紹 該項目使用 Node.js 實現了一個模擬的 Linux 終端環境&#xff0c;支持多種常見的 Linux 命令&#xff08;如 ls, cd, cat, mkdir, rm 等&#xff09;&#xff0c;所有文件操作都在內存中進行&#xff0c;并持久化到本地文件系統中。適合用于學習 Shel…

HAProxy 實驗指南:從零開始搭建高可用負載均衡系統

引言HAProxy&#xff08;High Availability Proxy&#xff09;是一款高性能的TCP/HTTP負載均衡器和代理服務器&#xff0c;廣泛用于構建高可用、可擴展的Web架構。它由法國開發者Willy Tarreau于2000年開發&#xff0c;如今已成為開源社區和企業級應用中不可或缺的工具。HAProx…

2.10DOM和BOM插入/移除/克隆

1.DOM創建/插入/移除/克隆1.1創建元素前面我們使用過 document.write 方法寫入一個元素&#xff1a;這種方式寫起來非常便捷&#xff0c;但是對于復雜的內容、元素關系拼接并不方便&#xff1b;它是在早期沒有 DOM 的時候使用的方案&#xff0c;目前依然被保留了下來&#xff1…

華為倉頡編程語言的表達式及其特點

華為倉頡編程語言的表達式及其特點 倉頡&#xff08;Cangjie&#xff09;語言的表達式有一個明顯的特點&#xff0c;范圍不再局限于傳統算術運算&#xff0c;而是擴展到條件表達式、循環表達式等多種類型&#xff0c;每種表達式均有確定的類型和值。 傳統基本表達式&#xff0…

【linux】keepalived

一.高可用集群1.1 集群類型LB&#xff1a;Load Balance 負載均衡 LVS/HAProxy/nginx&#xff08;http/upstream, stream/upstream&#xff09; HA&#xff1a;High Availability 高可用集群 數據庫、Redis SPoF: Single Point of Failure&#xff0c;解決單點故障 HPC&#xff…

Webpack配置原理

一、Loader&#xff1a; 1、定義&#xff1a;將不同類型的文件轉換為 webpack 可識別的模塊2、分類&#xff1a; ① pre&#xff1a; 前置 loader &#xff08;1&#xff09;配置&#xff1a;在 webpack 配置文件中通過enforce進行指定 loader的優先級配置&#xff08;2&#x…

對比JS“上下文”與“作用域”

下面從定義、特性、示例&#xff0c;以及在代碼分析中何時側重“上下文”&#xff08;Execution Context/this&#xff09;和何時側重“作用域”&#xff08;Scope/變量查找&#xff09;&#xff0c;以及二者結合的場景來做對比和指導。一、概念對比 | 維度 | 上下文&#xff0…

如何做數據增強?

目錄 1、為什么要做數據增強&#xff1f; 2、圖像數據增強&#xff1f; 3、文本與音頻數據增強&#xff1f; 4、高級數據增強&#xff1f; 數據增強技術就像是一種“造數據”的魔法&#xff0c;通過對原始數據進行各種變換&#xff0c;生成新的樣本&#xff0c;從而提高模型…

Go by Example

網頁地址Go by Example 中文版 Github倉庫地址mmcgrana/gobyexample&#xff1a;按示例進行 HelloWorld package mainimport ("fmt" )func main() {fmt.Println("Hello World") } Hello World 值 package mainimport ("fmt" )func main() {…

ClickHouse高性能實時分析數據庫-消費實時數據流(消費kafka)

告別等待&#xff0c;秒級響應&#xff01;這不只是教程&#xff0c;這是你駕馭PB級數據的超能力&#xff01;我的ClickHouse視頻課&#xff0c;凝練十年實戰精華&#xff0c;從入門到精通&#xff0c;從單機到集群。點開它&#xff0c;讓數據處理速度快到飛起&#xff0c;讓你…

電子電氣架構 --- 車載軟件與樣件產品交付的方法

我是穿拖鞋的漢子,魔都中堅持長期主義的汽車電子工程師。 老規矩,分享一段喜歡的文字,避免自己成為高知識低文化的工程師: 簡單,單純,喜歡獨處,獨來獨往,不易合同頻過著接地氣的生活,除了生存溫飽問題之外,沒有什么過多的欲望,表面看起來很高冷,內心熱情,如果你身…

C++:STL中vector的使用和模擬實現

在上一篇中講到了string類&#xff0c;string并不屬于STL中因為string出現的比STL早&#xff0c;但是在使用方法上兩者有相似之處&#xff0c;學習完string后再來看vector會容易的多&#xff0c;接著往下閱讀&#xff0c;一定會有收獲滴&#xff01; 目錄 vector的介紹 vect…

倉庫管理的流程、績效和解決方案?

什么是倉庫管理&#xff1f; 倉庫管理涉及對所有倉庫運營的日常監督。一個全面、集成的倉庫管理解決方案采用行業最佳實踐&#xff0c;并涵蓋使高效運營得以實現的所有基本要素。這些要素包括分銷和庫存管理、倉庫勞動力管理以及業務支持服務。此外&#xff0c;由內部提供或與服…

TIM 實現定時中斷【STM32L4】【實操】

使用定時器實現定時中斷的功能&#xff1a;比如每1ms進入中斷處理函數使用STM32CubeMX配置TIM初始化先了解每個參數的含義&#xff0c;在進行配置Counter Settings: 計數器基本設置Prescaler(PSC): 預分頻器&#xff0c;設置預分頻器系數Counter Mode: 技術模式&#xff0c;…

Elasticsearch 的聚合(Aggregations)操作詳解

目錄 1. 概述 2. 聚合類型分類詳解 2.1 桶聚合&#xff08;Bucket Aggregations&#xff09; 2.1.1 基礎桶聚合 2.1.2 特殊桶聚合 2.1.3 高級桶聚合 2.2 指標聚合&#xff08;Metric Aggregations&#xff09; 2.2.1 單值指標聚合&#xff08;Single-value Metrics&am…

電子電氣架構 --- 高階智能駕駛對E/E架構的新要求

我是穿拖鞋的漢子,魔都中堅持長期主義的汽車電子工程師。 老規矩,分享一段喜歡的文字,避免自己成為高知識低文化的工程師: 做到欲望極簡,了解自己的真實欲望,不受外在潮流的影響,不盲從,不跟風。把自己的精力全部用在自己。一是去掉多余,凡事找規律,基礎是誠信;二是…

0.深度學習環境配置步驟

0.深度學習環境配置步驟 這里介紹深度學習環境配置詳細步驟&#xff0c;包括安裝軟件&#xff0c;每一步都有安裝時的截圖&#xff08;后續持續更新&#xff0c;敬請關注&#xff09; 目錄如下&#xff1a; 1.安裝anaconda 2.安裝CUDA 3.安裝CU_DNN 4.安裝pytorch