C language linked list-student management system

Linked list:

1) Linked list classification:

Header: only pointer field

Node: pointer field + data field The pointer field points to the data field of the next node, and the pointer field is the first address of the next node, namely ptemp->next=pnew

Table tail: pointer field + data field, pointer field points to NUL

2) The linked list is a chain storage structure, which is not necessarily continuous in space

Understand two code segments: (1) ptemp = ptemp->next // proceed to the next node operation

                             (2) ptemp->next = pnew; //The current node pointer field points to the next node (of course, the previous node address can also be stored)

Create Node---Create Linked List---Delete Node---Insert Node---Query Node---Modify Node

1. Structure structure

typedef struct student

{
    int ID;
    char name[10];
    float score;
    struct student *pnext 
}STD;

2. Create a node

STD *Creat_node(void)
{

    STD *pnew = (STD *)malloc(sizeof(STD));
    pnew->pnext = NULL;
    
    return pnew;
}

3. Create a linked list

STD *Creat_link(int num)
{
    STD *phead;
    STD *pnew,*pb;
    phead = NULL;
    
    for(int i = 0;i <= num;i++)    
    {
          pnew = Creat_node();
          if(i == 0 )
          {
            phead = pnew;
          }
          else
         {
            pb->pnext = pnew;
           
         }  
          pb = pnew;
    }
    if(phead != NULL)
    {
        pb->pnext = NULL;
    }
      return phead;

}

4. Delete the node

int Delete_node(STD *phead,int ID)
{
    STD *ptemp = phead;
    STD *pdelete = NULL;
    
    if(phead == NULL)
    {
       perror("要查询的链表为空");
    }
    while(ptemp->pnext != NULL)
    {
        if(ptemp->pnext->id == ID)
        {
           pdelete = ptemp->pnext;
           ptemp=>pnext = pdelete->pnext;
            free(pdelete);
            return 1;
        }
        ptemp = ptemp->pnext;
    }
        return 0;
    
}

5. Insert node

STD *Insert_node(STD *phead,STD *pnew)
{
    STD *ptemp = phead;
    
    if(ptemp == NULL)
    {
        perror("Insert 链表为空");
        exit(0);
    }
    while(ptemp->pnext != NULL)
    {
        ptemp=ptemp->pnext;
    }
    ptemp->pnext = pnew;
    
    return ptemp;

}

6. Query node

STD *SEEK_node(STD *phead,int id)
{
    STD *ptemp = phead;
    STD *pseek = NULL;
    while(ptemp->pnext != NULL)
    {
        if(ptemp->pnext->id == ID)
        {
            pseek = ptemp->pnext;
            return pseek;
        }
        ptemp = ptemp->pnext;
    }
    
    return 0;
}

7. Modify the node

int *Change_node(STD *phead,STD ptemp)
{
    STD *pseek = SEEK_node(phead,ptemp.id);
    
    pseek->id = std.id;
    strcpy(pseek->name,std.name);
    pseek->score = std.score;

    return 1;

   
}

2). Linked list commonly used functions

(1) malloc

         Function prototype: void *_Cdecl malloc(size_t size);

         Function function: apply for a memory space from the memory (manually open up. Open up from the heap area, as long as we do not release the space, the saved data will always exist until the main end automatically releases the space)

         Use: p = malloc(8); //Apply a space of 8 bytes to the memory. If successful, the first address of the space after the application is saved in p

(2)   calloc

            Function prototype: void *_Cdecl calloc(size_t nitems, size_t size);

            Function function: apply for multiple memory spaces from the memory

            Usage: Example: p = calloc(3,4); Success: return the first address of the first space opened up, return NULL on failure

(3) realloc

       Function prototype: void *_Cdecl realloc(void *block, size_t size);

       Function function: modify the size of the allocated memory space

       Function parameter: block: indicates the first address of the memory space that needs to be modified size: how much memory space to modify

       Function return value: success: return the first address of the new space, failure: NULL

       Example: p = realloc(q,20); //Apply to the memory to modify the space byte size, change it to 20 bytes, and then assign the first address of the new space to p  

Note: If there is data in the original space, data loss will occur if the new space is smaller than the original space.

(4)free

Function prototype: void _Cdecl free(void *block);

Function function: manually release the space dynamically opened up to the memory

Function parameter: the first address of the space to be released

Function return value: None

The procedures for writing temporary hand-printing articles are not verified and may be different!

 

Guess you like

Origin blog.csdn.net/qq_45604814/article/details/110450434