C language linear table operation

Type name: Linear list (List)
Data object set: Linear list is an ordered sequence composed of n (>=0) elements (a1, a2, a3..., an)
Operation set: Linear list L ∈
         ElementType ElementType : It can be an integer, a real type, or a structure; 
         
1. List MakeEmpty(): initialize an empty linear table L; 
2. ElementType FindKth(int K, List L): return the corresponding element according to the bit order K;
3. int Find(ElementType X, List L): Find the first occurrence position of X in the linear table L;
4. void Insert(ElementType X, int i, List L): Insert a new element X before the position sequence i; 
5.void Delete(int i,List L): delete the element of the specified bit order i;
6.int Length(List L): return the length n of the linear list L. 

1. The implementation of the sequential storage of the linear table
    uses the continuous storage space of the array to sequentially store the elements of the linear table
typedef struct LNode *List;
struct LNode{     ElementType Data[MAXSIZE];     int Last; };  struct LNode L; List PtrL;     access Elements marked i: L.Data[i] or PtrL->Data[i]     Length of linear table: L.Last+1 or PtrL->Last+1 







//1.初始化(建立空的顺序表) 
List MakeEmpty()
{
    List Ptrl;
    Ptrl=(List)malloc(sizeof(struct LNode));
    ptrl->last=-1;
    return Ptrl;
 } 
 
