C language learning (10) sequence table

 There is a lot to learn today. The mutual calling of functions and the clever use of pointers are all difficult points in learning. The following is a code-based self-review and inspection. I hope we can learn from each other and make progress together'. The logic of sequence tables is not difficult to learn. What is difficult is careful thinking and maintaining clear ideas. Every possibility needs to be considered, and the conversion or definition of every data type cannot be ignored.

1. Definition of linear table    

Define a finite sequence of n data elements, denoted as (a1, a2, …, an) ai is the data element in the table, n is the length of the table

2. Characteristics of linear tables    

Every element except the first element has one and only one immediate predecessor.    

Every element except the last element has one and only one direct successor.

3. Definition and characteristics of sequence list

Definition: Store the elements in a linear list one after another in a continuous storage space.          

One-dimensional arrays can be used to describe the storage structure characteristics. Sequential storage traversal of linear tables. Sequential access, random access is possible.

/****************************************************** ****
    > File name: work3.c
    > Author: Li Shang
    > Date: 2018-08-01 15:33
************************* ******************************/

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1 //There is no need to add ";" to the macro definition
#define FALSE 0

int MAX = 10;

typedef int Elementype;//Give int an alias to facilitate modification of a series of data types, such as typedef char Elementype

struct List//Sequence list structure
{     Elementype* head;//Sequence list header pointer     int length;//Sequence list length };


int init(struct List* l);//函数声明,后面逐个介绍
void print(struct List l);
int insert_tail(struct  List* l,Elementype value);
int insert_index(struct List* l,int index,Elementype value);
int detele_index(struct List* l,int index);
int detele_value(struct List* l,Elementype value);
int updata_index(struct List* l,int index,Elementype value);
int query_value(struct List* l,Elementype value);
int empty(struct List* l);

int main()
{     struct List list;//Define structure variable list     int ret;     ret = init(&list);//Receive and return the application result     if(ret == FALSE)     {         return 1;     }






    int i;
    for(i = 0;i < 20;i++)
    {         insert_tail(&list,i);//Use the tail insertion function to assign values ​​to the sequence list     }     print(list);//Use a custom output function to loop the output sequence surface


    insert_index(&list,2,66);
    print(list);
    
    detele_index(&list,2);
    print(list);
    
    insert_index(&list,2,2);
    print(list);
    
    insert_index(&list,2,66);
    print(list);

    updata_index(&list,2,99);
    print(list);

    query_value(&list,2);
    empty(&list);
    return 0;
}

int init(struct List* l)//Apply memory for the sequence table and return the application result
{     l -> head = (Elementype*)malloc(MAX*sizeof(Elementype));//malloc applies for memory     if(NULL == l -> head)//Determine if the address is empty, the application fails     {         return FALSE;     }      l -> length = 0;//Assign a value to the address pointed by the pointer     return TRUE; }







void print(struct List l)//Print function, loop printing sequence list
{     int i;     for(i = 0;i < l.length;i++)     {         printf("%d ",*(l.head+i) );     }     printf("\n"); }






int insert_tail(struct List* l,Elementype value)//Tail insertion function
{     if (l -> length == MAX)//Determine whether the memory is enough, if the upper limit is reached, apply     {         l -> head = (Elementype*) realloc(l -> head,sizeof(Elementype)*(MAX+MAX/2));//realloc applies again         if(NULL == l -> head)//determines whether the memory application is successful         {             return FALSE;         }         MAX + = MAX/2;//Calculate the memory size after application         printf("realloc MAX = %d\n",MAX);//Display the memory size after application








    }
    *(l -> head + l -> length) = value;//Assign the value to length, that is, the digit after the last digit
    l -> length++;
    return TRUE;
}

int insert_index(struct List* l,int index,Elementype value)//Insert function at specified position
{     if(l->length == MAX)//Judge the memory, if it is not enough, apply     {         l->head = (Elementype*)realloc (l->head,sizeof(Elementype)*(MAX += MAX/2));         if(NULL == l->head)//Determine whether the application is successful         {             return FALSE;         }         MAX += MAX/2;         printf("realloc MAX = %d\n",MAX );//Display the size after application     }     if(index < 0 || index >l->length)//Judge whether the insertion position is entered correctly     {         printf("index is error\n");         return FALSE;     }     int i ;     for(i = l->length - 1;i >= index;i--)//(create "vacancy") loop assignment, assign the previous digit to the next digit, complete the overall movement of the value     {


















        *(l->head + i + 1) = *(l->head + i);
    } 
    *(l->head + index) = value;
    l->length++;
    return TRUE;

}

int detele_index(struct List* l,int index)//Delete the data at the specified position
{     if(index < 0 || index > l-> length-1)//Judge whether the insertion position is entered correctly     {         printf("index is error \n");         return FALSE;     }     int i;     for(i = index;i < l -> length -1;i++)//Use the following content to overwrite the previous content at once to complete the deletion     {         *(l -> head +i) = *(l -> head +i +1);     }     l->length--;     return FALSE; }












int detele_value(struct List* l,Elementype value)
{     int i;     for(i = 0;i < l->length;i++)     {         if(*(l->head+i) == value)//determine whether Is the specified content         {             detele_index(l,1);             i--;//Used to determine whether the location of the new specified content meets the requirements         }     }     return TRUE; }










int updata_index(struct List* l,int index,Elementype value)
{     if(index < 0 || index > l-> length-1)     {         printf("index is error\n");         return TRUE;     }     *(l -> head +index) = value;//Overwrite the content at the specified position with the given content     return TRUE; }


        





int query_value(struct List* l,Elementype value)
{     printf("querry_value(%d)\n",value );//Show what you are looking for     int i;     int flag = 0;     for(i = 0;i < l ->length;i++)     {         if(value == *(l->head+i))//Judge whether it is the specified content         {             flag++;//Count the number that meets the requirements             printf("%d ",i );         }     }     if(flag == 0)//Determine if there is no such value     {         printf("no found\n");         return FALSE;     }     printf("\n");     return TRUE; }


















int empty(struct List* l)//Release memory and pointer
{     free(l->head);     l->head = NULL;     l->length = 10;     MAX = 10;     return TRUE;




}
 

Guess you like

Origin blog.csdn.net/ls_dashang/article/details/81348495