日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux C 数据结构---线性表

發布時間:2023/12/9 linux 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux C 数据结构---线性表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?數據結構指的是數據元素及數據元素之間的相互關系,包含下面三方面的內容:

?

????其中,線性表是最基本、最簡單、也是最常用的一種數據結構。線性表中數據元素之間的關系是一對一的關系,即除了第一個和最后一個數據元素之外,其它數據元素都是首尾相接的。線性表的邏輯結構簡單,便于實現和操作。因此,線性表這種數據結構在實際應用中是廣泛采用的一種數據結構。

???? 線性表是一個線性結構,它是一個含有n≥0個結點的有限序列,對于其中的結點,有且僅有一個開始結點沒有前驅但有一個后繼結點,有且僅有一個終端結點沒有后繼但有一個前驅結點,其它的結點都有且僅有一個前驅和一個后繼結點。

特征:

1.集合中必存在唯一的一個“第一元素”;
2.集合中必存在唯一的一個 “最后元素” ;
3.除最后一個元素之外,均有 唯一的后繼(后件);
4.除第一個元素之外,均有 唯一的前驅(前件);

??? 線性表作為一種基本的數據結構類型,在計算機存儲器的映像(或表示)一般有兩種形式:

1、順序映像---即我們常用的數組

2、鏈式映像---即我們常用的鏈表

??? 今天,我們先來學習順序存儲結構:

一、順序存儲結構的表示

??? 1、順序存儲結構的特點:

1)邏輯上相鄰的元素A(i) ?A(i+1),其存儲位置也是相鄰的;

2)對數據元素A(i)的存取為隨機存取或地址存取。

3)存儲密度高。存儲密度D=(數據結構中的元素所占存儲空間)/(整個數據結構所占空間)

???? 2、順序存儲結構的不足:

?? ? 對表的插入和刪除等運算的時間復雜度較差。

?????3、順序存儲結構的表示:

在C語言中,一維數組的元素也是存放于一片連續的存儲空間中,故可借助于C語言中一維數組類型來描述線性表的存儲結構,即