//2.查找
int Find(ElementType X,List Ptrl)
{
    int i=0;
    while(i<=Ptrl->Last&&Ptrl->Data[i]!=X)
    i++;
    if(i>Ptrl->Last)
    return -1;
    else 
    return i;

//3.插入操作实现
void Insert(ElementType X,int i,List PtrL)
{
    int j;
    if(PtrL->Last==MAXSIZE-1)
    {
        printf("表满");
        return;
    }
    if(i<1||i>PtrL->Last+2)
    {
        printf("位置不合法");
        return;
    }
    for(j=PtrL->last;j>=i-1;j--)
        PtrL->Data[j+1]=PtrL->Data[j];
    PtrL->Data[i-1]=X;
    PtrL->Last++;
    return; 

//4. Delete operation to achieve
void Delete(int i,List PtrL)
{     int j;     if(i<1||i>PtrL->Last+1)     {         printf("The % dth element does not exist",i );         return;     }     for(j=i;j<=PtrL->Last;j++)         PtrL->Data[j-1]=PtrL->Data[j];     PtrL->Last--;     return;  二·The chain storage implementation of linear table typedef struct LNode *List;  struct LNode{     ElementType Data;     List Next; }; struct Lnode L; List PtrL; 1. Find the length of the table int Length(List PtrL) {     List p=PtrL;     int j=0;     while(p)     {











 
 








 







        p=p->Next;
        j++;
        }    
    return j;

 
2.查找
(1)按序号查找:FindKth;
List FindKth(int K,List PtrL)
{
    List p=PtrL;
    int i=1;
    while(p!=NULL&&i<k)
    {
        p=p->Next;
        i++;
     } 
    if(i==K)
    return p;
    else
    return NULL;
 } 
(2)按值查找:Find;
List Find(ElementType X,List PtrL)
{
    List p=PtrL;
    while(p!=NULL&&p->Data!=X)
        p=p->Next;
    return p;

3.插入
List Insert(ElementType X,int i,List PtrL)
{
    List p,s;
    if(i==1)
    {
        s=(List)malloc(sizeof(struct LNode));
        s->Data=X;
        s->Next=PtrL;
        return s;
    }
    p=FindKth(i-1,PtrL);
    if(p==NULL)
    {
        printf("参数i错");
        return NULL; 
    }else{
        s=(List)malloc(sizeof(struct LNode));
        s->Data=X;
        s->Next=p->Next;
        p->Next=s;
        return PtrL;
    }

4. Delete
List Delete(int i,List PtrL)
{     List p,s;     if(i==1){         s=PtrL;         if(PtrL!=NULL) PtrL=PtrL->Next;         else return NULL;         free( s);         return PtrL;     }     p=FindKth(i-1,PtrL);     if(p==NULL){         printf("The %d node does not exist",i-1);         return NULL;     }else if (p->Next==NULL){         printf("The %d node does not exist",i);         return NULL;     }else{         s=p->Next;         p->Next=s->Next;         free (s);         return PtrL;     } generalized table






















 

    An element in a generalized table can be a single element or another generalized table. 
typedef struct GNode *GList;
struct GNode{     int Tag;     union{         ElementType Data;         GList SubList;     }URegion;     GList Next; }; 






Sequence table

 typedef int Position;
typedef struct LNode *List; 
struct LNode 
{     
     ElementType Data[MAXSIZE];   
    Position Last;
}; /* Initialization*/ 
List MakeEmpty()
{     
    List L; 
    L = (List)malloc(sizeof(struct LNode)) ;
    L->Last = -1;
    return L; 
} /* Find*/ 
#define ERROR -1 
Position Find( List L, ElementType X) 
{     
    Position i = 0;     
    while( i <= L->Last && L- >Data[i]!= X) 
    i++;     
    if (i> L->Last) return ERROR; /* If not found, return an error message*/ 
    else return i; /* If found, return the storage location*/ 
} /* Insert*/ /*Note: The insertion position parameter P is different from the course video.
    In the course video, i is the sequence position (starting from 1), where P is the storage index position (starting from 0), the difference between the two is 1*/ 
bool Insert( List L, ElementType X, Position P) 

/* in L Insert a new element X before the specified position P */     
    Position i; 
    if (L->Last == MAXSIZE-1) 
    {/* The table space is full and cannot be inserted */        
     printf("table full");          
     return false;     
    }       
    if (P<0 || P>L->Last+1) 
    {/* Check the validity of the insertion position*/         
    printf("The position is illegal");         
    return false;      
    }      
    for( i=L->Last; i>=P; i--)         
    L->Data[i+1] = L->Data[i]; /* Move the order of elements at position P and beyond */     
    L->Data[P] = X; /* New element insertion*/     
    L->Last++;      /* Last still points to the last element*/
    return true;  
} /* Delete*/ /*Note: The deletion position parameter P is different from the
    course video . In the course video, i is the sequence position (starting from 1), where P is the storage subscript position (from 0 Start), the difference between the two is 1*/ 
bool Delete(List L,Position P) 
{/* Delete the element at the specified position P from L*/ 
    Position i;
    if( P<0 || P>L->Last)
    { /* Check the validity of the empty list and the deleted position*/        
        printf("No element exists in position %d", P );         
        return false;     
    }     
    for( i=P+1; i<=L->Last; i++)   
       L ->Data[i-1] = L->Data[i]; 
    /* Move the elements at position P+1 and later in order */    
    L->Last--; /* Last still points to the last element*/     
    return true;    
}

  Linked watch

 typedef struct LNode *PtrToLNode;
struct LNode {     
    ElementType Data;     
    PtrToLNode Next; 
}; 
typedef PtrToLNode Position; 
typedef PtrToLNode List; /* Find*/ 
#define ERROR NULL 
Position Find( List L, ElementType X) 
{Position p = L; / * p points to the first node of L*/     
    while (p && p->Data!=X)         
    p = p->Next; /* The following statements can be replaced with return p; */     
    if (p)         
    return p;     
    else         
    return ERROR; 
} /* Insert the leading node*/ /*Note: The insertion position parameter P is different from the
course video . In the course video, i is the sequence position (starting from 1), where P is the linked list node Point pointer, insert a new node before P*/ 
bool Insert( List L, ElementType X, Position P) 
{/* The default L has a head node here*/     
    Position tmp, pre; /* Find the previous node of P*/             
    for (pre=L; pre&&pre->Next!=P; pre=pre->Next );                 
    if (pre==NULL) {/* The node pointed to by P is not in L*/         
        printf("Insert position parameter error\n");         
        return false;     
    }     
    else {/* Found the previous node pre of P */ /* Insert a new node before P*/         
    tmp = (Position)malloc(sizeof(struct LNode)); /* Apply and fill the node*/         
    tmp->Data = X;          
    tmp->Next = P ;         
    pre->Next = tmp;         
    return true;} 
} /* Deletion of the leading node*/ /*Note: The deletion position parameter P is different from the
course video . In the course video, i is the sequence position (from 1 Start), where P is the pointer of the node to be deleted*/ 
bool Delete( List L, Position P)
{/* Here default L has a head node*/     
    Position pre; /* Find the previous node of P*/             
    for (pre=L; pre&&pre->Next!=P; pre=pre->Next );                 
    if ( pre==NULL || P==NULL) {/* The node pointed to by P is not in L*/         
        printf("delete position parameter error\n");         
        return false;
     } else {/* Found the front of P A node pre */ /* delete the node at position P*/         
     pre->Next = P->Next;         
     free(P);         
     return true;     
     } 
}

Guess you like

Origin blog.csdn.net/SignalFire/article/details/109404695