C/C++:各種基本演算法實現小結(一)—— 單鏈表
各種基本演算法實現小結(一)—— 單鏈表
(均已測試通過)
============================================================
單鏈表(測試通過)
測試環境: Win-TC
[cpp] view plain copy print?- #include <stdio.h>
- struct _node
- {
- int data;
- struct _node *next;
- };
-
typedefstruct _node list;
- void display(list *l)
- {
- list *p;
- p=l;
- while(p->next)
- {
- printf("%5d", p->next->data);
- p=p->next;
- }
- }
- void main()
- {
- int i, n;
- list *h, *p, *s;
- printf("Enter num n:");
- scanf("%d", &n);
-
h=(list*)malloc(sizeof
- h->data=-1;
- h->next=NULL;
- s=p=h;
- for(i=n;i>0;i--)
- {
- p=(list*)malloc(sizeof(list));
- scanf("%d", &(p->data));
- p->next=h->next;
- h->next=p;
- h=h->next;
- }
-
display(s);
- getch();
- }
執行結果:
=================================================
單鏈表各種操作(測試通過)
測試環境: Win-TC
print?- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- struct _node
- {
- int data;
- struct _node *next;
- };
- typedefstruct _node node, *plist;
- plist init_list()
- {
- plist pl;
- pl=(plist)malloc(sizeof(node));
- if(NULL==pl)
- {
- printf("init list, malloc is fail.../n");
- return NULL;
- }
- pl->data=-1;
- pl->next=NULL;
- return pl;
- }
- int isempty_list(plist pl)
- {
- if(NULL==pl || NULL!=pl->next)
- return 1;
- else
- return 0;
- }
- plist clear_list(plist pl)
- {
- pl=NULL;
- return pl;
- }
- void destroy_list(plist pl)
- {
- plist p, s;
- p=pl->next;
- while(p)
- {
- s=p;
- p=p->next;
- free(s);
- }
- pl=NULL;
- }
- void insert_item(plist pl, int i, int e)
- {
- int j=1;
- plist p, s;
- p=pl;
- while(p && j<i)
- {
- p=p->next;
- j++;
- }
- if(!p || j>i) /* >len or <1 */
- printf("Insert fail.../n");
- s=(plist)malloc(sizeof(node));
- s->data=e;
- s->next=p->next;
- p->next=s;
- }
- void display(plist pl)
- {
- plist p;
- p=pl->next;
- while(pl && p)
- {
- printf("%5d", p->data);
- p=p->next;
- }
- printf("/n/n");
- }
- int getbyid_item(plist pl, int i)
- {
- plist p=pl->next;
- int j=1;
- while(p && j<i)
-
相關推薦
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