單向循環鏈表的操作

main函數:

 #ifndef __loopLinkList_H__#define __loopLinkList_H__typedef int datatype;union msg{    //若數據的類型也為int,則不需要這個聯合體datatype data;int len;       //放頭結點,記錄鏈表長度};typedef struct node{union msg text;struct node* next; //指針,由于指針指向這一整個節點,所以類型為struct node*}loopLinkList;loopLinkList* create_loopLinkList(void);void insertHead_loopLinkList(loopLinkList* head,datatype num);void insertTail_loopLinkList(loopLinkList* head,datatype num);void deleteHead_loopLinkList(loopLinkList* head);void deleteTail_loopLinkList(loopLinkList* head);void show_loopLinkList(loopLinkList* head);#endif                                                                          

功能函數:

#include<stdio.h>                                                                
#include <stdlib.h>                                                              
#include "./13_loopLinkList.h"                                                   
//創建一個單向循環鏈表                                                           
loopLinkList* create_loopLinkList()                                              
{                                                                                //定義頭結點并初始化                                                         loopLinkList* head=(loopLinkList*)malloc(sizeof(loopLinkList));              head->next=head;                                                             head->text.len=0;                                                            return head;                                                                 
}                                                                                
//判斷鏈表是否為空                                                               
int isEmpty_loopLinkList(loopLinkList* head)                                     
{                                                                                //1表示鏈表為空,0表示鏈表不為空                                             return head->next==head?1:0;                                                 
}                                                                                
//頭插                                                                           
void insertHead_loopLinkList(loopLinkList* head,datatype num)                    
{                                                                                //申請一個空間定義一個新的結點                                               loopLinkList* temp=(loopLinkList*)malloc(sizeof(loopLinkList));              if(NULL == temp)                                                             {                                                                            printf("結點定義失敗!\n");                                              return;                                                                  }                                                                            //初始化這個結點                                                             temp->text.data=num;                                                         temp->next=NULL;                                                             //插入                                                                       temp->next=head->next;                                                       head->next=temp;                                                             head->text.len++;                                                            return;                                                                      
}                                                                                
//尾插                                                                           
void insertTail_loopLinkList(loopLinkList* head,datatype num)                    
{                                                                                //申請一個空間定義一個新的結點                                               loopLinkList* temp=(loopLinkList*)malloc(sizeof(loopLinkList));              if(NULL == temp)                                                             {                                                                            printf("結點定義失敗!\n");                                              return;                                                                  }                                                                            //初始化這個結點                                                             temp->text.data=num;                                                         temp->next=NULL;                                                             //找到鏈表最后一個結點                                                       loopLinkList* p=head;                                                        while(p->next!=head)                                                         {                                                                            p=p->next;                                                               }                                                                            //插入temp                                                                   temp->next=head;                                                             p->next=temp;                                                                head->text.len--;                                                            return;                                                                      
}   
//按位置插入                                                               
void insertByPositon_loopLinkList(loopLinkList* head,datatype num,int pos) 
{                                                                          if(pos<1 || pos >head->text.len+1)                                     {                                                                      printf("插入位置不合法!\n");                                      }                                                                      loopLinkList* temp = (loopLinkList*)malloc(sizeof(loopLinkList));      temp->text.data=num;                                                   temp->next=NULL;                                                       loopLinkList* p=head;                                                  int i;                                                                 for(i=0;i<pos-1;i++)                                                   {                                                                      p=p->next;                                                         }                                                                      temp->next=p->next;                                                    p->next=temp;                                                          head->text.len++;                                                      return;                                                                
}                                                                          
//頭刪                                                                     
void deleteHead_loopLinkList(loopLinkList* head)                           
{                                                                          if(isEmpty_loopLinkList(head))                                         {                                                                      printf("鏈表為空,刪除失敗!\n");                                   return;                                                            }                                                                      loopLinkList* temp=head->next;                                         head->next=temp->next;                                                 free(temp);                                                            head->text.len--;                                                      return;                                                                
}                                                                          //尾刪                                                          void deleteTail_loopLinkList(loopLinkList* head)                {                                                               if(isEmpty_loopLinkList(head))                              {                                                           printf("鏈表為空,刪除失敗!\n");                        return;                                                 }                                                           loopLinkList* p=head;                                       while(p->next->next!=head)                                  {                                                           p=p->next;                                              }                                                           free(p->next);                                              p->next=head;                                               head->text.len--;                                           return;                                                     }                                                               //按位置刪除                                                    void deleteBypos_loopLinkList(loopLinkList* head,int pos)       {                                                               if(isEmpty_loopLinkList(head))                          {                                                           printf("鏈表為空,刪除失敗!\n");                        return;                                                 }                                                           loopLinkList* p=head;                                       int i;                                                      for(i=0;i<pos-1;i++)                                        {                                                           p=p->next;                                              }                                                           free(p->next);                                              p->next=p->next->next;                                      head->text.len--;                                           return;                                                     }                                                               //遍歷                                                          void show_loopLinkList(loopLinkList* head)                      {                                                               loopLinkList* p=head;                                       if(isEmpty_loopLinkList(head))                              {                                                           printf("鏈表為空!\n");                                 return;                                                 }                                                           while(p->next!=head)                                        {                                                           p=p->next;                                              printf("%d ",p->text.data);                             }                                                           printf("\n");                                               return;                                                     }   
//約瑟夫問題 void josepg_loopLinkList(int n ,int k,int m){loopLinkList* head = create_loopLinkList();//將1到n的數據插入到循環列表中int i=0;for(i=1;i<=n;i++){insertTail_loopLinkList(head,i);}//刪除頭結點,將頭指針指向頭結點后的第一個有效數據loopLinkList* p=head;while(p->next!=head){p=p->next;}//p就是當前鏈表的尾結點//移動頭指針到第一個有效數據head=head->next;//釋放頭結點free(p->next);//將尾結點的指針指向第一個有效數據p->next=head;//通過循環找到編號為1的位置p=head;for(i=0;i<k-1;i++){p=p->next;}//p就是編號為1的位置while(p->next != p)//當鏈表中只有一個結點時退出循環{//找到要出列的那個數的前一個結點for(i=0;i<m-2;i++){p=p->next;}//p就是要出列的前一個結點//將要數列的那個結點存起來,后面方便釋放loopLinkList* temp = p->next;//將p的指針域指向要出列的結點的下一個結點,即將要出列的那個節點刪除p->next = temp->next;//打印出列的結點里面的數printf("%d ",temp->text.data);//釋放該結點free(temp);                                                                    temp=NULL;//將剛剛出隊的下一個結點置為1p=p->next;}//上述循環后,整個鏈表中還剩一個結點printf("%d\n",p->text.data);free(p);p=NULL;}

