Chapter 3 of "Dahua Data Structure"

linear table

  • Definition: A finite sequence of zero or more data elements

  • Abstract data type definition of linear table:Insert image description hereInsert image description here

  • Implement the union operation of two linear table sets A and B. That is: A=AUB:Insert image description here

  • Structural code for sequential storage of linear tables:Insert image description here

  • The position of the i+1th data element and the storage location of the i-th data element in the linear table are as follows:Insert image description here

  • Get element operation:Insert image description here

  • Insert image description here

  • Insertion algorithm implementation ideas:

     如果插入位置不合理,抛出异常;
     如果线性长度大于数组长度,则抛出异常或动态增加容量;
     从最后一个元素开始向前遍历到第i个位置,分别将它们都向后移动一个位置;
     将要插入元素填入位置i处;
     表长加1。
    
  • The code is implemented as follows:Insert image description here

  • Delete algorithm implementation ideas:

     如果删除位置不合理,抛出异常;
     取出删除元素;
     从删除元素位置开始遍历到最后一个元素位置,分别将它们都向前移动一个位置;
     表长减1。
    
  • The code is implemented as follows:Insert image description here
    Insert image description hereInsert image description here

     存储和读取数据时,无论在哪个位置,时间复制度都是O(1);
     而插入和删除时,时间复杂度都是O(n)
    
  • Advantages and disadvantages of linear table sequential storage structure:Insert image description here

  • x linear list chain storage structure definition:Insert image description here

  • Head node: A node attached before the first node of the linked list. The data field does not need to store any information, or it can store additional information such as the length of the linear table. The pointer field of the head node stores points to the first node. Pointer to node:Insert image description here

  • Similarities and differences between head pointer and head node:

     头指针:
     		链表指向的第一个结点的指针,若链表有头结点,则指向头结点的指针
     		头指针具有标识作用,所以常用头指针冠以链表的名字
     		无论链表是否为空,头指针均不为空。头指针是链表的必要元素
     
     头结点:
     		头结点是为了操作的统一和方便而设立的;放在第一元素的结点之前,其数据域一般无意义(也可以放链表的长度)
     		有了头结点,对在第一元素点前插入结点和删除第一结点,其操作与其它结点的操作就统一了
     		头结点不一定是链表的必要元素
    

Implementation of singly linked list in C speech

/*线性表的单链表存储结构*/
typedef struct Node
{
	ElemType data;
	struct Node *next;
} Node;
typedef struct Node *LinkList;  /*定义LinkList*/
  • Reading from singly linked list:Insert image description hereInsert image description here
  • Insertion into singly linked list:Insert image description here
  • Deletion of singly linked list:Insert image description here
  • Entire table creation of singly linked list:

Guess you like

Origin blog.csdn.net/qq_31709953/article/details/92408376