Pig Data Structure Learning notes (3), pig Data Structure Learning

Source: Internet
Author: User
Tags random seed

Pig Data Structure Learning notes (3), pig Data Structure Learning

Pig's Data Structure Study Notes (3)

Single-chain table of linear table


This chapter introduces:


In the previous section, we saw the sequence table in the first linear table data structure;

When you write the operation code several times by yourself, you will feel a little bit confused. If you want to write the sequence table

Insert an algorithm. Can you come up with an approximate code? If you can, you can enter a new chapter;

Otherwise, let's look back! In this section, we will see the chain representation of a linear table-a single-chain table.

What are the advantages and disadvantages of a single-chain table and a sequence table? What are the differences between the head insertion and tail insertion of a single-chain table?

Please follow the steps of the author to parse the single-chain table in a linear table!


This section describes the road map.




Roadmap analysis:

① First, you must understand the advantages and disadvantages of the sequence table and single-chain table!

② Record the form of a single-chain table, understanding the header pointer, header node, tail node, data domain and pointer domain!

③ Code of the node Structure of a single-chain table

④ Differentiate the header nodes of the header pointer field!

⑤ Two ways to create a single-chain table: the header insertion method and the tail Insertion Method

⑥ 12 basic operations on a single-linked table

7. Interesting algorithms: How do I obtain the nodes in the middle of the position row in a single-chain table?



Body:


Single-chain table introduction:




Structure of a single-chain table:




Node storage structure of a single-chain table




Comparison between head pointer and head node




Comparison between the header insertion method and the tail Insertion Method for table creation:


① Process of the header insertion method:


Ps: the header pointer at the beginning


Code:

