Data Structure Encyclopedia

Table of contents

1. Linear table

2. Linear table for sequential storage -- sequential table

3. Linear list of linked storage - linked list

4. stack

5. queue

6. tree


1. Linear table

    A linear table is composed of a finite number of data elements of the same type. Among the limited number of data elements, the data elements form an ordered sequence, except for the first element and the last element, other elements have a unique predecessor element and a unique successor element.
    Data elements can be either atomic or composite.
    Linear tables are mainly divided into two types, sequential storage structure and chain storage structure.
 

2. Linear table for sequential storage -- sequential table

The data field of the linear table stored sequentially:
    #define ListSize 100//Create the length of the linear table 
    typedef int MyType;//Create the type you want to use for your own linear table 
    typedef struct
    {         MyType list[ListSize];//Data storage part          int length;//indicates the current length of the linear list      }SeqList;//      initialization of the linear list of the linear list body:     void InitList(SeqList *l)//initialization of the linear list      {         l->length = 0;     }     judging whether the linear list is empty :     int ListEmpty(SeqList *l)//judging whether the linear list is empty     {         if(l->length==0)             return 1;//if the linear list is empty, return 1         else             return 0;      }     specify the position for the linear list Insert elements:     int InsertList(SeqList *l,int i,MyType e)//insert data into the linear table 


















    {         int j;         if(i<1||i>l->length+1)         {             cout<<"The position you want to insert is illegal!"<<endl;             return -1;//Return value -1 means operation Illegal          }          else if(l->length==ListSize)         {             cout<<"The linear list is full!"<<endl;             return 0;//A return value of 0 means that it cannot be inserted          }         else         {             if(l->length= =0)             {                 l->list[0] = e;                 l->length++;                 return 1;             }             for(j=l->length;j>=i;j--)             {      l->list[j] = l->list[j-1];






















            }        
            l->list[j] = e;
            l->length++;
            return 1;//A return value of 1 means the insertion is successful 
        } 
    }
    Look up the linear table by content:
    int LocateElem(SeqList l,MyType e)//Search by content 
    {         int i;         for(i=0;i<l.length;i++)             if(l.list[i]==e)                 return i+1;         return 0;      }      Find by position:     int GetElem(SeqList *l,int i,MyType *e)//Search by location      {         if(i<1||i>l->length+1)         {             cout<<"The location you are looking for is illegal!"<<endl;             return -1; //The return value -1 means the operation is illegal          }  














        *e = l->list[i-1];//put the found element into the variable e 
        return 1;//return 1 means found 
    } 
    delete the specified element:
    int DeleteList(SeqList *l,int i,MyType *e)//Delete the specified element
    {         int j;         if(l->length<=0)         {             cout<<"Empty list cannot complete the deletion operation"<<endl;             return 0;          }          else if(i<1| |i>l->length)         {             cout<<"Delete position is invalid!"<<endl;             return -1;         }         else         {             *e = l->list[i-1];              for(j=i;j <l->length;j++)             {                 l->list[j-1] = l->list[j];

















            }
            l->length--;
            return 1;
        }
    } 
    Return the length of the linear list:
    int ListLength(SeqList l)// Return the length of the linear list 
    {         return l.length;     }     Clear the linear list:     void ClearList(SeqList *l) //Clear the linear table     {         l->length = 0;      }     Display all linear tables:     void PrintList(SeqList *l) //Display all linear tables      {         for(int i=0;i<l->length;i++)             cout <<l->list[i]<<" ";         cout<<endl;     }














 

3. Linear list of linked storage - linked list

