一、线性表的定义

有零个或多个具有线性关系的元素的集合。比如 : 数组、单链表
特点:
1.有且只有一个头
2.有且只有一个尾
3.除了头元素外,每个元素都有一个前驱
4.除了尾元素外,每个元素都有一个后继
每个元素可以是一种单一类型,也可以是复合类型。
线性表从存储的角度:线性存储结构和链式存储结构
具有线性存储结构的线性表我们成为顺序表;
元素之间在物理上相邻并且在逻辑上也相邻——静态顺序表;
在物理上不相邻在逻辑上相邻的存储结构——链表。
清空顺序表:
size=0;

二、数据结构之栈

栈(stack):
是一种特殊的线性表,是只允许在一端进行插入和删除的线性表,我们把允许删除和插入的一端称为栈顶,另一端称为栈底。不含任何元素的栈称为空栈。
栈的应用:将十进制25的值用二进制显示出来
for(int i=0;i<get_len(head);i++) 警惕这样的错误

  1. //链表 链式栈
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdbool.h>
  5. #include<assert.h>
  6. #include<stdlib.h>
  7. typedef int ele_type;
  8. typedef struct list{
  9. ele_type score;
  10. struct list *next;
  11. } node;
  12. //初始化单链表
  13. node * init_list()
  14. {
  15. node *head=(node *)malloc(sizeof(node));
  16. if(head==NULL)
  17. exit(1);
  18. head->next=NULL;
  19. return head;
  20. }
  21. //头插
  22. void head_insert(node *head,ele_type val)
  23. {
  24. assert(head);
  25. node * new_node=malloc(sizeof(node));
  26. new_node->score=val;
  27. new_node->next=head->next;
  28. head->next=new_node;
  29. }
  30. //头删
  31. bool head_del(node *head,ele_type *val)
  32. {
  33. assert(head);
  34. if(head->next!=NULL)
  35. {
  36. node *p=head->next;//p保存要删除的节点
  37. head->next=p->next;
  38. *val=p->score;
  39. free(p);
  40. p=NULL;
  41. return true;
  42. }
  43. return false;
  44. }
  45. void print_list(node *head)
  46. {
  47. assert(head);
  48. node *cur=head;
  49. if(head->next!=NULL)
  50. {
  51. do
  52. {
  53. cur=cur->next;
  54. printf("%d ",cur->score);
  55. }while(cur->next!=NULL);
  56. printf("\n");
  57. }
  58. }
  59. int main()
  60. {
  61. node *head=init_list();
  62. int num=25;//被除数
  63. int bit=0; //存余数
  64. while(num!=0)
  65. {
  66. bit=num%2;
  67. head_insert(head,bit);
  68. num=num/2;
  69. }
  70. ele_type data=0;
  71. //printf(" $%d",get_len(head));
  72. int len=get_len(head);
  73. for(int i=0;i<len;i++)
  74. {
  75. head_del(head,&data);
  76. printf("%d",data);
  77. }
  78. return 0;
  79. }
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdbool.h>
  4. #include<assert.h>
  5. #include<stdlib.h>
  6. #define INIT_SIZE 10
  7. #define EXPAND_SIZE 5
  8. typedef int ele_type;
  9. typedef struct stack{ //定义一个栈结构,相当于设计了一个桶的图纸
  10. ele_type *base; //桶底
  11. ele_type *top; //桶中的水位
  12. int cap ; //栈容量==桶容量
  13. } Stack;
  14. void init_stack(Stack *sk) //初始化栈 就相当于根据图纸造桶
  15. {
  16. sk->base=(ele_type *)malloc(sizeof(ele_type)*INIT_SIZE);
  17. if(sk->base==NULL)
  18. {
  19. printf("申请内存失败!\n");
  20. return;
  21. }
  22. sk->top=sk->base;
  23. sk->cap=INIT_SIZE;
  24. }
  25. //判满
  26. bool is_full(Stack *sk)
  27. {
  28. if(sk->cap==(sk->top-sk->base))
  29. return true;
  30. else
  31. return false;
  32. }
  33. //判空
  34. bool is_empty(Stack *sk)
  35. {
  36. if(sk->top==sk->base)
  37. return true;
  38. else
  39. return false;
  40. }
  41. //入栈
  42. void push_stack(Stack *sk,ele_type val)
  43. {
  44. assert(sk);
  45. if(is_full(sk))
  46. {
  47. sk->base=(ele_type*)realloc(sk->base,sizeof(ele_type)*(sk->cap+EXPAND_SIZE));
  48. if(sk->base==NULL)
  49. {
  50. printf("扩容内存失败!");
  51. return ;
  52. }
  53. sk->cap=sk->cap+EXPAND_SIZE;
  54. }
  55. *(sk->top)=val;//放入数据
  56. sk->top++;
  57. }
  58. bool pop_stack(Stack *sk,ele_type *rtval)
  59. {
  60. if(is_empty(sk)||sk==NULL)
  61. return false;
  62. sk->top--;
  63. *rtval=*(sk->top);
  64. return true;
  65. }
  66. bool get_top(Stack *sk,ele_type * rtval)
  67. {
  68. if(is_empty(sk)||sk==NULL)
  69. return false;
  70. *rtval= *(sk->top-1);
  71. return true;
  72. }
  73. void print_stack(Stack *sk)
  74. {
  75. ele_type *temp=sk->top;
  76. while(sk->base!=sk->top)
  77. {
  78. sk->top--;
  79. printf("%d ",*(sk->top));
  80. }
  81. sk->top=temp;//还原
  82. }
  83. int main()
  84. {
  85. Stack sk;
  86. init_stack(&sk) ;
  87. push_stack(&sk,11);
  88. push_stack(&sk,22);
  89. push_stack(&sk,33);
  90. push_stack(&sk,44);
  91. print_stack(&sk);
  92. ele_type data;
  93. pop_stack(&sk,&data);
  94. printf("pop stack=%d\n",data);
  95. pop_stack(&sk,&data);
  96. printf("pop stack=%d\n",data);
  97. print_stack(&sk);
  98. get_top(&sk,&data);
  99. printf("top stack=%d\n",data);
  100. return 0;
  101. }

查找算法——二分查找
举例:

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. bool get_val(int arr[],int size,int val,int *npos)
  4. {
  5. int left = 0;
  6. int right = size-1;
  7. int mid = 0;
  8. while (left <= right)
  9. {
  10. mid = left + (right-left) / 2;//防止越界
  11. if(arr[mid] == val)
  12. {
  13. printf("找到了%d\n",val);
  14. *npos = mid;
  15. return true;
  16. }
  17. else if(arr[mid] < val)
  18. {
  19. left = mid+1;
  20. }
  21. else
  22. {
  23. right = mid-1;
  24. }
  25. }
  26. return false;
  27. }
  28. int main()
  29. {
  30. int arr[9]={11,24,49,75,89,101,108,206};
  31. int npos;
  32. int flag=get_val(arr,9,1001,&npos);
  33. if(flag)
  34. {
  35. printf("index=%d\n",npos);
  36. }
  37. else
  38. {
  39. printf("查找失败!\n");
  40. }
  41. return 0;
  42. }