数据结构的重点

1不带头结点的单链表

头插

尾插

 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <assert.h>
 11 struct node{
 12     int date;
 13     struct node* next;
 14 };
 15 struct node* malloc_nohead(){
 16     struct node* pnew=NULL;
 17     pnew=(struct node*)malloc(sizeof(struct node));
 18     assert(pnew!=NULL);
 19     pnew->next=NULL;
 20     return pnew;
 21 }
 22 struct node* add_date(){//头插
 23     struct node* tatil=NULL;
 24     struct node* head=NULL;
 25     struct node* pnew=NULL;
 26     int x;
 27     scanf("%d",&x);
 28     while(getchar()!='\n');
 29     while(x){
 30         pnew=malloc_nohead();
 31         pnew->date=x;
 32         if(head==NULL){
 33             head=pnew;
 34             tatil=pnew;
 35         }
 36         else{
 37             tatil->next=pnew;
 38             tatil=pnew;
 39         }
 40         scanf("%d",&x);
 41         while(getchar()!='\n');
 42     }
 43     return head;
 44 }
 45 struct node* add_date1(){
 46     struct node* pnew=NULL;
 47     int x;
 48     struct node* head=NULL;
 49     scanf("%d",&x);
 50     while(getchar()!='\n');
 51     while(x){
 52         pnew=malloc_nohead();
 53         pnew->date=x;
 54 
 55         pnew->next=head;
 56         head=pnew;
 57 
 58         scanf("%d",&x);
 59         while(getchar()!='\n');
 60     }
 61     return head;
 62 }
 63 int main(){
 64 
 65     struct node* p=NULL;
 66 //     p=add_date();
 67     p=add_date1();
 68     while(p!=NULL){
 69         printf("%d ",p->date);
 70         p=p->next;
 71     }
 72     printf("\n");
 73     return 0;
 74 }
~                                  

注意:

sizeof(struct node)不可以带*号,不然只能打印一部分数据,因为空间不够

这是不带头结点的使用要返回值,不用传值

头插注意第一个节点和(中间,尾部)节点

注意结构体的;不可以丢掉,申请空间要在while里

2带头接点的单链表

8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <assert.h>
 11 struct node{
 12     int date;
 13     struct node* next;
 14 };
 15 struct node* malloc_head(){
 16     struct node* pnew=NULL;
 17     pnew=(struct node*)malloc(sizeof(struct node));
 18     assert(pnew!=NULL);
 19     pnew->next=NULL;
 20     return pnew;
 21 };
 22 void add_head(struct node* head){
 23     struct node* pnew=NULL;
 24     int x;
 25     scanf("%d",&x);
 26     while(x){
 27         pnew=malloc_head();
 28         pnew->date=x;
 29 
 30         pnew->next=head->next;
 31         head->next=pnew;
 32 
 33         scanf("%d",&x);
 34         while(getchar()!='\n');
 35     }
 36 }
 37 void add_head1(struct node* head){
 38     struct node* pnew=NULL;
 39     struct node* p=head;
 40     int x;
 41     scanf("%d",&x);
 42     while(x){
 43         pnew=malloc_head();
 44         pnew->date=x;
 45         while(p->next!=NULL){
 46             p=p->next;
 47         }
 48         p->next=pnew;
 49 
 50         scanf("%d",&x);
 51         while(getchar()!='\n');
 52     }
 53 }
 54 int main(){
 55 struct node* head=NULL;
 56 head=malloc_head();
 57 //add_head(head);
 58 add_head1(head);
 59 struct node* p=head->next;
 60 while(p!=NULL){
 61     printf("%d ",p->date);
 62     p=p->next;
 63 }
 64 printf("\n");
 65     return 0;
 66 }
~                                                                                                                                                                                                                                                                                                                                                                              
~                                                                                                                                                                                                                                                                                                                                                                              
~                                                                                                                                                                                                                                                                                                                                                                              
~                          

这个比较简单,不需要返回值,因为头节点没有动 

3双向链表

注意取最大值的时候也是跟之前一样只考虑一个方向

下面是带头节点找最大值

