1. 程式人生 > >C/C++:各種基本演算法實現小結(一)—— 單鏈表

C/C++:各種基本演算法實現小結(一)—— 單鏈表

各種基本演算法實現小結(一)—— 單鏈表

(均已測試通過)

============================================================

單鏈表(測試通過)

測試環境: Win-TC

[cpp] view plain copy  print?
  1. #include <stdio.h>
  2. struct _node  
  3. {  
  4.     int data;  
  5.     struct _node *next;  
  6. };  
  7. typedefstruct _node list;  
  8. void display(list *l)  
  9. {  
  10.     list *p;  
  11.     p=l;  
  12.     while(p->next)  
  13.     {  
  14.         printf("%5d", p->next->data);  
  15.         p=p->next;  
  16.     }  
  17. }  
  18. void main()  
  19. {  
  20.     int i, n;  
  21.     list *h, *p, *s;  
  22.     printf("Enter num n:");  
  23.     scanf("%d", &n);  
  24.     h=(list*)malloc(sizeof
    (list));  
  25.     h->data=-1;  
  26.     h->next=NULL;  
  27.     s=p=h;  
  28.     for(i=n;i>0;i--)  
  29.     {  
  30.         p=(list*)malloc(sizeof(list));  
  31.         scanf("%d", &(p->data));  
  32.         p->next=h->next;  
  33.         h->next=p;  
  34.         h=h->next;  
  35.     }  
  36.     display(s);  
  37.     getch();  
  38. }  

執行結果:


=================================================

單鏈表各種操作(測試通過)   