頭文件:

 #ifndef __loopLinkList_H__                                                                                             #define __loopLinkList_H__                                                                                             typedef int datatype;                                                                                                  union msg{    //若數據的類型也為int,則不需要這個聯合體                                                                datatype data;                                                                                                         int len;       //放頭結點,記錄鏈表長度                                                                                };                                                                                                                     typedef struct node{                                                                                                   union msg text;                                                                                                    struct node* next; //指針,由于指針指向這一整個節點,所以類型為struct node*                                        }loopLinkList;                                                                                                         loopLinkList* create_loopLinkList(void);                                                                               void insertHead_loopLinkList(loopLinkList* head,datatype num);                                                         void insertByPositon_loopLinkList(loopLinkList* head,datatype num,int pos);                                            void insertTail_loopLinkList(loopLinkList* head,datatype num);                                                         void deleteHead_loopLinkList(loopLinkList* head);                                                                      void deleteTail_loopLinkList(loopLinkList* head);                                                                      void show_loopLinkList(loopLinkList* head);                                                                            
void josepg_loopLinkList(int n,int k,int m);void deleteBypos_loopLinkList(loopLinkList* head,int pos);#endif                                                                                                                 

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

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

相關文章

Istio實戰:Istio Kiali部署與驗證

目錄 前言一、Istio安裝小插曲 注意事項 二、Kiali安裝三、Istio測試參考資料 前言 前幾天我就開始搗騰Istio。前幾天在執行istioctl install --set profiledemo -y 的時候老是在第二步就報錯了&#xff0c;開始我用的istio版本是1.6.8。 后面查看k8s與istio的版本對應關系后發…