双向链表就是在结构体里面定义一个前向指针,然后在最后面new_head赋值的时候和head赋值的时候加个前向指针就行了

 #include <stdio.h>
  9 void max_find(struct node* head){
 10     struct node* p=NULL;
 11     struct node* head1=head->next;
 12     struct node* pmax=NULL;
 13     struct node* new_head=NULL;
 14     head->next=NULL;
 15 // 找最值
 16     while(head1!=NULL){//当链表上的全部移除了
 17         p=head1;
 18         pamx=head1;
 19         while(p!=NULL){//遍历链表
 20             if(p->date>pmax->date){
 21                 pmax=p;//找到了
 22             }
 23             p=p->next;
 24 
 25         }
 26             //将最大值移除
 27             if(head1==pmax){
 28                 head1=head1->next;
 29             }
 30             else{
 31                 p=head1;
 32                 while(p->next!=pmax){
 33                     p=p->next;
 34                 }
 35                     p->next=pmax->next;
 36             }
 37             pmax->next=NULL;
 38             
 39             pmax->next=new_head;
 40             new_head=pmax;
 41         }
 42         head->next=new_head;
 43     }
 44     return ;
 45 
 46 }

4栈

  8 #include <stdio.h>
  9 #include <assert.h>
 10 #include <stdbool.h>
 11 #include <stdlib.h>
 12 struct stack{
 13     int * my_space;
 14     int len; //栈的长度
 15     int count;//栈的数量
 16 };
 17 void stack_info(struct stack* ps,int n){  定义的时候要用地址传参
 18     ps->count=0;
 19     ps->len=n;
 20     ps->my_space=(int*)malloc(sizeof(int)*n);
 21     assert(ps->my_space!=NULL);
 22     return ;
 23 }
 24 
 25 bool is_full(struct stack s){
 26     if(s.len==s.count){
 27         printf("满了\n");
 28         return true;
 29     }
 30     return false;
 31 }
 32 
 33 bool is_empty(struct stack s){
 34     if(s.count==0){
 35         printf("空栈\n");
 36         return true;
 37     }
 38     return false;
 39 }
 40 
 41 bool stack_push(struct stack *ps,int date){ 地址传参
 42     if(is_full(*ps)){
 43         return false;
 44     }
 45     ps->my_space[ps->count]=date;
 46     (ps->count)++;
 47     return true;
 48 }
 49 bool stack_pop(struct stack *ps,int* pdate){  地址传参 有时候出栈需要知道其值
 50     if(is_empty(*ps)){
 51         return false;
 52     }
 53     if(pdate!=NULL){
 54         *pdate=ps->my_space[ps->count-1];  这里一定要写成count-1 因为我们相当于数组,小标要减一
 55     }
 56     (ps->count)--;
 57     return true;
 58 }
 59 int main()
 60 {
 61     struct stack  s;
 62     stack_info(&s, 5);
 63     stack_push(&s,1);
 64     stack_push(&s,3);
 65     stack_push(&s,5);
 66     stack_push(&s,7);
 67 
 68     int data;
 69     stack_pop(&s, &data);
 70     printf("data: %d\n", data);
 71     stack_pop(&s, &data);
 72     printf("data: %d\n", data);
 73     stack_pop(&s, &data);
 74     printf("data: %d\n", data);
 75     stack_pop(&s, &data);
 76     printf("data: %d\n", data);
 77 
 78     stack_pop(&s, &data);
 79 
 80     return 0;
 81 }
 82 

5 队列

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
struct node{
    int date;
    struct node* next;
};
struct pueue{
    struct node* frist;
    struct node* tatil;
    int len;
    int count;
};
void pueue_info(struct pueue *pq,int n){
    pq->len=n;
    pq->count=0;
    pq-> frist=NULL;
    pq-> tatil=NULL;
}
bool is_empty(struct pueue q){
    if(q.frist==NULL){
        return true;
    }
    else{
        return false;
    }
}
bool is_full(struct pueue q){
    if(q.count==q.len){
        return true;
    }
    else{
        return false;
    }
}
bool pueue_push(struct pueue *pq,int date){
    if(is_full(*pq)){
        printf("满了\n");
        return false;
    }
    struct node* pnew=NULL;
    pnew=(struct node*)malloc(sizeof(struct node));
    assert(pnew!=NULL);
    pnew->date=date;
    pnew->next=NULL;
    if(pq->frist==NULL){
        pq->frist=pnew;
        pq->tatil=pnew;
    }
    else{
        pq->tatil->next=pnew;
        pq->tatil=pnew;
    }
    pq->count++;
    return true;
}