typedef int DataType;
    typedef struct Node
    {         DataType data;//data field (the part of the carriage)          struct Node *next;//pointer field (the hook connecting the carriage)      }ListNode,*LinkList;//ListNode is a linked list for each The type of the node, LinkList is a pointer to the linked list node      LinkList L;//Indicates that a linked list is defined. L actually points to      the head node of the locomotive (the meaning of the locomotive).     If there is no linked list of the leading node, L = NULL; means     the linked list of the empty linked list leading the leading node, L->next = NULL; means     the initialized list of the empty linked list:     void InitList(LinkList *head)//Initialize the linked list (the essence is to initialize the locomotive)      {         if((*head=(LinkList)malloc(sizeof(ListNode)))==NULL)//If the memory allocation fails              exit(-1);//With Error end program          (*head)->next = NULL;//The head of the linked list (locomotive) is empty first      }     Judge whether the linked list is empty:     int ListEmpty(LinkList head)//Judge whether the linked list is empty 



    













    {         if(head->next==NULL)             return 1;//If it is empty, return 1, otherwise return 0          else             return 0;     }     Search the node of the linked list according to the serial number:     ListNode *Get(LinkList head,int i)//press Search by serial number, find the first element in the linked list      {         ListNode *p;         int j;         if(ListEmpty(head))//To judge that the linked list to be searched cannot be empty              return NULL;         if(i<1)//To search The serial number cannot be wrong              return NULL;         j = 0;         p = head;         while(p->next!=NULL&&j<i)         {             p=p->next;             j++;         }         if(j==i)             return p;/ /Success returns a pointer to the node























        else
            NULL:
    }
ListNode *LocateElem(LinkList head,DataType e)//按内容查找 
    {
        ListNode *p;
        p = head->next;
        while(p)
        {
            if(p->data!=e)
                p=p->next;
            else
                break;
        }
        return p;
    }
    
    int LocatePos(LinkList head,DataType e)//定位 
    {
        ListNode *p;
        int i;
        if(ListEmpty(head))
            return 0;
        p=head->next;
        i=1;
        while(p)
        {
            if(p->data==0)
                return 1;
            else
            {
                p=p->next;
                i++;
            }
        }
        if(!p)
            return 0;        
    } 
    
    int InsertList(LinkList head,int i,DataType e)//插入 
    {
        ListNode *p,*pre;
        int j;
        pre = head;
        j=0;
        while(pre->next!=NULL&&j<i-1)
        {
            pre=pre->next;
            j++;
        }
        if(j!=i-1)
        {
            cout<<"插入位置错!"<<endl;
            return 0; 
        }
        if((p=(ListNode *)malloc(sizeof(ListNode)))==NULL)
            exit(-1);
        p->data=e;
        p->next=pre->next;
        pre->next = p;
        return 1;
    } 
    
    int DeleteList(LinkList head,int i,DataType *e)//删除指定位置的函数 
    {         ListNode *pre,*p;         int j;         pre = head;         j = 0;         while(pre->next!=NULL&&pre->next->next!=NULL&&j<i-1)         {             pre=pre->next;             j++;         }         if(j!=i-1)             cout<<"Delete location error"<<endl;         {












            return 0; 
        }
        p=pre->next;
        *e=p->data;
        pre->next=p->next;
        free(p);
        return 1; 
    } 
    
    int ListLength(LinkList head)//return link list length 
    {         ListNode *p;         int count = 0;         p = head;         while(p->next!=NULL)         {             p=P->next;             count++;         }         return count;     }      void DestroyList(LinkList head)//Destroy the linked list      {         ListNode *p,*q;         p = head;         while(p!=NULL)         {










    






            q=p;
            p=p->next;
            free(q);
        } 
    } 
    
    Circular singly-linked list: refers to the next of the last node (carriage) pointing to the head of the car

    Doubly linked list: each node has two pointers, one pointing to the front and one pointing to the back
 

4. stack

#define StackSize 100
typedef int DataType;
typedef struct
{     DataType stack[StackSize];     int top; } SeqStack; void InitStack(SeqStack *S)//initialize the stack  {     S->top = 0; } int StackEmpty(SeqStack S)/ /Judge whether the stack is empty  {     if(S.top==0)         return 1;     else         return 0; } int GetTop(SeqStack S,DataType *e)//Get an element  {     if(S.top<=0)     {         cout<<"The stack is empty"<<endl;         return 0;     }     else     {         *e = S.stack[S.top-1];         return 1;

























    } 