vCenter、vSphere Client硬盤擴容詳解

文章目錄 1、需求2、vSphere 操作流程3、服務器操作3.1、查看分區空間大小3.2、列出所有可用塊設備的信息3.3、新建分區3.4、重讀分區表信息3.5、格式化分區信息3.6、查看卷組的詳細狀態3.7、創建物理卷3.8、擴容卷組3.9、邏輯卷在線擴容3.10、顯示物理卷屬性3.11、XFS 文件系統…

最少停車數(C 語言)

題目描述 特定大小的停車場&#xff0c;數組cars[]表示&#xff0c;其中1表示有車&#xff0c;0表示沒車。車輛大小不一&#xff0c;小車占一個車位&#xff08;長度1&#xff09;&#xff0c;貨車占兩個車位&#xff08;長度2&#xff09;&#xff0c;卡車占三個車位&#xf…

Rollup + Ts

Rollup Ts RollupTs demo 一、文件配置 | - src | | - utils | | | - .ts | | - .babelrc | | - main.js | | - style.css | - package.json | - rollup.config.js | - tsconfig.json二、插件下載 rollup // rollup 基本的包 typescript // ts 包 rollup/plug…

如何做bug分析 ?bug分析什么 ? 為什么要做bug分析 ?

每當我們完成一個版本測試時&#xff0c;總會在測試報告中添加一些分析bug的指標 &#xff0c;主要用于分析在測試過程中存在的問題 。但是在分析的過程中你就可能遇到如下的問題 &#xff1a; 我應該分析那些指標呢 &#xff1f;每一個具體的指標該如何分析 &#xff1f;它能說…

Vue3學習——computed、watch、watchEffect

