一、线性表的定义
有零个或多个具有线性关系的元素的集合。比如 : 数组、单链表
特点:
1.有且只有一个头
2.有且只有一个尾
3.除了头元素外,每个元素都有一个前驱
4.除了尾元素外,每个元素都有一个后继
每个元素可以是一种单一类型,也可以是复合类型。
线性表从存储的角度:线性存储结构和链式存储结构
具有线性存储结构的线性表我们成为顺序表;
元素之间在物理上相邻并且在逻辑上也相邻——静态顺序表;
在物理上不相邻在逻辑上相邻的存储结构——链表。
清空顺序表:
size=0;
二、数据结构之栈
栈(stack):
是一种特殊的线性表,是只允许在一端进行插入和删除的线性表,我们把允许删除和插入的一端称为栈顶,另一端称为栈底。不含任何元素的栈称为空栈。
栈的应用:将十进制25的值用二进制显示出来
for(int i=0;i<get_len(head);i++) 警惕这样的错误
//链表 链式栈#include<stdio.h>#include<string.h>#include<stdbool.h>#include<assert.h>#include<stdlib.h>typedef int ele_type;typedef struct list{ele_type score;struct list *next;} node;//初始化单链表node * init_list(){node *head=(node *)malloc(sizeof(node));if(head==NULL)exit(1);head->next=NULL;return head;}//头插void head_insert(node *head,ele_type val){assert(head);node * new_node=malloc(sizeof(node));new_node->score=val;new_node->next=head->next;head->next=new_node;}//头删bool head_del(node *head,ele_type *val){assert(head);if(head->next!=NULL){node *p=head->next;//p保存要删除的节点head->next=p->next;*val=p->score;free(p);p=NULL;return true;}return false;}void print_list(node *head){assert(head);node *cur=head;if(head->next!=NULL){do{cur=cur->next;printf("%d ",cur->score);}while(cur->next!=NULL);printf("\n");}}int main(){node *head=init_list();int num=25;//被除数int bit=0; //存余数while(num!=0){bit=num%2;head_insert(head,bit);num=num/2;}ele_type data=0;//printf(" $%d",get_len(head));int len=get_len(head);for(int i=0;i<len;i++){head_del(head,&data);printf("%d",data);}return 0;}
#include<stdio.h>#include<string.h>#include<stdbool.h>#include<assert.h>#include<stdlib.h>#define INIT_SIZE 10#define EXPAND_SIZE 5typedef int ele_type;typedef struct stack{ //定义一个栈结构,相当于设计了一个桶的图纸ele_type *base; //桶底ele_type *top; //桶中的水位int cap ; //栈容量==桶容量} Stack;void init_stack(Stack *sk) //初始化栈 就相当于根据图纸造桶{sk->base=(ele_type *)malloc(sizeof(ele_type)*INIT_SIZE);if(sk->base==NULL){printf("申请内存失败!\n");return;}sk->top=sk->base;sk->cap=INIT_SIZE;}//判满bool is_full(Stack *sk){if(sk->cap==(sk->top-sk->base))return true;elsereturn false;}//判空bool is_empty(Stack *sk){if(sk->top==sk->base)return true;elsereturn false;}//入栈void push_stack(Stack *sk,ele_type val){assert(sk);if(is_full(sk)){sk->base=(ele_type*)realloc(sk->base,sizeof(ele_type)*(sk->cap+EXPAND_SIZE));if(sk->base==NULL){printf("扩容内存失败!");return ;}sk->cap=sk->cap+EXPAND_SIZE;}*(sk->top)=val;//放入数据sk->top++;}bool pop_stack(Stack *sk,ele_type *rtval){if(is_empty(sk)||sk==NULL)return false;sk->top--;*rtval=*(sk->top);return true;}bool get_top(Stack *sk,ele_type * rtval){if(is_empty(sk)||sk==NULL)return false;*rtval= *(sk->top-1);return true;}void print_stack(Stack *sk){ele_type *temp=sk->top;while(sk->base!=sk->top){sk->top--;printf("%d ",*(sk->top));}sk->top=temp;//还原}int main(){Stack sk;init_stack(&sk) ;push_stack(&sk,11);push_stack(&sk,22);push_stack(&sk,33);push_stack(&sk,44);print_stack(&sk);ele_type data;pop_stack(&sk,&data);printf("pop stack=%d\n",data);pop_stack(&sk,&data);printf("pop stack=%d\n",data);print_stack(&sk);get_top(&sk,&data);printf("top stack=%d\n",data);return 0;}
查找算法——二分查找
举例:
#include <stdio.h>#include <stdbool.h>bool get_val(int arr[],int size,int val,int *npos){int left = 0;int right = size-1;int mid = 0;while (left <= right){mid = left + (right-left) / 2;//防止越界if(arr[mid] == val){printf("找到了%d\n",val);*npos = mid;return true;}else if(arr[mid] < val){left = mid+1;}else{right = mid-1;}}return false;}int main(){int arr[9]={11,24,49,75,89,101,108,206};int npos;int flag=get_val(arr,9,1001,&npos);if(flag){printf("index=%d\n",npos);}else{printf("查找失败!\n");}return 0;}
