嵌入式Linux C語言基礎——ARM Linux內核常見數據結構

1、單向鏈表、雙向鏈表、循環鏈表

 

  1. #include<stdio.h> 
  2. #include<stdlib.h> 
  3. #include<malloc.h> 
  4. #include<string.h> 
  5. struct STU 
  6. {char name[10]; 
  7. char stuno[10]; 
  8. int age; 
  9. int score; 
  10. }; 
  11. typedef struct STU ElemType; 
  12. struct LNODE 
  13. ElemType data; 
  14. struct LNODE *next; 
  15. }; 
  16. typedef struct LNODE LNode; 
  17. typedef struct LNODE *LinkList; 
  18.  
  19. int init(LinkList *L) 
  20. {*L=(LNode *)malloc(sizeof(LNode)); 
  21.   if(!L) 
  22.    exit(0); 
  23.  memset(&((*L)->data),0,sizeof(struct STU)); 
  24.  (*L)->next=NULL; 
  25.  return 1; 
  26.  
  27. int ListInsert(LinkList L,int i,ElemType e) 
  28. LinkList p,s; 
  29. int j=0; 
  30. p=L; 
  31.  
  32. while(p&&j<i-1) 
  33. {p=p->next; 
  34. ++j; 
  35. if(!p||j>i-1) return 0; 
  36.  
  37. s=(LinkList)malloc(sizeof(LNode)); 
  38. s->data=e; 
  39.  
  40. s->next=p->next; 
  41. p->next=s; 
  42. return 1; 
  43.  
  44. int Less_EqualList(ElemType *e1,ElemType *e2) 
  45. {if(strcmp(e1->name,e2->name)<=0) 
  46.    return 1; 
  47.   else 
  48.    return 0; 
  49.  
  50. void MergeList(LinkList La,LinkList Lb,LinkList *Lc) 
  51.  LinkList pa,pb,pc; 
  52.  pa=La->next; 
  53.  pb=Lb->next; 
  54.  *Lc=pc=La; 
  55.  while(pa&&pb) 
  56.  { 
  57.   if(Less_EqualList(&pa->data,&pb->data)) 
  58.     {pc->next=pa;pc=pa;pa=pa->next;} 
  59.   else 
  60.     {pc->next=pb;pc=pb;pb=pb->next;} 
  61.  } 
  62.  pc->next=pa?pa:pb; 
  63.  free(Lb); 
  64.  
  65. void printlist(LinkList L) 
  66. int i; 
  67. LinkList p; 
  68. p=L; 
  69. printf("name      stuno     age     score\n"); 
  70. while(p->next) 
  71. {p=p->next; 
  72. printf("%-10s %s\t%d\t%d\n",p->data.name,p->data.stuno,p->data.age,p->data.score);} 
  73. printf("\n"); 
  74.  
  75. //#include<stdio.h> 
  76. //#include<malloc.h> 
  77. //#include<stdlib.h> 
  78. main() 
  79. struct STU e; 
  80. LinkList La,Lb,Lc; 
  81. printf("First is InsertList function.\n"); 
  82. init(&La); 
  83. strcpy(e.name,"stu4"); 
  84. strcpy(e.stuno,"100001"); 
  85. e.age=80; 
  86. e.score=1000; 
  87. ListInsert(La,1,e); 
  88.  
  89. strcpy(e.name,"stu6"); 
  90. strcpy(e.stuno,"100002"); 
  91. e.age=80; 
  92. e.score=1000; 
  93. ListInsert(La,2,e); 
  94. printlist(La); 
  95. getchar(); 
  96.  
  97. strcpy(e.name,"stu8"); 
  98. strcpy(e.stuno,"100003"); 
  99. e.age=80; 
  100. e.score=1000; 
  101. ListInsert(La,3,e); 
  102. printlist(La); 
  103. getchar(); 
  104.  
  105. init(&Lb); 
  106. strcpy(e.name,"stu5"); 
  107. strcpy(e.stuno,"100001"); 
  108. e.age=80; 
  109. e.score=1000; 
  110. ListInsert(Lb,1,e); 
  111.  
  112. strcpy(e.name,"stu7"); 
  113. strcpy(e.stuno,"100002"); 
  114. e.age=80; 
  115. e.score=1000; 
  116. ListInsert(Lb,2,e); 
  117.  
  118. strcpy(e.name,"stu1"); 
  119. strcpy(e.stuno,"100003"); 
  120. e.age=80; 
  121. e.score=1000; 
  122. ListInsert(Lb,3,e); 
  123. printlist(Lb); 
  124. getchar(); 
  125.  
  126. MergeList(La,Lb,&Lc); 
  127. printlist(Lc); 
  128. getchar(); 

 

2、二叉樹、平衡樹

 

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <malloc.h> 
  4.  
  5. struct TNode{ 
  6. char data; 
  7. struct TNode *lchild; 
  8. struct TNode *rchild; 
  9. }; 
  10. typedef struct TNode Node; 
  11.  
  12. void init(Node **node) 
  13. {  
  14.   *node=(Node *)malloc(sizeof(Node)); 
  15.   (*node)->lchild=(*node)->rchild=NULL; 
  16.   (*node)->data=0; 
  17.  
  18. void construct(char data,Node **node) 
  19.  int i; 
  20.  Node *temp_node=*node; 
  21.  while(temp_node) 
  22.  { 
  23.   if(!temp_node->data) 
  24.    {temp_node->data=data; 
  25.     break
  26.     } 
  27.  
  28.   else if(data<=temp_node->data) 
  29.   {if(!temp_node->lchild) 
  30.    {init(&temp_node->lchild); 
  31.     temp_node->lchild->data=data; 
  32.     break;} 
  33.    else 
  34.    {temp_node=temp_node->lchild; 
  35.     continue;} 
  36.    } 
  37.  
  38.   else if(data>temp_node->data) 
  39.   { 
  40.    if(!temp_node->rchild) 
  41.    {init(&temp_node->rchild); 
  42.     temp_node->rchild->data=data; 
  43.     break;} 
  44.    else 
  45.    {temp_node=temp_node->rchild; 
  46.     continue;} 
  47.   } 
  48.  } 
  49.   return
  50.  
  51. int PreOrderTraverse(Node *tree_node) 
  52.    if(tree_node) 
  53.    {if(printf("%c ",tree_node->data)) 
  54.      if(PreOrderTraverse(tree_node->lchild)) 
  55.        if(PreOrderTraverse(tree_node->rchild)) 
  56.             return 1; 
  57.      return 0; 
  58.    } 
  59.   else 
  60.     return 1; 
  61.  
  62. void main() 
  63.  int i; 
  64.  Node *root; 
  65.  char data[8]={'e','f','h','g','a','c','b','d'}; 
  66.  init(&root); 
  67.   for(i=0;i<8;i++) 
  68.    construct(data[i],&root); 
  69.  printf("PreOrder.............\n"); 
  70.  PreOrderTraverse(root); 

 

3、哈希表

 實現一個採用開鏈址法避免衝突的哈希表,哈希函數爲key%10,哈希鍵值如下所示:10,23,34,53,67,865,896,54,46,12,45,2345,43,234

 

  1. #include "stdio.h" 
  2. #include "stdlib.h" 
  3. #include "string.h" 
  4. #include "malloc.h" 
  5. #define NUM 10 
  6. #define KEYNUM 14 
  7. struct elem 
  8. {int k; 
  9.  int si; 
  10. }; 
  11. typedef struct elem ElemType; 
  12.  
  13. struct node  
  14. {ElemType data; 
  15.  struct node *next; 
  16. }; 
  17. typedef struct node Node; 
  18. typedef struct node *List; 
  19.  
  20. ElemType key[KEYNUM];  
  21. List table[NUM]; 
  22.  
  23. void InitKey() 
  24. {int i; 
  25.   key[0].k=10; 
  26.   key[1].k=23; 
  27.   key[2].k=34; 
  28.   key[3].k=53; 
  29.   key[4].k=67; 
  30.   key[5].k=865; 
  31.   key[6].k=896; 
  32.   key[7].k=54; 
  33.   key[8].k=46; 
  34.   key[9].k=12; 
  35.   key[10].k=45; 
  36.   key[11].k=2345; 
  37.   key[12].k=43; 
  38.   key[13].k=234; 
  39.  for(i=0;i<KEYNUM;i++) 
  40.  {key[i].si=0; 
  41.  } 
  42.  printf("InitKey() success!!!\n"); 
  43.  
  44. void InitTable(List *L) 
  45.  *L=(Node *)malloc(sizeof(Node)); 
  46.   if(!L) 
  47.     exit(0); 
  48.  memset(&((*L)->data),0,sizeof(struct elem)); 
  49.  (*L)->next=NULL; 
  50.  
  51. void TableInsert(List L,ElemType e) 
  52. {List p,s; 
  53.  p=L; 
  54.  s=(Node *)malloc(sizeof(Node)); 
  55.  s->data=e; 
  56.  s->next=p->next; 
  57.  p->next=s; 
  58.  
  59. void CreateList() 
  60. {int i,index; 
  61.  int num[NUM]; 
  62.  ElemType el; 
  63.  List p; 
  64.  
  65.  for(i=0;i<NUM;i++) 
  66.   {num[i]=0; 
  67.    InitTable(&table[i]); 
  68.   } 
  69.  for(i=0;i<KEYNUM;i++) 
  70.   {index=(key[i].k)%10; 
  71.    num[index]++; 
  72.    el.k=key[i].k; 
  73.    el.si=num[index]; 
  74.    index=index-2; 
  75.    TableInsert(&table[index],el); 
  76.   } 
  77.  
  78. void FindList() 
  79. {int i,g=0,ch,index; 
  80.  List pr; 
  81.   printf("Please Enter A Key:\n"); 
  82.   scanf("%d",&ch); 
  83.   index=ch%10; 
  84.   index=index; 
  85.   pr=table[index]; 
  86.   do 
  87.    {if(pr->data.k==ch) 
  88.      {g=1; 
  89.       printf("The address:%d\t The Key:%d \t Search Length:%d \n",index,pr->data.k,pr->data.si);} 
  90.      pr=pr->next; 
  91.    } 
  92.    while(pr); 
  93.   if(g==0) printf("no relative key\n"); 
  94.  
  95. void DisplayList() 
  96. {int i; 
  97.  List pr; 
  98.   printf("Address \t\t  The Key \t\t Search Length \n"); 
  99.   for(i=0;i<NUM;i++) 
  100.   { pr=table[i]; 
  101.     printf("%d \t ",i); 
  102.     do
  103.        printf("%d \t  %d \t",pr->data.k,pr->data.si); 
  104.        pr=pr->next; 
  105.       } 
  106.     while(pr); 
  107.    printf("\n"); 
  108.   } 
  109.  
  110. main() 
  111. {char ch; 
  112.   InitKey(); 
  113.   CreateList(); 
  114.   do{  
  115.      printf("Choose you wanted:D:DisplayList\nF:FindList\nQ:Quit\n"); 
  116.      ch=getchar(); 
  117.      switch(ch) 
  118.       { 
  119.        case 'D':DisplayList();break
  120.        case 'F':FindList();break
  121.        case 'Q':exit(0); 
  122.       } 
  123.      printf("come on!y/n?\n"); 
  124.      ch=getchar(); 
  125.     }while(ch!='n'); 
  126. return 0; 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章