DataStructure the linear form and its implementation

Linear table

Applications: polynomial representation

 

What is the linear form

Revelation given polynomial representation issues:

  The same question can have different representations (stored) method

  One class of common problems: rental and management of orderly linear sequence

 

"Linear table (Linear List)": linear structure constituted by an ordered sequence of data elements of the same type

  The number of elements in the table is called the length of the linear form

  When the table is not linear elements, it referred to the empty list

  Table origin location called a header, referred to the end position of the table footer

 

Linear table abstract data type description

 

Type Name: Linear meter (List)

Sets of data objects: linear table is n (> = 0) of an ordered sequence of elements constituting the (a_1, a_2, a_3, ...., a_n)

The set of operations: a linear list L List belongs, represents the position of the integer i, the element X belonging ElementType

 

Linear form basic operations are:

  MakeEmpty List () : initializing an empty list of L;

  FindKth the ElementType (int K, L List) : The rank K, returns the corresponding element;

  the Find int (ElementType X, List L) : L linear table find the first occurrence of X position;

  the Insert void (the ElementType X-, int i, List L) : i preamble of X-bit inserting a new element;

  the Delete void (int i, List L) : Deletes the specified rank i of the element;

  the Length int (List L) : returns the length of the linear form of n-L;

 

Linear form of sequential storage implemented

  Using an array of contiguous memory storage order of the elements of the linear form

 

 

#define MAXSIZE    <............>

typedef struct LNode  *List;

struct LNode{

  ElementType Data[MAXSIZE];

  int Last;

};

struct LNode L;

List PtrL;

  Access subscript element i: L.Data [i] or PtrL-> Data [i]

  Length linear form: L.Last + 1 or PtrL-> Last + 1

 

To achieve the main operation

                                                                                                                                                  

List MakeEmpty()

