資料結構:連結串列的初始化插入和刪除2.3.1

gaopengtttt發表於2016-09-14
前面我們已經說了線性表的順序實現,
http://blog.itpub.net/7728585/viewspace-2124937/
下面我們將討論一下線性表的連結串列實現。
連結串列結構使用得非常多,不管是作業系統還是資料庫都是使用非常頻繁的一種資料結構,
由於其相對靈活的記憶體使用,並且快速的插入和刪除,都是非常有優勢的。
這裡通過C語言實現5個功能:
1、使用線性表的順序結構初始化連結串列結構
2、初始化連結串列結構為指定的大小
3、獲取連結串列中的指定位置的元素
4、在指定位置後面插入一個元素
5、刪除指定位置的元素
先來看一個大概的圖,說明是是加NODENEW加入到節點NODE1後面的情況,實際上刪除也是類似,具體實現看程式碼

首先我們需要定義個頭結點指向第一個NODE,NODE中有NEXT指標指向下一個結點,在這種結構中,
檢視固定元素的時間複雜度最壞也是O(n),而插入一元素時間複雜度為O(1),刪除一個元素複雜度也是O(1)
但是我們應該清楚如果要插入指定位置或者刪除指定位置的元素首先需要的是查詢,那麼需要的時間則是他們
相加。來看C語言實現,在整個程式碼檔案中沒有使用標頭檔案導致每個檔案都需要定義一些需要的定義!同時我
使用了函式過載來實現
1、使用線性表的順序結構初始化連結串列結構
2、初始化連結串列結構為指定的大小
不可避免的引入了一點點C++的知識

點選(此處)摺疊或開啟

  1. 順序表實現:
  2. /*************************************************************************
  3.   > File Name: sqlist.cpp
  4.   > Author: gaopeng
  5.   > Mail: gaopp_200217@163.com
  6.   > Created Time: Wed 05 Oct 2016 02:44:35 AM CST
  7.  ************************************************************************/

  8. #include<iostream>
  9. #include<stdio.h>
  10. #include<stdlib.h>
  11. #include<string.h>
  12. #define INITSIZE 10

  13. typedef unsigned int uint;
  14. typedef int Etype;

  15. typedef struct sqlist
  16. {
  17.         Etype* elem; //pointer of sqlist base address
  18.         uint length; //current length of elem
  19.         uint m_size; //
  20. } SQLIST;


  21. void initsqlist(SQLIST* inlist)
  22. {

  23.         inlist->elem = (Etype* )malloc(sizeof(Etype)*INITSIZE + 1);
  24.         inlist->length = 0;
  25.         inlist->m_size = INITSIZE; //maxsize
  26. }


  27. void initsqlinsert(SQLIST* inlist,Etype ielem,uint postion) //insert elem before postion
  28. {

  29.         int i;
  30.         Etype* newbase;
  31.         if(postion > inlist->length + 1 || postion < 1)
  32.         {

  33.                 printf("line table must continuous or postion must>0!\n");
  34.                 exit(1);
  35.         }

  36.         if(inlist->length + 1 >= inlist->m_size ) //if memory small will give more memory
  37.         {

  38.                 if(!(newbase =(Etype* )realloc(inlist->elem,(inlist->length+INITSIZE)* sizeof(Etype)+1)))
  39.                 {

  40.                         printf("mem alloc failed!\n");
  41.                         exit(2);
  42.                 }
  43.                 inlist->elem = newbase;
  44.                 inlist->m_size = inlist->m_size+INITSIZE;
  45.         }

  46.         for(i=0;i<(inlist->length-postion+2);i++) //every elem houyi
  47.         {

  48.                 *(inlist->elem+inlist->length+1-i) = * (inlist->elem+inlist->length-i);
  49.         }
  50.         *(inlist->elem+inlist->length+1-i) = ielem; //give value
  51.         inlist->length =inlist->length+1;
  52. }

  53. void delsqldel(SQLIST* inlist,uint postion) //delete elem of postion
  54. {

  55.         int i;
  56.         if((postion > inlist->length) ||(postion <1))
  57.         {

  58.                 printf("give postion is must large than 1 and less than current length\n");
  59.         }

  60.         for(i=0;i<(inlist->length-postion) ;i++)
  61.         {

  62.                 *(inlist->elem+postion+i) = *(inlist->elem+postion+i+1);
  63.         }
  64.         inlist->length =inlist->length-1;
  65. }

  66. void view(SQLIST* inlist)
  67. {

  68.         int i;
  69.         if(inlist->length ==0 )
  70.         {

  71.                 printf("init data arrary! no data found!\n");
  72.                 exit(3);
  73.         }
  74.         for(i=0;i<inlist->length;i++)
  75.         {

  76.                 printf("node:%d values is:%d data length:%d max_size:%d \n",i,*(inlist->elem+i),inlist->length,inlist->m_size);
  77.         }
  78. }


