Spam code assessment-about "C program design partner" 9.4-linked list (2)

Source: Internet
Author: User

Link: http://www.cnblogs.com/pmer/archive/2012/11/21/2781703.html

[Reconstruction]

First, what is a linked list? No clear and accurate definitions are provided either in C programming or C programming partner.
I don't know what a linked list is. Of course, it's impossible to write the code about the linked list correctly. In "C program design partner", a nondescribedomaintail (tail) is installed for the linked list) the strange phenomenon is also strange.
Here I give my definition of the linked list: the linked list is a pointer, which either has a null value or points to data containing another linked list.
Of course, there are many types of linked lists. The definition here is only the simplest one-way linked list.
This definition obviously imitates n! Is a recursive definition. This recursive definition can be easily implemented using recursive methods.
Second, the description of the linked list node.

typedef    struct   {      char name[20];      float score;   }data_t;typedef    struct node   {      data_t item ;      struct node *next ;   }node_t;

 

In this way, an abstract node type is created. The advantage of this processing is that the data is successfully removed from the node in form, which is very important for the structure and maintainability of the Code. We know that a good code structure cannot be established in a bad data structure, and a bad code structure will definitely lead to a reduction in maintainability.
If you select a linked list, you choose the advantages of the linked list instead of the disadvantages of the linked list. This is the same as buying a car for driving rather than pushing it. Compared with arrays of the same linear structure, the advantage of a linked list is that it is easy to add node nodes in front, but adding nodes in the end is much more troublesome. Array, on the contrary, even if there is enough reserved space, it is difficult to add a data at the beginning of the array without any effort, however, it is easy to add a data at the end of the array (as long as the location is reserved in advance ).
Since it is easy to add nodes to the head of a linked list, you should also choose this method to create a linked list-add data to it continuously. The Code is as follows:

1. # include <stdlib. h> 2. # include <stdio. h> 3. 4. // ---------- general functions --------------------- // 5. void * my_malloc (size_t); 6. // ---------------------------------------- // 7. 8. 9. // ---------- "data" type ---------------- // 10. typedef 11. struct12. {13. char name [20]; 14. float score; 15 .} 16. data_t; 17. // ----------- function about "data" ----------- // 18. int input (data_t *); 19. void output (data_t *); 20. //------------ --------------------------- // 21. 22. 23. // --------- "Node" type ------------------ // 24. typedef 25. struct node26. {27. data_t item; 28. struct node * Next; 29 .} 30. node_t; 31. // -------- "linked list" operation function --------------- // 32. void create (node_t **); 33. void insert (node_t **, node_t *); 34. void print (node_t *); 35. // ---------------------------------------- // 36. 37. 38. int main (void) 39. {40. node_t * head = NULL; // empty linked list 41. 42. puts ("Create a Linked List"); 43. create (& head); 44. // Test 45. puts ("output Linked List"); 46. print (head); 47. 48. return 0; 49 .} 50. 51. // ------- General function definition ----------------- // 52. void * my_malloc (size_t size) 53. {54. void * P = malloc (size); 55. if (P = NULL) 56. {57. puts ("insufficient memory"); 58. exit (1); 59 .} 60. return P; 61 .} 62. // ---------------------------------------- // 63. 64. // --------- "data" Operation Function Definition --------- // 65. Int input (data_t * p_data) 66. {67. puts ("enter your name and score:"); 68. if (scanf ("% 20 S % F", p_data-> name, & p_data-> score )! = 2) // 20 very important 69. {70. While (getchar ())! = '\ N') // clear the input cache 71.; 72. Return 0; 73.} 74. Return! 0; 75 .} 76. 77. void output (data_t * p_data) 78. {79. printf ("Name: % S score: %. 2f \ n ", p_data-> name, p_data-> score); 80 .} 81. // ---------------------------------------- // 82. 83. 84. // ---------- "linked list" operation function definition -------- // 85. 86. void print (node_t * p_node) 87. {88. if (p_node! = NULL) 89. {90. output (& p_node-> item); 91. print (p_node-> next); 92 .} 93 .} 94. 95. void insert (node_t ** p_next, node_t * p_node) 96. {97. p_node-> next = * p_next; 98. * p_next = p_node; 99 .} 100. 101. void create (node_t ** p_next) 102. {103. data_t data; 104. 105. if (input (& Data )! = 0) 106. {107. node_t * p_node = my_malloc (sizeof (* p_node); 108. p_node-> item = data; 109. insert (p_next, p_node); // insert node 110. create (p_next); // continue 111 .} 112 .} 113. //--------------------------------------//

 

Because "data" is regarded as an abstract member of a node, operations on the "data" part are successfully separated from operations on the linked list like oil and water. In the development process, we can develop them as two relatively independent modules (High Cohesion and low coupling "). In addition, even if the "data" part is modified in the future, as long as the interface is not changed, the linked list part is completely unaffected.

Some people may still want the newly added nodes to be connected to the end of the linked list. Because the CREATE () function creates a linked list from scratch at a time, so it is not difficult to implement this, or even very simple. You just need to define

Create (p_next); // continue

Change

Create (& (* p_next)-> next); // create (p_next); // continue

 

You can.
That is to say, for a one-time linked list, there is no need for something like tail.

However, in actual development, it is not always possible to encounter problems that require creating a linked list at a time. If the problem requires dynamic (not one-time) add a node from the end of the linked list and delete the node from the front? At this time, you need to add a tail that records the tail node on the basis of the head. But even so, we should not "start and end", but "end and end together ".

typedef  struct  {    node_t *head;    node_t *tail; } queue_t  ;

 

Okay.
This kind of stuff is not called a linked list, but a queue ).

 

Related Article

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.