Pig Data Structure Learning notes (2), pig Data Structure Learning
Pig's Data Structure Learning notes (2)
Sequence Table in linear table
This section introduces:
In the previous chapter, we learned about the concepts related to data structures and algorithms, and learned about the concepts related to data structures.
The difference between the logical structure and the physical structure, the characteristics and design requirements of the algorithm, and how to measure an algorithm
Good or bad, and computing of time complexity! In this section, we will contact the first data structure-linear table;
Linear tables have two forms: sequential tables and linked lists. Learning this chapter is very important and is the cornerstone of learning;
In this section, we will focus on learning the sequence table. Here we will give you a piece of advice. To learn programming, you should never be very easy. Understanding it doesn't mean you are yourself.
You can write the implementation code, understand the ideas, and write the code yourself! When I write more, I feel it! Think more !!
We also attached a powerful tool for learning data structures. The data structure demonstration animation is provided at the end of this article!
Okay, that's all nonsense!
This section describes the road map.
Roadmap analysis:
① Abstract data types are three elements: data, relationships between data, and data operations
② A linear table is a set of data arranged by a line. One-to-one, except for the first and last elements, other elements have direct precursor and direct successor.
③ The sequence table is a bit like the previously learned array. be careful that the I element in the table corresponds to the data at the position I-1 !!!!
④ Keep in mind the basic storage structure. As for the 12 basic operations, apart from insertion and deletion, it is a little difficult, and others are very simple. You should write the code several times by yourself!
⑤ It's not difficult to find the Union. You should understand it if you think about it! I did not expect any good method when I removed the repeated elements in the table. I am welcome to raise it!
Body:
1. Abstract Data Types
Ii. sequence table in linear table
Iii. Code implementation and analysis of related operations:
1. Storage Structure of linear tables:
<Span style = "font-family: Microsoft YaHei; "> # define MAXSIZE 20 # define INCREMENT 10 # define OK 1 # define ERROR 0 # define TRUE 1 # define FALSE 0 // defines an alias for an int data type. // The demo int, the actual use is usually the composite data type typedef int ElemType; typedef int Status; typedef struct {ElemType * elem; // The starting address of the bucket int length; // The current table length int listsize; // total storage capacity of the table (sizeof (ElemType)} SqList; </span>
2. Construct an empty table
<Span style = "font-family: Microsoft YaHei;"> // create an empty table: void InitList (Sqlist & L) {L. elem = (ElemType *) malloc (MAXSIZE * sizeof (ElemType); if (! L. elem) exit (ERROR); L. length = 0; L. listsize = MAXSIZE ;}</span>
Code Parsing
: Steps:
① The bucket in the application form, assign the actual address of the space to elem, and then determine whether elem is empty to know whether the memory is allocated successfully.
② Set the current length of the table to 0 and the listsize of the table to MAXSIZE.
3. empty table
<Span style = "font-family: Microsoft YaHei;"> // void ClearList (SqList & L) {L. length = 0 ;}</span>
Code parsing:
You only need to set the length of the table to 0.
4. Check whether the table is empty.
<Span style = "font-family: Microsoft YaHei;"> // checks whether the table is empty. Table Status ListEmpty (SqList L) {if (L. length = 0) return TRUE; else return False;} </span>
Code Analysis:
Similarly, you only need to determine whether the length value is 0.
5. Destroy the table
<Span style = "font-family: Microsoft YaHei;"> // destroy the table void DestroyList (SqList & L) {free (L. elem); L. elem = NULL; L. length = 0; L. listsize = 0 ;}</span>
Code Analysis:
Steps:
① Call the free function to release the memory of the memory space pointed by elem.
② Assign the value of L. elem to NULL, that is, a NULL pointer.
③ Set the current Length of the table and the maximum Length of the table listsize to 0.
6. Obtain the number of data elements in the table.
<Span style = "font-family: Microsoft YaHei;"> // obtain the number of elements in the current table. int ListLength (SqList L) {return L. length ;}</span>
Code Analysis:
Returns the length of the current length.
7. Obtain the value of the I Data Element in the table.
<Span style = "font-family: Microsoft YaHei;"> // obtain the value of the I element in the table. The value of e reflects Status GetElem (SqList L, int I, elemType & e) {if (I <1 | I> L. length) return ERROR; e = * (l. elem + I-1); return OK ;}</span>
Code Analysis:
① First judge whether the position I is valid
② Assign the value of the position to e, and subtract one from the other because the array is subscript starting from 0 !!
We can see that the table is counted from 1, so we need to subtract one.
8. check whether there are elements in the table that meet the requirements.
<Span style = "font-family: Microsoft YaHei; "> // return the subscript of the first element in the table that meets the requirements. // we assume that the requirement is a function compare (int l_s, int e ); // The parameters are the element in the table and the parameter e that needs to be compared. // The third parameter function pointer, the hammer pointer to the function int LocateElem (SqList L, ElemType e, status (* compare) (ElemType, ElemType) {int I = 1; ElemType * p = L. elem; while (I <L. length &&! Compare (* p ++, e) I ++; if (I <= L. length) return I; else return 0 ;}</span>
Code Analysis:
Steps:
① Define the variable I and the pointer p to execute the 1st elements and the first address of the table respectively.
② Compare the cyclic calling of the compare () function with the elements in the table
③ If I <= length is found, the sequence number of the corresponding element is returned.
④ Otherwise, 0 is returned, indicating that no result is found.
9. Return the precursor of a node
<Span style = "font-family: Microsoft YaHei;"> // return the direct precursor of a node. // The second and third parameters are the specified data elements, respectively, before is used to store its predecessor Status PriorElem (SqList L, ElemType choose, ElemType & before) {int I = 2; ElemType * p = L. elem + 1; while (I <= L. length & * p! = Choose) {p ++; I ++;} if (I> L. length) return ERROR; else {before = * -- p; return OK ;}</span>
Code Analysis:
① There is no precursor to the first dollar, so I starts from 2 and p from L. elem + 1.
② View the position of the specified data element in the table in a loop. If I> L. length indicates that no data is found, ERROR is returned.
③ If found, use * -- p to obtain the value of the element precursor and assign the value to the before variable.
10. Return the successor of a node
<Span style = "font-family: Microsoft YaHei;"> // return the value of the selected data element directly after a pivot. // The value of the selected data element is displayed in sequence, store the subsequent variables Status NextElem (SqList L, ElemType choose, ElemType & behind) {int I =; ElemType * p = L. elem; while (I <L. length & * p! = Choose) {p ++; I ++;} if (I = L. length) return ERROR; else {behind = * ++ p; return OK ;}</span>
Code Analysis:
① It is similar to the above precursor. It should be noted that I = L. length is enough, because the final element has no successor, so the final element is directly excluded!
11. insert an element to position I in the table
<Span style = "font-family: Microsoft YaHei;"> // Insert the element eStatus ListInsert (SqList & L, int I, ElemType e) to the position I of the table) {ElemType * p, * q, * newbase; // The insert position is 1-length, and other values are invalid if (I <1 | I> L. length + 1) return ERROR; // if the table 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;} p = L. elem + I-1; // shift to the right, first move the last for (q = L. elem + L. length-1; q> = q; -- q) * (q + 1) = * q; // insert e, table length + 1 * q = e; ++ L. length; return OK ;}</span>
Code parsing:
12. Delete the element at position I in the table
<Span style = "font-family: Microsoft YaHei;"> // Delete the Status ListDelete (SqList & L, int n, ElemType & e) element at position I in the table) {ElemType * p, * q; // determine whether the deleted location is legal if (I <1 & I> L. length) return ERROR; // point to the location to be deleted and pay the value to e p = L. elem + I-1; e = * p; // point to the final element, starting from the successor element of the delete position. q = L. elem + L. length-1; for (+ + p; p <= q; p ++) * (p-1) = * p; L. length --; return OK ;}</span>
Code Analysis:
13. traverse all elements in the sequence table
<Span style = "font-family: Microsoft YaHei;"> // traverses all elements of a linear table, void ListTraverse (SqList L, void (* visit) (ElemType &)) {int I = 1; ElemType * p = L. elem; for (I = 1; I <= L. length; I ++) {visit (* p ++); printf ("\ n") ;}</span>
Code parsing:
The code is very simple, that is, starting from the first element, let the pointer move back, and call the visit () function in turn
Implement element traversal in a table
14. Small application example:
It is known that the elements in the sequence tables A and B are arranged in ascending order. Now you need to calculate the Union set C of the two;
In addition, the elements in C should also be sorted in ascending order, without changing the data in A and B!
It is a Union, that is, there cannot be repeated elements in C.
<Span style = "font-family: Microsoft YaHei;"> void UnionList (SqList La, SqList Lb, SqList & Lc) {// defines the pointer to the Table of the La AND Lb headers, and ElemType * la, * la_end, * lb, * lb_end, * Lc; la = La. elem; lb = La. elem; la_end = La. elem + La. length-1; lb_end = Lb. elem + Lb. length-1; // the size of the memory allocated to the lc is La. length + Lb. lengthLc. listsize = Lc. length = La. length + La. length; lc = Lc. elem = (ElemType *) malloc (Lc. listsize * sizeof (ElemType )); If (! Lc. elem) exit (ERROR); // merge the elements in a and B // If, while (la <= la_end & pb <= lb_end) {if (* la <= * lb) * lc ++ = * la ++; else * lc ++ = * lb ++;} // if there are other elements, either a or bwhile (la <= la_end) * lc ++ = * la ++; while (lb <= lb_end) * lc ++ = * lb ++; // Delete the repeated elements in Lc from ElemType * p, * q; p = Lc. elem; q = p; // use two pointers, p and q. If the pointer p does not point to the end while (p! = (Lc. length-1) {// q points to the suffix of p and compares the two values if (* p! = * (++ Q) {p ++; q = p ;}// if they are equal, delete the elements pointed to by q and then compare them, until the repeat ends, else {while (* p = * q) ListDelete (Lc, (p-Lc. elem), e) ;}}</span>
Ps: Finally, the algorithm for removing duplicate elements in Lc seems a little cumbersome. If you have an efficient algorithm, please feel very grateful!
Summary of this chapter:
① What is an abstract data type ADT?
② Definitions of linear tables and features of linear tables
③ Sequence table: a linear table stored by a sequential address storage unit at a time
④ Sequence table structure, three basic attributes
⑤ 12 operations related to the sequence table
⑥ Small example of sequence table usage
Download learning resources:
Package the code in this section: Click to download
Data Structure demo tool: Click to download
Error Message
What should I do if my child has a high volume of lead?
Don't let him eat popcorn.