測試環境: Win-TC

 print?
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. struct _node  
  5. {  
  6.     int data;  
  7.     struct _node *next;  
  8. };  
  9. typedefstruct _node node, *plist;  
  10. plist init_list()  
  11. {  
  12.     plist pl;  
  13.     pl=(plist)malloc(sizeof(node));  
  14.     if(NULL==pl)  
  15.     {  
  16.         printf("init list,  malloc is fail.../n");  
  17.         return NULL;  
  18.     }  
  19.     pl->data=-1;  
  20.     pl->next=NULL;  
  21.     return pl;  
  22. }  
  23. int isempty_list(plist pl)  
  24. {  
  25.     if(NULL==pl || NULL!=pl->next)  
  26.         return 1;  
  27.     else
  28.         return 0;  
  29. }  
  30. plist clear_list(plist pl)  
  31. {  
  32.     pl=NULL;  
  33.     return pl;  
  34. }  
  35. void destroy_list(plist pl)  
  36. {  
  37.     plist p, s;  
  38.     p=pl->next;  
  39.     while(p)  
  40.     {  
  41.         s=p;  
  42.         p=p->next;  
  43.         free(s);       
  44.     }  
  45.     pl=NULL;  
  46. }  
  47. void insert_item(plist pl, int i, int e)  
  48. {  
  49.     int j=1;  
  50.     plist p, s;  
  51.     p=pl;  
  52.     while(p && j<i)  
  53.     {  
  54.         p=p->next;  
  55.         j++;  
  56.     }  
  57.     if(!p || j>i)  /* >len or <1  */
  58.         printf("Insert fail.../n");  
  59.     s=(plist)malloc(sizeof(node));  
  60.     s->data=e;  
  61.     s->next=p->next;  
  62.     p->next=s;  
  63. }  
  64. void display(plist pl)  
  65. {  
  66.     plist p;  
  67.     p=pl->next;  
  68.     while(pl && p)  
  69.     {  
  70.         printf("%5d", p->data);  
  71.         p=p->next;  
  72.     }  
  73.     printf("/n/n");  
  74. }  
  75. int getbyid_item(plist pl, int i)  
  76. {  
  77.     plist p=pl->next;  
  78.     int j=1;  
  79.     while(p && j<i)  
  80. 相關推薦

    C/C++:各種基本演算法實現小結—— 單鏈

    各種基本演算法實現小結(一)—— 單鏈表 (均已測試通過) ============================================================ 單鏈表(測試通過) 測試環境: Win-TC

    C/C++:各種基本演算法實現小結—— 圖及其遍歷

    各種基本演算法實現小結(四)—— 圖及其遍歷 (均已測試通過) ==================================================================== 圖——深度優先和廣度優先演算法 無向圖

    C/C++:各種基本演算法實現小結—— 查詢演算法

    各種基本演算法實現小結(六)—— 查詢演算法 (均已測試通過) =================================================================== 1、簡單查詢 在一組無序數列中,查詢特定某個數值,並返回其位置

    C/C++:各種基本演算法實現小結—— 常用演算法

    各種基本演算法實現小結(七)—— 常用演算法 (均已測試通過) ====================================================================== 1、判斷素數 測試環境:VC 6.0

    C/C++:各種基本演算法實現小結—— 堆 棧

    各種基本演算法實現小結(二)—— 堆 棧 (均已測試通過) ============================================================== 棧——陣列實現 測試環境:Win - TC

    C/C++:各種基本演算法實現小結—— 樹與二叉樹

    各種基本演算法實現小結(三)—— 樹與二叉樹 (均已測試通過) =================================================================== 二叉樹——先序 測試環境:VC 6.0 (C

    各種基本演算法實現小結—— 排序演算法

    分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

    OpenCV下車牌定位演算法實現程式碼

                 分類:            影象處理

    經典演算法實現——字串

    這篇文章主要介紹字串相關的題目。 處理字串操作相關問題時,常見的做法是從字串尾部開始編輯,從後往前逆向操作。這麼做的原因是因為字串的尾部往往有足夠空間,可以直接修改而不用擔心覆蓋字串前面的資料。 摘自

    紅黑樹插入與刪除 演算法實現+程式碼

    要實現紅黑樹節點的插入刪除,得先實現二叉樹節點插入刪除,在這基礎上加入紅黑樹調整演算法。 今天早上編寫了二叉樹的節點刪除程式碼。結果如下 實踐經驗: 1.要刪除節點,得先遍歷出節點位置,我用陣列存放遍歷出來的結果。然後刪除結果中倒數第三個數字時,遇到了困難: (1)剛

    連結串列初解——單鏈的建立、刪除、插入、測長、排序、逆置

    由於考試需要,複習一下單鏈表的各種常見操作,直接上程式碼+註釋,需要的可以參考下哈~ Code: #include<iostream> using namespace std; typedef struct student { int data; str

    C++ 順序棧基本演算法實現

    C++ 順序棧基本演算法 #ifndef SeqStack_h #define SeqStack_h #include <iostream> using namespace std; const int StackSize = 1024; template <class T>

    C++ 鏈棧 基本演算法實現

    C++ 鏈棧 基本演算法實現 #ifndef LinkStack_h #define LinkStack_h #include <iostream> template <class T> struct Node{ T data; struct Node <

    C++ 迴圈佇列基本演算法實現

    C++ 迴圈佇列基本演算法實現 #ifndef CircleQueue_h #define CircleQueue_h const int QueueSize = 1000; template <class T> class CircleQueue { public: Circl

    C++ 鏈佇列基本演算法實現

    C++ 鏈佇列基本演算法實現 #ifndef LinkQueue_h #define LinkQueue_h #include <iostream> template <class T> struct Node{ T data; struct Node <

    【原始碼】C++實現嚴蔚敏資料結構所有演算法線性-順序

    日常說明:首先博主也是菜鳥一枚,有錯誤歡迎大家指正。另外本部落格所有的程式碼博主編寫後均除錯 通過。重要提醒!!!!博主使用的是VS2017,如果有低版本的小夥伴 最好新建空專案將此程式碼複製上去。 附加說明:最初的程式碼我沒有嚴格的按照專案規範來分離,希望

    各種基本算法實現小結—— 圖及其遍歷

    malloc end type details 幽默 can false 希望 頂點 分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!http://www.captainbed.net 各種基本算法實現

    C++ 實現反射

    反射,就是根據一個類名,即可根據類名獲取類資訊,建立新物件。反射在很多語言都天然支援,然而不包括 C++,但我們肯定會經常遇到這種根據類名生成物件的場景,這就需要我們自己動手來實現了。反正 C++ 這麼強大,一定沒有問題 :) version 1 我們略做思考,就可以想到一種最簡

    各種基本演算法實現總結

    各種基本演算法實現小結(七)—— 常用演算法 (均已測試通過) ====================================================================== 1、判斷素數 測試環境:VC 6.0 (C)

    資料結構:順序基本操作 C語言

    順序表   標頭檔案: Sqlist.h #include<stdio.h> #include<stdlib.h> #define SIZE 15 #pragma once typedef struct Sqlist { int elem[SIZ