computed 與Vue2.x中computed配置功能一致寫法 import {computed} from vuesetup(){...//計算屬性——簡寫let fullName computed(()>{return person.firstName - person.lastName})//計算屬性——完整let fullName computed({get()return person.firstName - perso…

算法——模擬

1. 什么是模擬算法&#xff1f; 官方一點來說 模擬算法&#xff08;Simulation Algorithm&#xff09;是一種通過模擬現實或抽象系統的運行過程來研究、分析或解決問題的方法。它通常涉及創建一個模型&#xff0c;模擬系統中的各種事件和過程&#xff0c;以便觀察系統的行為&a…

Redis緩存一致性問題(自用記錄)

背景 在開發過程中&#xff0c;redis緩存技術被大范圍應用。由于現在的系統大多是分布式的&#xff0c;高并發的&#xff0c;redis和傳統的數據庫&#xff0c;存在數據不一致的問題。 解決方案 本文主要探討兩者數據不一致的解決方案&#xff1a; 給緩存設置過期時間&#x…

dell戴爾電腦靈越系列Inspiron 15 3520原廠Win11系統中文版/英文版

Dell戴爾筆記本靈越3520原裝出廠Windows11系統包&#xff0c;恢復出廠開箱預裝OEM系統 鏈接&#xff1a;https://pan.baidu.com/s/1mMOAnvXz5NCDO_KImHR5gQ?pwd3nvw 提取碼&#xff1a;3nvw 原廠系統自帶所有驅動、出廠主題壁紙、系統屬性聯機支持標志、Office辦公軟件、MyD…

Jmeter接口測試 ,這應該是全網最詳細的教程了

&#x1f345; 視頻學習&#xff1a;文末有免費的配套視頻可觀看 &#x1f345; 關注公眾號【互聯網雜貨鋪】&#xff0c;回復 1 &#xff0c;免費獲取軟件測試全套資料&#xff0c;資料在手&#xff0c;漲薪更快 一、Jmeter 的使用步驟 打開Jmeter 安裝包&#xff0c;進入\bi…

postman-使用Postman的模擬服務來模擬(mock)后端數據,完成前端模擬API調用

最近項目上比較忙&#xff0c;任務多時間緊&#xff0c;導致后端開發任務繁多&#xff0c;無法及時開發完畢&#xff0c;但是前端同學已經把對應功能開發完成&#xff0c;需要進行前后端聯調來驗證API及一些交互問題&#xff1b;這不能因為后端的進度來影響前端的工作完成情況&…

【Linux進程】馮·諾依曼體系結構以及操作系統的深入理解

&#x1f4d9; 作者簡介 &#xff1a;RO-BERRY &#x1f4d7; 學習方向&#xff1a;致力于C、C、數據結構、TCP/IP、數據庫等等一系列知識 &#x1f4d2; 日后方向 : 偏向于CPP開發以及大數據方向&#xff0c;歡迎各位關注&#xff0c;謝謝各位的支持 目錄 1.馮諾依曼體系結構特…

kafka和ZK的關系

zk相當于是kafka的一個基礎設施 Kafka是一種高吞吐量、可擴展的分布式發布訂閱消息系統&#xff0c;ZooKeeper是一個分布式協調服務&#xff0c;用于管理和協調分布式系統中的各種資源 Zookeeper&#xff1a;管理broker&#xff0c;consumer 創建broker后&#xff0c;向zk注冊…

適用于生物行業的樣本管理系統

在生物樣本管理系統的應用中&#xff0c;我們首先需要了解生物樣本的特點和要求。生物樣本具有多樣性和易變性&#xff0c;需要被妥善保存和跟蹤&#xff0c;以確保其質量和可用性。 因此&#xff0c;一個有效的生物樣本管理系統需要具備以下特點&#xff1a; 全面性&#xff1…

Spring Event的原理以及缺陷

原理:Spring 事件監聽機制及原理分析 - Admol - 博客園 (cnblogs.com) 使用bug:Spring Event 別瞎用&#xff01;從我司的悲劇中&#xff0c;我總結了6 條最佳實踐&#xff01;-騰訊云開發者社區-騰訊云 (tencent.com)

2024最新任推邦邀請碼是什么

任推邦是一款非常受歡迎的推廣APP&#xff0c;任推邦邀請碼是【222222】獲得現金獎勵和提成獎勵用戶可以通過邀請好友加入來獲取收益。2024最新的任推邦邀請碼是【222222】&#xff0c;小編已經給大家整理好了他趣許多的邀請碼&#xff0c;想要領取獎勵的小伙伴快來和小編一起了…

聚合函數()不要隨意加空格

報錯&#xff1a;1630 - FUNCTION link.SUM does not exist. Check the Function Name Parsing and Resolution section in the Reference Manual 解決方法&#xff1a;count、avg、sum等函數后緊隨的()之間不能加空格&#xff0c;去掉這個空格即可&#xff0c;因為count()、a…

js 監聽元素是否出現在可視區域--IntersectionObserver

觀察者模式監聽判斷dom元素是否在可視區域內 本項目是使用vue3的寫法。 1.IntersectionObserver IntersectionObserver可以用來自動監聽元素是否進入了設備的可視區域之內&#xff0c;而不需要頻繁的計算來做這個判斷。由于可見&#xff08;visible&#xff09;的本質是&…

融中穿刺路徑角度評估的C++技術實現

消融模型的三維渲染 我們以肝部為例&#xff0c;通常肝部在做消融手術規劃時有幾個步驟。 一三維重建&#xff1a; 對器官進行圖像分割&#xff1b; 對腫瘤的原發區域GTV進行勾畫。 二穿刺路徑的規劃&#xff1a; 路徑規劃當中有幾個約束&#xff1a;穿刺深度、危及器官的…

OpenAI推出首個AI視頻模型Sora:重塑視頻創作與體驗

鏈接&#xff1a;華為OD機考原題附代碼 Sora - 探索AI視頻模型的無限可能 隨著人工智能技術的飛速發展&#xff0c;AI視頻模型已成為科技領域的新熱點。而在這個浪潮中&#xff0c;OpenAI推出的首個AI視頻模型Sora&#xff0c;以其卓越的性能和前瞻性的技術&#xff0c;引領著…