[cpp]?view plaincopy
  • #define?N?100??
  • ??
  • typedef?int?data_t;//這樣定義的目的是為了代碼便于維護,如果下次表中數據類型為char,修改比較方便;??
  • typedef?struct??
  • {??
  • ????data_t?data[N];//表的存儲空間??
  • ????int?last;//當前表尾指針,對我們對數據的定位起到很大的作用??
  • }sqlist_t,*sqlink_t;//順序表類型??
  • 指針 L 指向一個順序表,我們的數據{a0,a1,a2........? last};

    [cpp]?view plaincopy
  • sqlink_t?L;??
  • L?=?(sqlink_t?*)malloc(sizeof(sqlink_t));??
  • 這里我們定義的 int last ,可以表示{? } 、{ a0 }、{a0,a1}、{a0,a1......a99};

    ai可以表示為 L->data[i] (0<=i<=L->last),一般情況下,0 < L->last < N,如果是空表{? },此時L->last = -1;

    ?

    下面我們通過一個實例來看線性表基本算法的相關算法如何使用:

    seqlist.h

    [cpp]?view plaincopy
  • #ifndef?_SEQ_LIST_H_??
  • #define?_SEQ_LIST_H_??
  • ??
  • #include?"datatype.h"??
  • ??
  • #define?MAX?100??
  • ??
  • typedef?struct?{??
  • ????data_t??data[MAX];??
  • ????int?last;???/*?pointer?to?the?position??
  • ?????????????*?in?the?array?'data'?where??
  • ?????????????*?stores?the?last?element?of?
  • ?????????????*?the?list?
  • ?????????????*/??
  • }?seqlist_t;??
  • ??
  • /*??
  • ?*?create?a?list?and?init?it?as?empty??
  • ?*?Input?:?void?
  • ?*?Output:?void?
  • ?*?Return:?new?list,?NULL?when?failed??
  • ?*/??
  • seqlist_t?*CreateEmptySqlist();??
  • ??
  • /*??
  • ?*?destroy?a?list??
  • ?*?Input?:?the?list?to?be?destroied.??
  • ?*?Output:?void?
  • ?*?Return:?void?
  • ?*/??
  • void?DestroySqlist(seqlist_t?*list);??
  • ??
  • /*?
  • ?*?clear?the?list?and?reset?it?as?empty?
  • ?*?Input?:?the?list?to?be?cleared.??
  • ?*?Output:?void?
  • ?*?Return:?void?
  • ?*/??
  • void?ClearSqlist(seqlist_t?*list);??
  • ??
  • /*??
  • ?*?judge?if?the?list?is?empty?
  • ?*?Input:???the?list?to?be?tested.??
  • ?*?Output:??void?
  • ?*?Return:?
  • ?*??1:??list?is?empty?
  • ?*??0:??not??
  • ?*??-1:?error,?e.g.?the?list?is?invalid?
  • ?*/??
  • int?EmptySqlist(seqlist_t?*list);??
  • ??
  • /*??
  • ?*?judge?if?the?list?is?full??
  • ?*?Input?:?the?list?to?be?tested.??
  • ?*?Output:?void?
  • ?*?Return:?
  • ?*??1?:?list?is?full?
  • ?*??0?:?not??
  • ?*??-1:?error?
  • ?*/??
  • int?FullSqlist(seqlist_t?*list);??
  • ??
  • /*??
  • ?*?get?length?of?the?list??
  • ?*?Input?:?the?list?to?be?tested.??
  • ?*?Output:?void?
  • ?*?Return:??
  • ?*??>=?0:?length?of?the?list;?
  • ?*???-1?:?means?error??
  • ?*/??
  • int?LengthSqlist(seqlist_t?*list);??
  • ??
  • /*?
  • ?*?get?data?of?element?at?specified?position?
  • ?*?Input?:??
  • ?*??list?:??the?list?to?be?operated.?
  • ?*??at?:????the?position?where?to?get?the?element?at,??
  • ?*??????position?index?starts?from?zero.?
  • ?*?Output:?
  • ?*??x?:?the?data?value?returned?
  • ?*?Return:?
  • ?*??0?:?success;?
  • ?*??-1:?error,?e.g.?list?is?invalid;?'at'?extends??
  • ?*??????the?range?of?the?list?????
  • ?*/??
  • int?GetSqlist(seqlist_t?*list,?int?at,?data_t?*x);??
  • ??
  • /*?
  • ?*?set/update?data?of?element?at?specified?position?
  • ?*?Input?:??
  • ?*??list?:??the?list?to?be?operated.?
  • ?*??at?:????the?position?at?where?to?set?the?element,??
  • ?*??????position?index?starts?from?zero?
  • ?*??x?:?the?new?data?value?
  • ?*?Output:?void?
  • ?*?Return:?
  • ?*??0?:?success;?
  • ?*??-1:?error,?e.g.?list?is?invalid;?'at'?extends?the??
  • ?*??????range?of?the?list????
  • ?*/??
  • int?SetSqlist(seqlist_t?*list,?int?at,?data_t?x);??
  • ??
  • /*??
  • ?*?Insert?element?at?the?specified?position.?If?the?"at"?exceed?the??
  • ?*?upper?limit?of?the?list,?append?the?data?at?the?end?of?the?list.??
  • ?*?e.g.?insert?a?new?data?into?a?{}.?
  • ?*?Input?:??
  • ?*??list?:??the?list?to?be?operated.?
  • ?*??at?:????the?position?at?which?to?insert?the?new?element,??
  • ?*??????position?index?starts?from?zero.?
  • ?*??x?:?the?data?to?be?inserted??
  • ?*?Output:?void?
  • ?*?Return:?
  • ?*??0?:?success;??
  • ?*??<0:?error??
  • ?*/??
  • int?InsertSqlist(seqlist_t?*list,?int?at,?data_t?x);??
  • ??
  • /*?
  • ?*?delete?the?element?by?the?position?
  • ?*?Input?:??
  • ?*??list?:??the?list?to?be?operated.?
  • ?*??at?:????the?position?at?which?to?delete?the?element,??
  • ?*??????position?index?starts?from?zero?
  • ?*?Output?:?void?
  • ?*?Return?:?
  • ?*??0?:?success;?
  • ?*??!0?:????error???
  • ?*/??
  • int?DeleteSqlist(seqlist_t?*list,?int?at);??
  • ??
  • #endif?/*?_SEQ_LIST_H_?*/??
  • seqlist.c

    [cpp]?view plaincopy
  • #include?<stdio.h>??
  • #include?<stdlib.h>??
  • #include?"seqlist.h"??
  • ??
  • seqlist_t?*CreateEmptySqlist()??
  • {??
  • ????seqlist_t?*list;??
  • ????list?=?(seqlist_t?*)malloc(sizeof(seqlist_t));??
  • ????if(list?!=?NULL)??
  • ????{??
  • ????????list->last?=?-1;//list->last初始化,空表中,list-last?=?-1;??
  • ????}??
  • ??
  • ????return?list;??
  • }??
  • ??
  • void?DestroySqlist(seqlist_t?*list)??
  • {??
  • ????if?(list?==?NULL)??
  • ????????return?;??
  • ??????
  • ????free(list);??
  • ??
  • ????return;??
  • }??
  • ??
  • void?ClearSqlist(seqlist_t?*list)??
  • {??
  • ????if?(list?==?NULL)??
  • ????????return?;??
  • ??
  • ????list->last?=?-1;//清空線性表??
  • ??
  • }??
  • ??
  • int?EmptySqlist(seqlist_t?*list)??
  • {??
  • ????if?(list?==?NULL)??
  • ????????return?-1;??
  • ??????
  • ????if(list->last?==?-1)//空表的標志??
  • ????????return?1;??
  • ????else??
  • ????????return?0;??
  • }??
  • ??
  • int?FullSqlist(seqlist_t?*list)??
  • {??
  • ????if?(list?==?NULL)??
  • ????????return?-1;??
  • ??
  • ????if?(MAX?-?1?==?list->last)??
  • ????????return?1;??
  • ????else??
  • ????????return?0;??
  • }??
  • ??
  • int?LengthSqlist(seqlist_t?*list)??
  • {??
  • ????if?(list?==?NULL)??
  • ????????return?-1;??
  • ????else??
  • ????????return?(list->last?+?1);??
  • }??
  • ??
  • int?GetSqlist(seqlist_t?*list,?int?at,?data_t?*x)?//data_t?*x為出參??
  • {??
  • ????if(list?==?NULL?||?at?<?0?||?at?>?list->last)??
  • ????????return?-1;??
  • ??
  • ????*x?=?list->data[at];??
  • ??
  • ????return?0;??
  • }??
  • ??
  • int?SetSqlist(seqlist_t?*list,?int?at,?data_t?x)//data_t?x為入參??
  • {??
  • ????if(list?==?NULL?||?at?<?0?||?(at?>?list->last))??
  • ????????return?-1;??
  • ??
  • ????list->data[at]?=?x;??
  • ??
  • ????return?0;??
  • }??
  • ??
  • int?InsertSqlist(seqlist_t?*list,?int?at,?data_t?x)??
  • {??
  • ????int?j;??
  • ????if(list?==?NULL?||?at?<?0?||?FullSqlist(list))??
  • ????????return?-1;??
  • ??
  • ????if(at?>?list->last)//此情況比較特殊,如表中長度是50,卻要插在60前面,我們這里將其插在50后面,即at?=?list->last?+?1??
  • ????{??
  • ????????at?=?list->last?+?1;//若是空表{},list->last為-1,此時將其放在第0位;??
  • ????}??
  • ????else??
  • ????{??
  • ????????for(j?=?list->last;?j?>=?at;?j--)??
  • ????????{??
  • ????????????list->data[j+1]?=?list->data[j];??
  • ????????}??
  • ????}??
  • ??
  • ????list->data[at]?=?x;??
  • ????list->last++;//list->last要+1??
  • ??
  • ????return?0;??
  • }??
  • ??
  • int?DeleteSqlist(seqlist_t?*list,?int?at)??
  • {??
  • ????int?i;??
  • ????if(list?==?NULL?||?at?<?0?||?(at?>?list->last))??
  • ????????return?-1;??
  • ??
  • ????for(i?=?at?+?1;?i?<=?list->last;i++)??
  • ????????list->data[i-1]?=?list->data[i];??
  • ??
  • ????list->last--;??
  • ??
  • ????return?0;??
  • }??
  • main.c

    [cpp]?view plaincopy
  • #include?<stdio.h>??
  • #include?<stdlib.h>??
  • #include?"seqlist.h"??
  • #include?"datatype.h"??
  • ??
  • void?iterate_list(seqlist_t?*list)??
  • {??
  • ????int?i;??
  • ??????
  • ????printf("list.last?=?%d,?list?=?{",?list->last);??
  • ??????
  • ????for?(i?=?-1;?i?<?list->last;)?{??
  • ????????printf("%d,",?list->data[++i]);??
  • ????}??
  • ??????????
  • ????if?(LengthSqlist(list)?>?0)??
  • ????????printf("\b}\n");??
  • ????else??
  • ????????printf("}\n");??
  • ??
  • }??
  • ??
  • int?main(int?argc,?const?char?*argv[])??
  • {??
  • ????int?i;??
  • ????int?length;??
  • ????data_t?a[10]?=?{2,4,6,8,10,12,14,16,18,20};??
  • ????data_t?x;??
  • ????seqlist_t?*list;??
  • ????list?=?CreateEmptySqlist();??
  • ??
  • ????if?(list?==?NULL)??
  • ????????return?-1;??
  • ??
  • ????for(i?=?0;?i?<?10;i++)??
  • ????{??
  • ????????if((InsertSqlist(list,i,a[i]))?<?0)??
  • ????????????break;??
  • ????}??
  • ????iterate_list(list);??
  • ????length?=?LengthSqlist(list);??
  • ????printf("The?length?of?the?list?is?%d\n",length);??
  • ??
  • ????GetSqlist(list,4,&x);??
  • ????printf("The?NO.4?data?of?the?list?is?%d\n",x);??
  • ????SetSqlist(list,4,5);??
  • ????GetSqlist(list,4,&x);??
  • ????printf("After?updataing!?The?NO.4?data?of?the?list?is?%d\n",x);??
  • ??
  • ????printf("Delete?data[4]!\n");??
  • ????DeleteSqlist(list,4);??
  • ????GetSqlist(list,4,&x);??
  • ????printf("Now?data[4]?=?%d\n",x);??
  • ????printf("Now?the?legth?of?the?list?is?%d\n",LengthSqlist(list));??
  • ??
  • ????ClearSqlist(list);??
  • ????if(EmptySqlist(list))??
  • ????????printf("The?list?is?empty!\n");??
  • ??
  • ????printf("Now?the?legth?of?the?list?is?%d\n",LengthSqlist(list));??
  • ??
  • ????DestroySqlist(list);??
  • ??
  • ????printf("The?list?is?destroyed!\n");??
  • ??
  • ????return?0;??
  • }??
  • makefile:

    [cpp]?view plaincopy
  • OBJS?=?seqlist.o?main.o??
  • CFLAGS?=?-c?-g?-Wall??
  • CC?=?gcc??
  • ??
  • Test:$(OBJS)??
  • ????$(CC)?-o?Test?-g?$(OBJS)??
  • main.o:main.c?seqlist.h?datatype.h??
  • ????$(CC)?$(CFLAGS)?main.c??
  • seqlist.o:seqlist.c?seqlist.h?datatype.h??
  • ????$(CC)?$(CFLAGS)?seqlist.c??
  • ??
  • clean:??
  • ????rm?-rf?Test?*.o??
  • 執行結果如下:

    [cpp]?view plaincopy
  • fs@ubuntu:~/qiang/list$?make??
  • gcc?-c?-g?-Wall?seqlist.c??
  • gcc?-c?-g?-Wall?main.c??
  • gcc?-o?Test?-g?seqlist.o?main.o??
  • fs@ubuntu:~/qiang/list$?./Test???
  • list.last?=?9,?list?=?{2,4,6,8,10,12,14,16,18,20}??
  • The?length?of?the?list?is?10??
  • The?NO.4?data?of?the?list?is?10??
  • After?updataing!?The?NO.4?data?of?the?list?is?5??
  • Delete?data[4]!??
  • Now?data[4]?=?12??
  • Now?the?legth?of?the?list?is?9??
  • The?list?is?empty!??
  • Now?the?legth?of?the?list?is?0??
  • The?list?is?destroyed!?
  • 總結

    以上是生活随笔為你收集整理的Linux C 数据结构---线性表的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。