int PushStack(SeqStack *S,DataType e)//Push operation 
{     if(S->top>=StackSize)     {         cout<<"The stack is full"<<endl;         return 0;     }     else     {         S-> stack[S->top] = e;         S->top++;         return 1;     }  int PopStack(SeqStack *S, DataType *e) //Pop operation {     if(S->top==0)     {         cout<<"The stack is empty"<<endl;         return 0;     }     else     {         S->top--;         *e = S ->stack[S->top];         return 1;     }  }


























int StackLength(SeqStack S)//return stack length 
{     return S.top; void ClearStack(SeqStack *S)//clear stack  {     S->top = 0; int main() {     SeqStack zhan;     InitStack(&zhan) ;//Initialize the stack      return 0; }











5. Queue
#define QueueSize 100
typedef int DataType;
typedef struct Squeue{     DataType queue[QueueSize];     int front, rear; }SeqQueue; void InitQueue(SeqQueue *SQ)//Initialize the queue {     SQ->front = SQ->rear = 0; int QueueEmpty(SeqQueue SQ)//judging whether the list is empty {     if(SQ.front==SQ.rear)         return 1;     else         return 0; int EnterQueue(SeqQueue *SQ,DataType x)//Enter Queue operation  {     if(SQ->rear==QueueSize)         return 0;     SQ->queue[SQ->rear] = x;     SQ->rear++;     return 1; }






















int DeteleQueue(SeqQueue *SQ,DataType *e)//dequeue operation
{     if(SQ->front==SQ->rear)//judging whether it is empty          return 0;     else     {         *e = SQ->queue[SQ ->front];         SQ->front++;         return 1;     }  int main() {     SeqQueue dl;     InitQueue(&dl);     if(QueueEmpty(dl))         cout<<"The queue is temporarily empty"<<endl;     EnterQueue( &dl,5);     int a;     DeteleQueue(&dl,&a);     cout<<a<<endl;     return 0; }





















 

6. tree

Tree node: Contains a data element and some information pointing to subtree branches.
Degree of a node: The number of subtrees a node has is called the degree of the node.
Leaf node: Also known as terminal node, a node with no subtree, that is, a node with zero degree, is called a leaf node.
Branch node: also known as non-terminal node, the node whose degree is not zero is called non-terminal node.
Child node: The root node of a node's subtree is called a child node.
Parent node: also known as parent node, if a node has a child node, the node is called the parent node or parent node of the child node.
Descendent node: Any node in the subtree of a root node is called the descendant node of the node.
Ancestor node: From the root node to a node, all the branch nodes passed through are called the ancestor nodes of the node.
Brother node: All child nodes of a parent node are called brother nodes.
Degree of the tree: the maximum value of the degrees of all nodes in the tree.
The hierarchy of the tree: starting from the root node, the root node is the first layer, the child node of the root node is the second layer, and so on, if a node is the Lth layer, other child nodes are in the Lth layer +1 layer.
The depth of the tree: also becomes the height of the tree, and the maximum level of all nodes in the tree is called the depth of the tree.
Ordered tree: If the order of each subtree in the tree is sequential, the tree is called ordered tree.
Unordered tree: If the order of each subtree in the tree is not in order, then the tree is called unordered tree.
Forest: Disjoint trees of class m form a forest.

Binary tree: A node has at most two child nodes.

Full binary tree: Each level of nodes is full called full binary tree.
Complete binary tree: If a binary tree has n nodes, and the structure of the n nodes of the binary tree is exactly the same as the structure of the first n nodes of the full binary tree, then such a binary tree is called a complete binary tree.

The nature of the binary tree:
(1) In the binary tree, there are at most 2 m-1 nodes on the mth layer (m>=1) (the root node is specified as the first layer).
(2) There are at most 2 to the kth power minus 1 node in a binary tree with a depth of k (k>=1).
(3) For any binary tree T, if the total number of leaf nodes is n0, the total number of nodes with degree 2 is n2. , then there is n0=n2+1
(4) If the complete binary tree has n nodes, the depth is the largest integer not greater than the logarithm of log with base 2 plus 1.
(5) If the complete binary tree has n nodes, the binary tree is numbered from 1 to n for each node in the binary tree in order from top to bottom and from left to right, then for any node i has the following properties :
a. If i=1, the node corresponding to the serial number i is the root node, and this node has no parent node. If i>1, then the serial number of the parent node of the node with the serial number i is the largest integer not greater than i/2; b. If 2*
i>n, then the node with the serial number i has no left child node; If 2*i<=n, then the serial number of the left child node of the node with the serial number i is 2*i.
c. If 2*i+1>n, then the node with the serial number i has no right child node. If 2*i+1<=n, then the sequence number of the right child node of the node with sequence number i is 2*i+1.

typedef int DataType;
typedef struct Node
{     struct Node *leftchild;//point to the left child node     struct Node *rightchild;//point to the right child node      DataType data;//data field  }*BitTree,BitNode; void InitBitTree(BitTree * T)//Initialize the binary tree  {     *T = NULL; void DestroyBitTree(BitTree *T)//Destroy (delete) the binary tree  {     if(*T)//Whether the binary tree is empty     {         if((*T)->leftchild)             DestroyBitTree(&(*T)->leftchild);         if((*T)->rightchild)             DestroyBitTree(&(*T)->rightchild);         free(*T);         *T = NULL;     }  } void CreateBitTree( BitTree *T) //Create a binary tree 





















{
    DataType ch;
    cin>>ch;
    if(ch==0)
        *T = NULL;
    else
    {
        *T = (BitTree)malloc(sizeof(BitTree));
        if(!(*T))
            exit(-1);
        (*T)->data = ch;
         CreateBitTree(&(*T)->leftchild);
         CreateBitTree(&(*T)->rightchild);
    }
}
int InsertLeftChild(BitTree p,BitTree c)//二叉树的左插入操作
{
    if(p)
    {
        c->rightchild = p->leftchild;
        p->leftchild = c;
        return 1;
    }
    return 0;

int InsertRightChild(BitTree p,BitTree c)//Binary tree right insertion operation
{     if(p)     {         c->rightchild = p->rightchild;         p->rightchild = c;         return 1;     }     return 0; BitTree Point( BitTree T,DataType e)//The operation of returning the binary tree node pointer  {     BitTree Q[MaxSize];// MaxSize size defined by yourself     int front=0, rear=0;      BitNode *p;     if(T)     {         Q[rear] =T;         rear++;         while(front!=rear)         {             p=Q[front];             front++;             if(p->data==e)                 return p;























            if(p->leftchild)
            {                 Q[rear++]=p->leftchild;             }             if(p->rightchild)             {                 Q[rear++]=p->rightchild;             }         }     }     return NULL; DataType LeftChild(BitTree T, DataType e)// Return the left child element value operation of the node of the binary tree {     BitTree p;     if(T)     {         p = Point(T,e);         if(P&&P->leftchild)             return p->leftchild->data;     }      return ; DataType RightChild(BitTree T,DataType e)//Return the right child element value operation of the node of the binary tree {























    BitTree p;
    if(T)
    {         p = Point(T,e);         if(P&&P->rightchild)             return p->rightchild->data;     }      return ; } int DeleteLeftChild(BitTree p) // Left delete operation of binary tree {     if(p)     {         DestroyBitTree(&(p->leftchild));         return 1;     }      return 0; } int DeleteRightChild(BitTree p) //Left delete operation of binary tree {     if(p)     {         DestroyBitTree(&(p- >rightchild));         return 1;     }      return 0; } int main() {


























    BitTree T;
    InitBitTree(&T);
    return 0;
}

Binary tree traversal:
(1) Pre-order traversal:
a. Visit the root node
b. Pre-order traversal of the left subtree
c. Pre-order traversal of the right subtree
(2) In-order traversal:
a. In-order traversal of the left subtree
b. Visit the root node
c. In-order traversal of the right subtree
(3) Post-order traversal
a. Post-order traversal of the left subtree
b. Post-order traversal of the right subtree
c. Visit the root node

===================================================

Thank you for your likes! May every programmer avoid detours, every blog is output with heart, hope to get your approval!

Guess you like

Origin blog.csdn.net/2301_76461941/article/details/131075924