Data structure life from scratch - sequential list and linked list

Posted by Cognito on Sun, 06 Feb 2022 04:10:34 +0100

Sequence table

  • Understand several key points

    • C language is process oriented programming, so the object in C refers to the physical memory that stores (multiple) values. An object may not store the actual value, but it has the corresponding size when stored

    • C + + object-oriented programming, so object in C + + refers to class object, which includes data set and operation set

    • #No semicolon (;) after define Size 5

    • In software (program), one way to access objects (memory) is to declare variables

      int entity=3;

      Understand the Declaration: an identifier named entity is created, which is used to specify a specific object (this memory) and store a value in the object (3). Therefore, identifiers are a way for software to access hardware.

    • typedef is used to get the alias of the data type

      typedef int ElementType;//typedef type alias
      int i = 0;
      ElementType j = 0;

  • malloc function

    • Form: (void *) malloc (parameter)

    • Accept a parameter: the number of bytes of memory required

    • Malloc will find an anonymous (no name identifier) free memory block, which cannot be accessed by name, but malloc will return the first byte address of the dynamically allocated memory block, so you can access the memory block with a pointer, and return a null pointer when the allocation fails

    • (void *) here is a pointer to void, which is equivalent to a general pointer. Should insist on cast

  • malloc deep understanding

    • Create an array with malloc()

      double* ptr;
      ptr=(double *)malloc(30*sizeof(double));
      • This code requests memory space for 30 double type values and sets ptr to point to this location. However, ptr only points to a double space, not a block space of 30 double. Analogously, the array name is the address of the first element of the array, so when ptr points to the first element of the block, it can be used like an array

      • At this time, the array is represented by a pointer: ptr[0] to access the first element and ptr[1] to access the second element

  • free() function

    • Free (pointer parameter) is used to free the dynamic memory allocated by malloc()

  • Sequence table

    • Data set (data involved in the sequence table)

      typedef struct ArrayList {//Data set
          int* element;//An array of data elements
          int size;//Number of current elements
          int length;//Length of array in sequence table / total capacity of data
      }MyArray;//alias

    • Operation set (data operation in sequence table - addition, deletion, modification and query)

      MyArray initArray()//Initialize array
      {
          MyArray array;//Create a structure space
          //Dynamically allocate 5 memory blocks
          array.element=(int*)malloc(Size*sizeof(int));
          if(array.element=NULL)
          {
              printf("Failed to allocate memory");
              exit(0);
          }
          array.size=0;
          array.length=Size;
          return array;
      }
      ​
      /*
      int key:One element
      MyArray* myarray:Array to find
      */
      void insert(int key,MyArray* myarray)//Add an element
      {
          //You can add as long as the number of current elements is less than the length of the array
          if(myarray->size<myarray->length)
          {
              myarray->element[myarray->size]=key;
              myarray->size++;
          }
          else
          {
              printf("The array is full");
          }
      }
      ​
      void index_insert(int key, MyArray* myarray);//Adds an element to the specified location
      ​
      void delete(int key, MyArray* myarray)//Delete an element
      {
          int i=index(key,myarray);
          if(i=-1)
          {
              printf("No corresponding data found");
          }
          else
          {//Size is the number of elements and i is the index of the array. Ensure that i < size
              while(i<myarray->size)
              {
                  //Each element is overwritten forward. In fact, the last element is still considered empty
                  myarray->element[i]=myarray->element[i+1];
                  i++;
              }
              myarray->size--;//Number of elements minus one
          }
      }
      ​
      //Is there such a data (room for judgment) where is this data (data positioning)
      //The find operation returns the subscript of an element to be found
      int index(int key, MyArray* myarray){
          for(int i=0;i<5;i++){//i is the array subscript variable
              if(myarray->element[i]==key){
                  return i;//Returns the corresponding subscript
              }
              else{return -1;}//- 1 if not found
          }
      }

Linked list

  • Linked list features

    • In memory, but not in logic

    • Each node needs to store data (data field) and pointer (pointer field) to the next node

  • Linked list classification

    • Unidirectional linked list

    • One way circular linked list

    • Bidirectional linked list

    • Bidirectional circular linked list

  • Unidirectional linked list

    • Leading node

    • No lead node

  • Key concepts in linked list

    • Header pointer: pointer to the first node (no element is stored)

    • Head node: the first node without data elements (the number of actual storage can be stored according to personal needs)

    • First element node: the first node where the element is stored

  • One key point: exercise the ability to transform abstract logic into concrete code

  • Node creation

typedef struct NodeList
{
    int element;//Data field for storing data
    struct NodeList* next//Point to a node
}Node;
  • Linked list implementation

    Node* InitList()//Initialize one-way linked list
    {
        //Initialize a header node
        Node* node=(Node*)malloc(sizeof(Node));
        if(node==NULL)
        {
            printf("memory allocation failed");
            exit(0);
        }
        else
        {
            node->element=NULL;
        }
        return node;
    }
    ​
    void head_insert(int key,Node* node)//Insert data into a single linked list (header insertion)
    {
        //Create a new node
        Node* temp=(Node*)malloc(sizeof(Node));
        //Put the data to be inserted into the new node
        temp->element=key;
        temp->next=node->next;
        node->next=temp;
    }
    ​
    void delete(int key,Node* node)
    {
        //First find the subscript of the element to be deleted (judge whether there is this element)
        int index=find(key,node);
        if(index==-1)
        {
            printf("There is no such element");
            exit(0);
        }
        else
        {
            int i=0;
            Node* temp=node;
            //Navigate to the front of the element to be deleted
            while(i<index)
            {
                temp=temp->next;
                i++;
            }
            //Temporary pointer to release node
            Node* free_node=temp->next;
            //Delete intermediate element
            temp->next=temp->next->next;
            //Release node
            free(free_node);
        }
    }
    ​
    int find(int key,Node* node)
    {
        Node* temp;//Temporary pointers are used for traversal lookups
        temp=node->next;
        int i=0;//Used to calibrate the subscript of the linked list
        while(temp!=NULL)
        {
            if(temp->element==key)
            {
                return i;//If the corresponding value is found, the corresponding subscript of the node is returned
            }
            temp=temp->next;//Point to the next node, i+1
            i++;
        }
        return -1;//If not found, - 1 is returned
    }
    • Implementation of insertion operation (otherwise, the node cannot be found)

Topics: C++ data structure linked list array