OBJECTIVE-C Study Preparation __c Language 7

Source: Internet
Author: User



The preparation of C language is almost here. Finally, learn about dynamic arrays



We know that arrays of C have to specify lengths when they are defined, so how do we define dynamic arrays?



The fact that the dynamic array is a space for you to use in the heap and this space can only be accessed by pointers, let's look at the code


#include <stdio.h> #include <stdlib.h> int main(){ int* arr1 = malloc(3*sizeof(int)); int* arr2 = calloc(3 , sizeof(int));
   arr1[0]=2; arr1[2]=4;
   printf("%d\n",arr1[0]);
   printf("%d\n",arr1[1]);
   printf("%d\n",arr1[2]);   
   arr2[0]=20; arr2[2]=40;
   printf("%d\n",arr2[0]);
   printf("%d\n",arr2[1]);
   printf("%d\n",arr2[2]); realloc(arr1,5*sizeof(int));
   arr1[3]=23;
   printf("%d\n",arr1[3]);                  
  free(arr1);
  free(arr2);
} 


You can see that both malloc and calloc can create a dynamic space array just a function is two parameters a function is a parameter, for what?



Run several times, carefully observe the output of arr1[1] and arr2[1] Both spaces are not assigned arr1[1] in the original memory of the data, but Arr2[1] is 0;



It is concluded that the dynamic space created by Calloc will be zeroed, while malloc creation will not, because the former has two parameters to know how many bytes to do once cleared 0 processing;



ReAlloc can modify the length of the original dynamic array;



Finally there is a free () function to release the dynamic space, each space after use to release or always occupy space, over time, more and more occupy will cause memory leaks, the computer is getting more and more cards






Now that you know the creation of a dynamic array, think about whether you can make a collection like a list single-linked list in Java or C #.



A single-linked list, expressed in structure.


 
typedef struct List{
    int value;
    struct List *next;
}ArrayList;





Create a node type pointer to create a dynamic array space


 
node_t *head= malloc(sizeof(ArrayList));
head->value = 1;
head->next = NULL;


How to add a single linked list?


head->next = malloc(sizeof(ArrayList));
head->next->value = 2;
head->next ->next = NULL;
head->next->next = malloc(sizeof(ArrayList));
head->next->next->value = 3;
head->next->next->next = NULL;
printf("%d\n",head->value);
printf("%d\n",head->next->value);
printf("%d\n",head->nex->nextt->value);


Isn't that a lot of trouble? We can do this in a single way.


 
void Listadd(ArrayList** head,int val){
    if((*head)==NULL){
        (*head)=malloc(sizeof(ArrayList));
        (*head)->value=val;
        (*head)->next=NULL;
        return;
    }
    ArrayList* he=*head;
    while(he->next!=NULL){
        he=he->next;
    }
    he->next=malloc(sizeof(ArrayList));
    he=he->next;
    he->value=val;
    he->next=NULL;
}


With this method, you can directly call add to add a dataset.



Here is a pointer to the struct body of the symbolic struct variable name access is a dot access


 
int main(){
 ArrayList* p=NULL;
 Listadd(&p,2);   
 Listadd(&p,1);
 Listadd(&p,5);
}


Since there is added so sure there is a delete, delete is to use the free () method to release the space specific practice I will not say a few people think about it. Here's an example of what I wrote



Arraylist.c


#include <stdlib.h> #include <stdio.h> #include "ArrayList.h" void Listadd(ArrayList** head,int val){ if((*head)==NULL){
        (*head)=malloc(sizeof(ArrayList));
        (*head)->value=val;
        (*head)->next=NULL; return;
    }
    ArrayList* he=*head; while(he->next!=NULL){
        he=he->next;
    }
    he->next=malloc(sizeof(ArrayList));
    he=he->next;
    he->value=val;
    he->next=NULL;
} void ListaddFrist(ArrayList** head,int val){ if((*head)->value==0){
        (*head)->value=val; return;
    }
    ArrayList* p=malloc(sizeof(ArrayList));
    p->value=val;
    p->next=*head; *head=p;
} void ListaddLast(ArrayList** head,int val){
    Listadd(head,val);
} void ListRemoveFrist(ArrayList** head){
    ArrayList*p=(*head)->next; free(*head); *head=p;
} void ListRemoveLast(ArrayList** head){ if((*head)==NULL){
        printf("没有可释放的动态数组空间\n"); return;
    } if((*head)->next==NULL){ free(*head);
        printf("该动态数组空间已全部释放\n"); return;
    }
    ArrayList* he=*head; while(he->next->next!=NULL)
        he=he->next; free(he->next);
    he->next=NULL;
} void ListRemoveby(ArrayList** head,int i){
    ArrayList*p; if(i==0){
        ListRemoveFrist(head); return;
    } if(i==1){
        p=(*head)->next->next; free((*head)->next);
        (*head)->next=p; return;
    }
    ArrayList* he=*head; while(i!=1){
        he=he->next;
        i--;
    }
    p=he->next->next; free(he->next);
    he->next=p;
} void print(ArrayList* head){ while(head!=NULL){
        printf("%d\t",(*head).value);
        head=(*head).next;
    }
}
ArrayList*Listequals(ArrayList** head,int i){
    ArrayList* he=*head; while(i!=0){
        he=he->next;
        i--;
    } return he;
}


ArrayList.h


 
 
#ifndef _ArrayList_H_
#define _ArrayList_H_
typedef struct arr{
    int value;
    struct arr* next;
} ArrayList;
ArrayList* NewArrayList();
void Listadd(ArrayList**,int);
void print(ArrayList*);
void ListaddFrist(ArrayList**,int);
void ListaddLast(ArrayList**,int);
void ListRemoveFrist(ArrayList** head);
void ListRemoveLast(ArrayList** head);
ArrayList*Listequals(ArrayList**,int);
void ListRemoveby(ArrayList**,int);
#endif


TEST.c


#include <stdio.h> #include <stdlib.h> #include "ArrayList.h" int main(){
    ArrayList* p=NULL;
    Listadd(&p,2);
    Listadd(&p,3);
    Listadd(&p,4);
    Listadd(&p,5);
    ListaddLast(&p,6);
    print(p);
    printf("\n-----------\n");
    ListaddFrist(&p,1);
    ListaddFrist(&p,0);
    print(p);
    printf("\n-----------\n");
    ListRemoveFrist(&p);
    print(p);
    printf("\n-----------\n");
    ListRemoveLast(&p);
    print(p);
    printf("\n-----------\n");
    ArrayList*test=Listequals(&p,2);
    printf("indexOf(2)=%d\n",test->value);
    printf("\n-----------\n");
    printf("remove indexOf(2)\n");
    ListRemoveby(&p,2);
    print(p);
    printf("\n-----------\n");
}





OBJECTIVE-C Study Preparation __c Language 7


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.