Auxiliary tutorial on data structure of Piglet--2.1 order table in linear table

Source: Internet
Author: User

Auxiliary tutorial on data structure of Piglet--2.1 order table in linear table

tags (space delimited): Data structure

Learning Roadmap and Learning Essentials in this section

Learning Essentials :

  • 1. The concept of abstract data type (ADT), three elements: data, relationship between data elements and manipulation of data
  • 2. Characteristics of linear table: according to a line of data set, 1 to 1, except the first and the tail, the other elements have direct precursor and direct successor
  • 3. Keep in mind the storage structure of linear tables, understand and familiarize yourself with the logic of the 12 basic operations, preferably by tearing out the code with bare hands
  • 4. The classic example of the order table must be mastered!
1. Abstract data types

Simply put:

Abstract: A bit like our object-oriented language of the idea of the class, the commonality of things extracted, the formation of properties and corresponding methods!
Data type: For example, we C Middle School integral type, float type and so on these are the data type, is has the same nature the collection;
In addition to the definition of floating-point type is to avoid some trouble, such as saving 2 of this integer with floating-point type is a bit overqualified ~

2. Introduction of linear table and sequential table

From the storage structure above, it is not difficult to know the three properties of the order table:
The location of the storage space; maximum storage capacity listsize; current length;

Code implementation of 3.12 basic operations

PS: Do not go to rote code, the logic to grasp the good, do not know how to make a few data, draw pictures!

The storage structure of linear tables
#define MAXSIZE#define INCREMENT#Define OK 1#define ERROR 0#Define TRUE 1#define FALSE 0//define an alias for an int data type//Here because the demo takes int, the actual use of the composite data type is usuallytypedefintElemtype; typedefintStatus;typedefstruct{Elemtype *elem;//The start address of the storage space    intLength//The current length of the table    intListsize;//Table total storage capacity in sizeof (Elemtype)}sqlist;
1. Constructing Empty Tables
void InitList(SqList *L){    L -> elem = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));    if(!L -> elem)exit(ERROR);    L ->0;    L -> listsize = MAXSIZE;}

Steps:

  • Step 1: The storage space of the application form, assigning the starting address of the space to the Elem,
    Then know if the memory is allocated successfully by judging whether the elem is empty
  • Step 2: Set the current length of the table to 0, set the table size listsize to MaxSize
2. Set the table to an empty table
void ClearList(SqList *L)  {      L ->length0;  }

Steps:

Simply set the table length to empty, which is simple

3. Determine if the table is empty
//3)判断表是否为空Status ListEmpty(SqList L){       return0?TRUE:FALSE

Steps:

Just determine if the table length is 0

4. Destruction of tables
//4)销毁表void*L){    ->elem);    ->=NULL;    ->=0;    ->=0

Steps:

  • Step 1: Call the Free method to release the memory space that L.elem points to
  • Step 2: Assign a value of L.elem to null
  • Step 3: Set the current table length and the storage capacity of the table to 0
5. Get the number of elements in the table
int ListLength(SqList L)  {      return L.length;   }

Steps:

Return the current table length directly

6. Get the value of element I in a table
Status GetElem(SqList L,int*e){    if1||i > L.length)return ERROR;    *(1);    return OK; }  

Steps:

Step 1: First determine whether the first position is legal
Step 2: Assign the value of the corresponding position to E, and subtract 1 here because the array subscript is calculated from 0, and we look at the table is
Starting from 1, so you need to subtract one