{

  List PtrL;

  PtrL = ( List )malloc( sizeof( struct LNode ) );

  PtrL->Last = -1;

  return PtrL;

Seek

int Find(ElementType K, List PtrL)

{

  int i = 0;

  while ( i <= PtrL->Last && PtrL->Data[i] != X)

    i++;

  IF (I> PtrL-> Last) return -1; // If not found, returns -1

  return the else I; // find the storage location of the return

}

Insert (the i (1 <= i <= n + inserting a new element value X 1) th position)

Here it is to be noted that the code used here is counted from 1 Design

Insertion operation embodied

It should be noted here that, where the position is counted from 1 (the default is the human element in the first position is the first, for the computer, it can also start from a zero count, generally, in the computer large are counted starting from 0)

void Insert ( ElementType X, int  i, List PtrL)

{

  int j;

  IF (PtrL-> Last the MAXSIZE == -. 1) {// the table is full, the new element can not be inserted (here MAXSiZE - 1 can be inserted into the final position, and represents MASIZE table element the most number of elements)

    printf ( "table full");

    return ;

  }

  if ( i < 1 || i > PtrL->Last+2){

/ * (Counting from zero using the position information here) for PtrL-> position information of the last element in the array element number represented by the list Last, so if using numbered from 1, then this is the first position PtrL- > last + 1 position on the element, but the scope is to be inserted in the element (1 <= i <= n + 1) that is, a first start position, the start position of the n + 1, and therefore to the last position where the inserted location (n + 1-position) with PtrL-> Last word is represented PtrL-> Last + 2 positions,

Therefore, the range of i is determined herein, can easily know the meaning of this range. * /

    printf ( "illegal position");

    return;

  }

  for ( j = PtrL->Last; i >= i - 1; j--)

    PtrL-> Data [j + 1] = PtrL-> Data [j]; // will move rearwardly descending a_i .... a_n

  PtrL-> the Data [I -. 1] = X- ; // new element inserted

  PtrL-> Last ++; // Last still points to the last element

  return;

}

// insert another version count is employed herein starts counting at 0

void  Insert(List L, Elementype X, Position P)

{
    / * Insert a new element L X before the designated position P * /
    Position i;
    
    if(L->Last == MAXSIZE -1)
    {
        / * The table is full, you can not insert * /
        printf ( "table full");
        return FALSE;
    }
  
    IF (  P <0 || P> L-> Last +. 1 ) { // This is different from the change is adopted where the legality is determined inserting position counting from 0
        // check the legal position when inserted
        printf ( "illegal position");
        return FALSE;
    }

    for( i = L->Last; i >= P; i--)
        L-> Data [i + 1] = L-> Data [i]; // position P and the position after moving back
    L-> the Data [P] = X-; // new element inserted
    L-> Last ++; // Last still points to the last element
    return ;
}

 Remove (delete Table i-th (1 <= i <= n) elements on locations)

 Delete operation implementation (there is also a position count for the title, the following is used in the first position from the start of counting)

void Delete( int i, List PtrL)

{

  int j;

  if ( i < 1 || i > PtrL->Last + 1){

    printf ( "% d absence of element", i);

    return;

  }

  for ( j = i; j <= PtrL->Last; j++)

    PtrL-> Data [j - 1] = PtrL-> Data [j]; // The a_ (i + 1) ..... a_n order to move forward

  PtrL-> Last -; // Last still points to the last element

  return ;

}

// count positions adopted here is counted from 0

BOOL Delete(List L, Position P)
{
    // delete the element specified from the position P of the L
    Position i;

    if ( P < 0 || P > L->Last){
        // check the legality of an empty table set delete location
        printf ( "% d position element is not present", P);
        return FALSE;
    }

    for( i = P + 1; i <= L->Last; i++)
        L-> Data [i - 1] = L-> Data []; // P + 1 position and the position after moved forward
    L-> Last -; // last point to the last element
    return  TRUE;
}

 Complete test code:

/*list_array.c*/

#include <stdio.h>//printf
#include <stdlib.h>//malloc

#define MAXSIZE 2000
#define TRUE 1
#define FALSE 0
#define ERROR -1

typedef int BOOL;
typedef int Elementype;
typedef int Position;

typedef struct LNode* List;

// array for data structure list, defined
struct  LNode{
    Elementype Data[MAXSIZE];
    Position Last;
};

Position Length(List L);

/*init*/
List MakeEmpty()
{
    List L;
    L = (List)malloc(sizeof(struct LNode));
    L->Last = -1;
    return L;
}

/ * Look * /
Position Find(List L, Elementype X)
{
    Position i = 0;
    
    while( i <= L->Last && L->Data[i] != X)
        i++;
    if( i > L->Last){ 
        return ERROR; // if not found, an error is returned
    }else{
        return i; // find the storage location of the return
    }
}

BOOL Insert(List L, Elementype X, Position P)
{
    / * Insert a new element L X before the designated position P * /
    Position i;
    
    if(L->Last == MAXSIZE -1)
    {
        / * The table is full, you can not insert * /
        printf ( "table full");
        return FALSE;
    }

    if( P < 0 || P > L->Last + 1){
        // check the legal position when inserted
        printf ( "illegal position");
        return FALSE;
    }

    for( i = L->Last; i >= P; i--)
        L-> Data [i + 1] = L-> Data [i]; // position P and the position after moving back
    L-> Data [P] = X; // new element inserted
    L-> Last ++; // Last still points to the last element
    return TRUE;
}


BOOL Delete(List L, Position P)
{
    // delete the element specified from the position P of the L
    Position i;

    if ( P < 0 || P > L->Last){
        // check the legality of an empty table set delete location
        printf ( "% d position element is not present", P);
        return FALSE;
    }

    for( i = P + 1; i <= L->Last; i++)
        L-> Data [i - 1] = L-> Data []; // P + 1 position and the position after moved forward
    L-> Last -; // last point to the last element
    return  TRUE;
}

void PrintList(List L)
{
    Position i;
    for(i = 0; i < Length(L); i++)
        printf("%d ", L->Data[i]);
    printf("\n");
}

Position Length(List L)
{
    return L->Last + 1;
}

Elementype getElement(List L, Position P)
{
    return L->Data[P];
}



int main ()
{
    List list;
    list = MakeEmpty();

    Position index = 0;
    for(int i = 10; i >= 0; i--)
    {
        if(Insert(list,i,index)){
            printf("    insert success\n");
        }else{
            printf("    insert failed\n");
        }
        index++;
    }

    PrintList(list);

    printf("this list length : %d\n", Length(list));
    printf("the positon 2 is : %d \n", getElement(list, 10));

    return 0;    
}

Compile and run:



  gcc list_array.c
  ./a.exe

Print Results:

insert success
insert success
insert success
insert success
insert success
insert success
insert success
insert success
insert success
insert success
insert success
10 9 8 7 6 5 4 3 2 1 0
this list length : 11
the positon 2 is : 0

---------- linear form of sequential storage ------------------

 

Guess you like

Origin www.cnblogs.com/Davirain/p/11711289.html