<Span style = "font-family: Microsoft YaHei;"> void HeadCreateList (LinkList & L, int n) {LinkList p; int I; // initialize the random seed, allocate memory space for the header pointer, and set the pointer field to null srand (time (0); L = (LinkList) malloc (sizeof (LNode )); l-> next = NULL; // use a loop to randomly generate n numbers and store them in a single-chain table: for (I = 0; I <n; I ++) {// generate a new node p = (LNode) malloc (sizeof (LNode); // generate a two-digit random number of 100, and a three-digit random number of 1000, similarly, p-> data = rand () % 100 + 1; p-> next = L-> next; L-> next = p ;}</span>


Code steps:

① Initialize the header node, and let the header pointer point to the header node, and set the pointer field of the header node to NULL;

② Construct the node of the single-chain table. Here we use random generation. Of course, you can also scan the node yourself!

③ After the construction, point the pointer field of the current node to the next node pointed to by L.

④ Point the header pointer to the current node




② Tail plugging Process



PS: because each time a new node is inserted to the end of the linked list, you need to add a pointer R to always point to the end of the target node, so that

Insert the new node to the end of the linked list



Code

<Span style = "font-family: Microsoft YaHei;"> void TailCraeteList (LinkList & L, int n) {LinkList p, r; int I; srand (time (0); L = (LinkList) malloc (sizeof (LNode); L-> next = NULL; r = L; for (I = 0; I <n; I ++) {L = (LNode) malloc (sizeof (LNode); p-> data = rand () % 100 + 1; // note that p is the intermediary node, and the next of the current tail node points to pr-> next = p; // r always points to the end node, and p is the end node, so p is assigned to rr = p;} r-> next = NULL;} </span>




Code steps:

① Construct a header node to point the header pointer to the header node, and assign the value of the header pointer to the pointer R (R always points to the end node)

② Randomly generate n random numbers and construct nodes, and point the pointer field of r to p

③ Because a new tail node is generated and the intermediary Variable p appears, r must point to p again to ensure that p always points to the node.

④ Set the pointer field of the node to NULL.




Interesting algorithms: Finding intermediate nodes

How do I obtain the nodes in the middle of a single-link table?


Analysis:

Elements in a single linked list can only be moved from start to end. It seems that there is only one method, that is, to record the length of a single linked list,

Then divide by 2 to get the number of intermediate nodes! What if we don't know the length? Is there any other way? The answer must be yes,

Here we provide a simple method: using two different pointers, in different moving order, is also called a speed pointer, each loop

① Fast pointer moves two nodes each time: p = p-> next ② slow pointer moves one node each time: q = q-> next;

When the fast pointer reaches the end, the slow Pointer Points to the intermediate node! Done!


Code:

<span style="font-family:Microsoft YaHei;">Status GetMidLNode(LinkList L,ElemType &e){LinkList p,q;p = q = L;while(p -> next -> next != NULL){if(p -> next -> next != NULL){p = p -> next -> next;q = q -> next;}else {p = p -> next;}}e = q -> data;return OK;}</span>


The code will not be explained. Let's take a look at it!





Twelve basic operations:


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

==== Split line. The above is the key and difficult point of this section. The basic operations are very simple. If you are interested, let's take a look at it. ====

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


① Storage structure of a single-chain table:

<Span style = "font-family: Microsoft YaHei;"> typedef struct LNode {ElemType data; // data field struct Lnode * next; // pointer field} LNode; typedef struct LNode * LinkList; // defines two options for ease of use. </span>




② Construct an empty table

<span style="font-family:Microsoft YaHei;">void InitList(LinkList &L){L = (LinkList)malloc(sizeof(LNode));if(!L)exit(ERROR);L -> next = NULL;} </span>


③ Empty table:

<Span style = "font-family: Microsoft YaHei;"> void ClearList (LinkList & L) {LinkList p = L-> next; L-> next = NULL; // then, the while (p) {q = L-> next node except the header node is destroyed; // release the first element node free (L ); // move to the current first metanode L = q ;}</span>


④ Determine whether the table is empty

There are two scenarios:

Header node: L-> next = NULL. The table is empty.

Headless node: L = NULL. In this case, L is the header pointer.


<Span style = "font-family: Microsoft YaHei;"> Status ListEmpty (LinkList L) {// you only need to judge whether the pointer field of the header node is null. if (L-> next) return False; else return TRUE ;}</span>



⑤ Destroy a single-chain table

<Span style = "font-family: Microsoft YaHei;"> void Destory (LinkList & L) {LinkList q; while (L) {// delete all nodes except the header node // point to the first node, not the header node q = L-> next; free (L); // after deletion, redirect to the first dollar L = q ;}</span>


6. Obtain the table length:
<Span style = "font-family: Microsoft YaHei;"> int ListLength (LinkList L) {int I = 0; LinkList p = L-> next; // point to the first metanode, non-header node while (p) {I ++; p = p-> next;} return I ;}</span>


7. Obtain the value of the I element in the table.

<Span style = "font-family: Microsoft YaHei;"> Status GetElem (LinkList L, int I, ElemType & e) {int j = 1; // points to the first dollar, then move the values one by one // if the end is reached or the j value is greater than I, it means that I is invalid LinkList p = L-> next; while (p & j <I) {j ++; p = p-> next;} if (! P | j> I) return ERROR; e = p-> data; return OK ;}</span>


Locate

<span style="font-family:Microsoft YaHei;">int LocateElem(LinkList L,ElemType e,Status(*compare)(ElemType,ElemType)){int i = 0;LinkList p = L -> next;while(p){i++;if(compare(p->data,e))return i;p = p -> next;}return 0;} </span>


Worker returns the direct precursor of a node.

<Span style = "font-family: Microsoft YaHei;"> Status BeforeElem (LinkList L, ElemType choose, ElemType & before) {LinkList q, p = L-> next; while (p-> next) {q = p-> next; if (q-> data = choose) {before = p-> data; return OK ;} // if the successor of p is not choose, return ERROR;} </span>


Worker returns the direct successor of a node.

<span style="font-family:Microsoft YaHei;">Status NextElem(LinkList L,ElemType choose,ElemType &behind){LinkList p = L -> next;while(p -> next){if(p ->data == choose){behind = p -> next -> data;return OK;}p = p -> next;}return ERROR;} </span>


Insert an element to the position I in the table

<Span style = "font-family: Microsoft YaHei;"> Status ListInsert (ListList L, int I, ElemType) {int j = 0; LinkList p, q = L; while (q & j <I-1) {j ++; q = q-> next;} if (! P & j> I-1) return ERROR; p = (LinkList) malloc (sizeof (LNode )); // you must first point the pointer field of the inserted node to the next node of the inserted position. // then, let the pointer field of the inserted node point to the inserted node //!!! The sequence cannot be messy. 1 p-> data = e; p-> next = q-> next; q-> next = p; return OK ;}</span>



⑫ Delete the element at position I in the table

<Span style = "font-family: Microsoft YaHei;"> Status ListDelete (LinkList L, int I, ElemType & e) {int j = 0; LinkList p, q = L; while (q-> next & j <I-1) {j ++; q = q-> next;} // judge whether the input position is reasonable if (! Q-> next | j> I-1) return ERROR; // point to the node to be deleted p = q-> next; // The pointer field used to delete the node points to the next q-> next = p-> next; e = p-> data; // release the node to be deleted free (p); return OK ;}</span>




Lists All elements in a single-chain table.

<span style="font-family:Microsoft YaHei;">void ListTraverser(LinkList L,void(*visit)(ElemType)){LinkList p = L -> next;while(p){visit(p -> data);p = p -> next;}printf("\n");}</span>



Download related code:

Click to download











What are the three aspects of Data Structure Research? What are their connections and differences?

The logical structure of the data, the storage structure of the data, and the calculation of the data.
The three are the main lines throughout the data structure and complement each other.
Logical Structure refers to the logical relationship between data;
The storage structure is the ing from the logical structure of the index data to the computer memory;
Computation refers to processing and processing data elements.

What are the three elements of the data structure?

(39. The three elements of a data model are data structure, data operations, and ___ (40 )___. The primary goal of establishing a database system is to reduce data redundancy, improve data independence, and focus on ___ (41 )___.
(39) A. File System B. compilation system C. Application System D. Database Management System
(40) A. Data Security B. Data compatibility C. Data constraints D. Data Maintenance
(41) A. Data operability B. Data compatibility C. Data Integrity D. Data maintainability

Answer D C

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.