點選(此處)摺疊或開啟

  1. 連結串列實現:
  2. #include<iostream>
  3. #include<stdio.h>
  4. #include <stdlib.h>
  5. typedef int datatype;
  6. typedef int Etype;
  7. typedef unsigned int uint;

  8. typedef struct node
  9. {
  10.         datatype data;
  11.         struct node *next;
  12. } Node,*Nodep;

  13. typedef struct hnode
  14. {
  15.         int clenth;
  16.         Nodep headp;
  17. } Hnode,*Hnodep;


  18. typedef struct sqlist
  19. {
  20.         Etype* elem; //pointer of sqlist base address
  21.         uint length; //current length of elem
  22.         uint m_size; //
  23. } SQLIST;

  24. void initchain(Hnodep hnode,int n)
  25. {
  26.         Nodep p;
  27.         int i;
  28.         hnode->clenth = 0;
  29.         hnode->headp = (Nodep)malloc(sizeof(Node));
  30.         hnode->headp->next = NULL;
  31.         hnode->headp->data = 0;
  32.         (hnode->clenth)++;
  33.         for(i=(n-1);i>0;--i)
  34.         {
  35.                 p = (Nodep)malloc(sizeof(Node));
  36.                 p->next = hnode->headp->next;
  37.                 hnode->headp->next = p;
  38.                 p->data = 0;
  39.                 (hnode->clenth)++;
  40.         }
  41. }


  42. void initchain(Hnodep hnode,const SQLIST* sqdata)
  43. {
  44.         Nodep p;
  45.         int i;
  46.         int t=1;
  47.         int sqlen = sqdata->length;
  48.         hnode->clenth = 0;
  49.         hnode->headp = (Nodep)malloc(sizeof(Node));
  50.         hnode->headp->next = NULL;
  51.         hnode->headp->data = *(sqdata->elem);
  52.         (hnode->clenth)++;
  53.         sqlen--;
  54.         for(i=sqlen;i>0;--i)//every time insert elem after first elem and before seconed elem
  55.         {
  56.                 p = (Nodep)malloc(sizeof(Node));
  57.                 p->next = hnode->headp->next;
  58.                 hnode->headp->next = p;
  59.                 p->data = *(sqdata->elem+t);
  60.                 (hnode->clenth)++;
  61.                 t++;
  62.         }
  63. }


  64. void viewchain(Hnodep hnode)
  65. {
  66.         int i=1;
  67.         Nodep p;
  68.         int max_len;
  69.         p = hnode->headp;
  70.         max_len = hnode->clenth;

  71.         printf("Max chain length is:%d\n",max_len);
  72.         do
  73.         {
  74.                 printf("node:%d values is: %d\n",i,p->data);
  75.                 i++;

  76.         }while(p=p->next);
  77. }

  78. void getelem(Hnodep hnode,int postion)
  79. {
  80.         int i=0;
  81.         Nodep p;
  82.         if(postion > hnode->clenth || postion ==0 )
  83.         {
  84.                 printf("postion large than lenth or poastion = 0\n");
  85.                 exit(4);
  86.         }
  87.         p = hnode->headp;

  88.         while(i<postion-1)
  89.         {
  90.                 i++;
  91.                 p = p->next;
  92.         }

  93.         printf("node %d values is %d\n",i+1,p->data);

  94. }


  95. Nodep getelemp(const Hnodep hnode,int postion) //insert one elem after postion
  96. {
  97.         int i=0;
  98.         Nodep p;
  99.         if(postion > hnode->clenth || postion ==0 )
  100.         {
  101.                 printf("postion large than lenth or poastion = 0\n");
  102.                 exit(5);
  103.         }
  104.         p = hnode->headp;

  105.         while(i<postion-1)
  106.         {
  107.                 i++;
  108.                 p = p->next;
  109.         }

  110.         return p;

  111. }

  112. void addnode(Nodep inode,int postion,Hnodep hnode)//insert one elem after postion
  113. {
  114.         Nodep p;
  115.         p = getelemp(hnode,postion);
  116.         if(!p->next) //end elem?
  117.         {
  118.                 p->next = inode;
  119.                 inode->next = NULL;
  120.         }
  121.         else
  122.         {
  123.                 inode->next = p->next;
  124.                 p->next = inode;
  125.         }
  126.         hnode->clenth++;
  127. }

  128. void delnode(int postion,Hnodep hnode) //delete elem at give postion
  129. {
  130.         Nodep p1; //delete postion-1 p
  131.         Nodep p2; //delete postion p

  132.         if(postion == 1)
  133.         {
  134.                 p2 = hnode->headp->next;
  135.                 hnode->headp->next = hnode->headp->next->next;
  136.                 free(p2);
  137.         }
  138.         else
  139.         {
  140.                 p1=getelemp(hnode,postion-1);//find before delete node postion
  141.                 p2 = p1->next;
  142.                 p1->next = p1->next->next;
  143.                 free(p2);
  144.         }
  145.         hnode->clenth--;
  146. }