bool pueue_pop(struct pueue *pq ,int * pdate){
    if(is_empty(*pq)){
        printf("空了\n");
        return false;
    }
    struct node* pedl = pq->frist;
    if(pdate!=NULL){
        *pdate=pedl->date;
    }
    if(pq->frist->next==NULL){
        pq->frist=NULL;
        pq->tatil=NULL;
        free(pedl);
    }
    else{
        pq->frist=pedl->next;
        free(pedl);
    }
    pq->count--;
    return true;
}
int main()
{
    struct pueue  q;

    pueue_info(&q, 5);

    pueue_push(&q, 2);
    pueue_push(&q, 4);
    pueue_push(&q, 6);
    pueue_push(&q, 8);


    int data;

    pueue_pop(&q, &data);
    printf("Data: %d\n", data);
    pueue_pop(&q, &data);
    printf("Data: %d\n", data);
    pueue_pop(&q, &data);
    printf("Data: %d\n", data);
    pueue_pop(&q, &data);
    printf("Data: %d\n", data);

    pueue_pop(&q, &data);
    return 0;
}

                                                                                                                                                             

6二叉树(回调)


  8 #include <stdio.h>
  9 struct node{
 10     struct node* left;
 11     struct node* right;
 12     int date;
 13 };
 14 struct node*create_tree()
 15 {
 16     struct node *root = NULL, *pnew = NULL;
 17 
 18     int x;
 19     scanf("%d", &x);
 20     while(getchar()!='\n');
 21 
 22     while(x)
 23     {
 24         //1、创建新的节点,并且赋值
 25         pnew = (struct node*)malloc(sizeof(struct node));
 26         assert(pnew!=NULL);
 27         pnew->data = x;
 28         pnew->left = NULL;
 29         pnew->right = NULL;
 30 
 31 
 32         if(root==NULL){
 33             root=pnew;
 34         }
 35         else{
 36         struct node* p=root;
 37         struct node* q=NULL;
 38         while(p!=NULL){
 39             q=p;
 40             if(p->date<pnew->date){
 41                 p=p->right
 42             }
 43             else{
 44                 p=p->left
 45             }
 46         } // 找到没有叶子的节点
 47             if(pnew->data < q->data)
 48             {
 49                 q->left = pnew;
 50             }
 51             else
 52             {
 53                 q->right = pnew;
 54             }
 55         }赋值
 56         scanf("%d", &x);
 57         while(getchar()!='\n');
 58     }
 59     return root;
 60 }
 61 
 62 void  pre_print(struct node *root)
 63 {
 64     if(root != NULL)
 65     {
 66         printf("%d ", root->data);
 67         pre_print(root->left);
 68         pre_print(root->right);
 69     }
 70 }先序排序

先序排序跟中序排序和后续排序都是在root!=null的时候一直回调自己,这里的root!=null就是结束条件。

7插入排序

 #include <stdio.h>
  9 
 10 int main(){
 11 int arr[5];
 12 for(int i=0;i<5;i++){
 13     scanf("%d",&arr[i]);
 14 }
 15 for(int i=1;i<5;i++){  控制无序
 16     int pmin=arr[i];
 17     for(int j=0;j<i;j++){  控制有序的,有几个有序的就跟几个比较
 18         if(arr[i]<arr[j]){
 19             for(int k=i;k>j;k--){   控制交换几次的
 20                 arr[k]=arr[k-1];
 21             }
 22                 arr[j]=pmin;  换完直接break;
 23                 break;        重点
 24         }
 25     }
 26 }
 27 for(int i=0;i<5;i++){
 28 printf("%d ",arr[i]);
 29 }
 30 printf("\n");
 31     return 0;
 32 }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值