7. Find out if there are elements in the table that meet the requirements
int LocateElem(SqList L,ElemType e,Status(*compare)(ElemType,ElemType))  {      int1;      *p = L.elem;      while(i < L.length && !compare(*p++,e))      i++;      if(i <= L.lengthreturn i;      elsereturn0;  }

Steps:

Step 1: Define the variable i and pointer p to the first address of the 1th element and table, respectively
- Step 2: Loop calls the Compare () function to compare with the elements in the table
- Step 3: If I <= length, the description is found, returning the ordinal of the corresponding element
- Step 4: Otherwise returns 0, indicating that the element that satisfies the requirement is not found

8. Return the predecessor of a node
Status PriorElem(SqList L,ElemType choose,ElemType &Before){    int2;    *p1;    while(i < L.length*p != choose)    {        p++;        i++;    }    if(i > L.length)return ERROR;    else    {        *--p;        return OK;    }       

Steps:

Step 1: The first element is no direct precursor, so I should start from 2, so p starts with L.elem + 1;
Step 2: Cycle through the position of the data element in the table, if I > table length indicates that it is not found, returns error;
Step 3: If found, the value of the precursor of the element obtained by *–p is assigned to before;

9. Returns the successor of a node
*behind){    int1;    *p = L.elem;     while(i < L.length*p != choose)    {        p++;        i++;     }     if(i == L.length)return ERROR;    else{        behind = * ++p;        return OK;    

Steps:

And the precursor that almost, but note i = = L.length on the head, because the tail is no successor!

10. Insert an element into the table I position
Status Listinsert (sqlist*L,int I,elemtype e) {elemtype*P*Q*Newbase;//Determine if the insertion position is valid    if(I< 1 ||I>L -Length+ 1)returnERROR;//If the meter is full, increase the allocated space    if(L -Length==L -Listsize) {Newbase=(Elemtype*) ReAlloc (L -Elem, (L -Listsize+INCREMENT)*sizeof (Elemtype));if(!newbase) exit (ERROR); L -Elem=Newbase; L -Listsize+=INCREMENT; }//Insert positionQ=L -Elem+I- 1;//Shift to the right, move the last one firstFor (p=L -Elem+L -Length- 1;p>=Q--P) {*(p+ 1)= *P }*Q=E//Insert Element    ++L -Length//Table length + 1    returnOK;}

Steps:

11. Remove elements from the I position in a table
Status Listdelete (sqlist*l,intI,elemtype*e) {Elemtype*p,*q;//Determine if the deleted location is legitimateif(I <1|| i > Llength)returnERROR;//The P pointer points to the delete position, and the element to be deleted is assigned to e p = L->elem + i-1; E =*p;//qThe pointer points to the last element, starting at the left of the element after the delete positionQ= L->elem + Llength-1; for(++p;p <=Q; ++p) {*(P-1) =*p; } L--length--;//Table Length-1        returnOK; }

Steps:

12. Traversing all elements in a table
void ListTraverse(SqList L,void(* visit)(ElemType)){    int i;    *p = L.elem;    for1;i < L.length;i++)    {        visit(*p++);        printf("\n");    }}

Steps:

It is simple to start with the first element, move the pointer behind it, and call the visit () function in turn.
Implements the traversal of elements in a table

4. Apply a small example:

Questions :
It is known that the elements in A, B, two sequential table are arranged from small to large; now you ask for two of the set C;
and the elements in C also have to be sorted from small to large, without changing the data in A and B!
It's a set of elements that can't be duplicated in C.

void Unionlist (SqList la,sqlist lb,sqlist*LC){//1. Defines a pointer to a table, a, a header, a footer elemtype*la,*la_end,*lb,*lb_end,*LC;    La = La.elem;    LB = Lb.elem; La_end = La.elem + La.length-1; Lb_end = Lb.elem + Lb.length-1;//2Allocate memory space for table C, size table a length + table b length Lc->listsize = La.length+ Lb.length;LC= Lc->elem = (elemtype*)malloc (Lc->listsize * sizeof (elemtype));if(!LC)Exit(ERROR);//3The elements in tables A and B are merged. while(La <= la_end && lb <= lb_end) {if(*la<=*lb)*LC++ =*la++;Else *LC++ =*lb++; }     //4If there are any remaining elements, either table A, or table B's while(La < La_end)*LC++ =*la++; while(LB < Lb_end)*LC++ =*lb++;//5To remove the duplicate elements from the C table, here are two pointers elemtype*p,*q; p = Lc->elem;Q= P;//6loop, if pointer p does not point to footer while(P! = (LC->elem + LClength-1))    {//qPoint to the successor of P, compare the value of two numberif(*p!=*(++Q)) {p++;Q= P; }//equal words, deleteQThe element that is pointing toElse{ while(*p==*q);//Listdelete (Lc, (P-LC->elem), E); }     } }
5. This section of the code download:

Time relationship, back to the company in the morning and then put out ha ~

Auxiliary tutorial on data structure of Piglet--2.1 order table in linear table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.