點選(此處)摺疊或開啟

  1. main 函式:
  2. #include<iostream>
  3. #include<stdio.h>
  4. #include<stdlib.h>


  5. typedef unsigned int uint;
  6. typedef int Etype;
  7. typedef int datatype;

  8. typedef struct node
  9. {

  10.         datatype data;
  11.         struct node *next;
  12. } Node,*Nodep;

  13. typedef struct hnode
  14. {

  15.         int clenth;
  16.         Nodep headp;
  17. } Hnode,*Hnodep;


  18. typedef struct sqlist
  19. {

  20.         Etype* elem; //pointer of sqlist base address
  21.         uint length; //current length of elem
  22.         uint m_size; //
  23. } SQLIST;

  24. void initchain(Hnodep hnode,int n);
  25. void initchain(Hnodep hnode,const SQLIST* sqdata);
  26. void viewchain(Hnodep hnode);
  27. void view(SQLIST* inlist);
  28. void delsqldel(SQLIST* inlist,uint postion);
  29. void initsqlist(SQLIST* inlist);
  30. void initsqlinsert(SQLIST* inlist,Etype ielem,uint postion);
  31. void getelem(Hnodep hnode,int postion);
  32. void addnode(Nodep inode,int postion,const Hnodep hnode);
  33. void delnode(int postion,Hnodep hnode);


  34. int main(void)
  35. {
  36.         SQLIST a;
  37.         Hnode chd1;
  38.         Nodep newnode = (Nodep)malloc(sizeof(Node));
  39.         newnode->data=50;
  40.         newnode->next=NULL;
  41.         initsqlist(&a);
  42.         printf("insert 5 values use seq mode\n");
  43.         initsqlinsert(&a,5,1);
  44.         initsqlinsert(&a,10,1);
  45.         initsqlinsert(&a,15,1);
  46.         initsqlinsert(&a,20,1);
  47.         initsqlinsert(&a,25,1);
  48.         view(&a);
  49.         printf("\n\ninit chain use seq arrary\n");
  50.         initchain(&chd1,&a);
  51.         viewchain(&chd1);
  52.         printf("\n\nview one node at postion 3\n");
  53.         getelem(&chd1,3);
  54.     printf("\n\nadd one node after node 2\n");
  55.         addnode(newnode,2,&chd1);
  56.         viewchain(&chd1);
  57.         printf("\n\nadd one node at postion 2\n");
  58.         delnode(2,&chd1);
  59.         viewchain(&chd1);
  60.         return 0;
  61. }


編譯:
g++ main.cpp chain.cpp sqlist.cpp  -W
執行:
gaopeng@bogon:~/datas/part2/chain$ ./a.out 
insert 5 values use seq mode
node:0 values is:25 data length:5 max_size:10 
node:1 values is:20 data length:5 max_size:10 
node:2 values is:15 data length:5 max_size:10 
node:3 values is:10 data length:5 max_size:10 
node:4 values is:5 data length:5 max_size:10 

init chain use seq arrary
Max chain length is:5
node:1 values is: 25
node:2 values is: 5
node:3 values is: 10
node:4 values is: 15
node:5 values is: 20

view one node at postion 3
node 3 values is 10

add one node after node 2
Max chain length is:6
node:1 values is: 25
node:2 values is: 5
node:3 values is: 50
node:4 values is: 10
node:5 values is: 15
node:6 values is: 20

add one node at postion 2
Max chain length is:5
node:1 values is: 25
node:2 values is: 50
node:3 values is: 10
node:4 values is: 15
node:5 values is: 20

可以看到我初始化的連結串列為 25 5 10 15 20 檢視第三個元素為 10 在位置2後面插入一個元素50 變為
 25 5 50 10 15 20 刪除位置2元素變為了 25  50 10 15 20 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2125007/,如需轉載,請註明出處,否則將追究法律責任。

相關文章