C ++ 2nd week (spring) Project 6 Dynamic Linked List initial experience

Source: Internet
Author: User

Course home in: http://blog.csdn.net/sxhelijian/article/details/11890759


This article is a set of exercises designed for students who have just learned programming languages to experience dynamic linked lists. Dynamic Linked lists are widely used and must be paid attention to. It is of great significance to have an experience before learning data structures and algorithms.

Do not be confused by seemingly complex pointer operations. This is the basic thinking skills that professional students should possess. When it comes to how to establish a link, draw a picture on the paper and think about it. The truth is okay.

As a practice that requires brain work, the visual thinking of drawing a picture will form a logical thinking that is not needed or clear in the mind. This is a necessary process for improving the ability.

If you can't do it, read it several times. It is a great pleasure to make it happen.

The following is the question:

The following is a program for creating a dynamic linked list. Read the program and draw a chain table on the draft to learn how to create a chain table. Then, rebuild the program to fulfill the requirements of project 6.

# Include
 
  
Using namespace std; struct Node {int data; // the data of the Node struct Node * next; // point to the next Node}; Node * head = NULL; // define the linked list header as a global variable to facilitate subsequent operations on void make_list (); // create the linked list void out_list (); // output the linked list int main () {make_list (); out_list (); return 0;} void make_list () {int n; Node * p; cout <"Enter positive numbers (end with 0 or a negative number) create a linked list: "cin> n; while (n> 0) // enter positive numbers to create a linked list. When a positive number is input, the creation process ends {p = new Node; // create a node p-> data = n; p-> next = head; // the newly created node points to the head of the original linked list = p; // assign the value of the linked list header to the new node. In this way, the new node is always the Link List header cin> n; // enter the next number and prepare to create the next node} return;} void out_list () {Node * p = head; cout <"the data in the linked list is:" <
  
   
Data <"; p = p-> next;} cout <
   
    
IDCjrL2owaK1xMG0se3OqqO6PC9wPjxwPqGhoaGhoaGhoaE8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140309/20140309090952382.jpg" alt = "\"/>

---- Reference ---- <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD48L3A + PHByZSBjbGFzcz0 = "brush: java;"> # include # Include Using namespace std; struct Node {int data; struct Node * next;}; Node * head = NULL; void make_list2 (); // The new Node is always at the end of the linked list void out_list (); int main () {freopen ("input.txt", "r", stdin); make_list2 (); out_list (); return 0;} void make_list2 () {int n; node * p, * q; // p is used to point to the newly created Node. q points to the cout at the end of the linked list <"enter a number of positive numbers (ended with 0 or a negative number) to create a linked list: "< > N; while (n> 0) {p = new Node; p-> data = n; p-> next = NULL; if (head = NULL) head = p; else q-> next = p; q = p; cin> n;} return;} // output all nodes void out_list () {Node * p = head; cout <"the data in the linked list is:" < Data <"; p = p-> next;} cout <
(2) Compile the void search (int x) function to check whether the output chain table has a node with the value of x.

---- Reference ----

# Include
     
      
# Include
      
       
Using namespace std; struct Node {int data; struct Node * next;}; Node * head = NULL; void make_list2 (); // The new Node is always at the end of the linked list void out_list (); void search (int x); // you can check whether the node int main () {int x; freopen ("input.txt", "r", stdin) has a value of x ); // redirection is used in debugging to facilitate make_list2 (); out_list (); cin> x; // enter the number search (x) in a linked list in the test; cin> x; // Enter search (x); return 0;} void make_list2 () {int n; Node * p, * q; // p is used to point to the newly created node, and q points to the cout at the end of the linked list <"enter a number of positive numbers (ending with 0 or a negative number) to create a linked list:" <
       
        
> N; while (n> 0) {p = new Node; p-> data = n; p-> next = NULL; if (head = NULL) head = p; else q-> next = p; q = p; cin> n;} return;} // output all nodes void out_list () {Node * p = head; cout <"the data in the linked list is:" <
        
         
Data <"; p = p-> next;} cout <
         
          
Data! = X) {p = p-> next;} if (p! = NULL) // exit the previous loop because p-> data = x cout <"the value in the linked list is" <
          
           
(3) Compile the delete_first_node () function to delete the first node in the linked list.

---- Reference ----

# Include
            
             
# Include
             
              
Using namespace std; struct Node {int data; struct Node * next;}; Node * head = NULL; void make_list2 (); // The new Node is always at the end of the linked list void out_list (); void delete_first_node (); // Delete the first node int main () {freopen ("input.txt", "r", stdin); // use redirection in debugging to facilitate make_list2 (); out_list (); delete_first_node (); out_list (); return 0;} void make_list2 () {int n; Node * p, * q; // p is used to point to the newly created node, and q points to the cout at the end of the linked list <"enter a number of positive numbers (ending with 0 or a negative number) to create a linked list:" <
              
               
> N; while (n> 0) {p = new Node; p-> data = n; p-> next = NULL; if (head = NULL) head = p; else q-> next = p; q = p; cin> n;} return;} // output all nodes void out_list () {Node * p = head; cout <"the data in the linked list is:" <
               
                
Data <"; p = p-> next;} cout <
                
                 
Next; delete p; cout <"the first node is deleted." <
                 
                  
(4) Compile the delete_node (int x) function to delete the node with the node value of x.

---- Reference ----

# Include
                   
                    
# Include
                    
                     
Using namespace std; struct Node {int data; struct Node * next;}; Node * head = NULL; void make_list2 (); // The new Node is always at the end of the linked list void out_list (); void delete_node (int x); // Delete the first node int main () {freopen ("input.txt", "r", stdin ); // redirection is used in debugging to facilitate make_list2 (); out_list (); delete_node (3); out_list (); return 0;} void make_list2 () {int n; Node * p, * q; // p is used to point to the newly created node. q points to the cout at the end of the linked list <"enter a number of positive numbers (ending with 0 or a negative number) to create a linked list:" <
                     
                      
> N; while (n> 0) {p = new Node; p-> data = n; p-> next = NULL; if (head = NULL) head = p; else q-> next = p; q = p; cin> n;} return;} // output all nodes void out_list () {Node * p = head; cout <"the data in the linked list is:" <
                      
                       
Data <"; p = p-> next;} cout <
                       
                        
Data = x) {p = head; head = head-> next; delete p;} if (head! = NULL) {p = head; q = p-> next; while (q! = NULL) {if (q-> data = x) // q is the deleted node {p-> next = q-> next; delete q ;} else // q should not be deleted. Continue to test next {p = q;} q = p-> next; // always next node of p }}} return ;}
                       
                      
                     
                    
                   

For full testing, the program runs five times and the test input data is:

5 2 9 9 7 11 3 0

3 5 2 9 9 7 11 0

3 5 2 9 3 9 7 11 0

3 5 2 9 4 9 7 11 0

3 3 3 3 3 3 0


(5) Compile the make_list3 () function to create an ordered linked list, so that the data in the node is displayed in ascending order when the linked list is created. If the input is 3 5 2 9 4 7 0, the linked list is:

    

---- Reference ----

# Include
                   
                    
# Include
                    
                     
Using namespace std; struct Node {int data; struct Node * next;}; Node * head = NULL; void make_list3 (); // create an ordered linked list, each node ranges from small to large void out_list (); int main () {freopen ("input.txt", "r", stdin); // use redirection in debugging to facilitate make_list3 (); out_list (); return 0;} void make_list3 () {int n; Node * t, * p, * q; // p is used to point to the new Node, q points to cout at the end of the linked list <"enter a number of positive numbers (ended with 0 or a negative number) to create a linked list:" <
                     
                      
> N; while (n> 0) {t = new Node; t-> data = n; t-> next = NULL; if (head = NULL) // It is an empty linked list, and p can be head = t as the first node; else // after inserting p nodes, each node should be in order {if (n <= head-> data) // The newly added node should start with {t-> next = head; head = t;} // after finding a proper position, insert the node // at this time, the linked list already has at least one node, and the insertion node is not the first node else {p = head; q = p-> next; // p is adjacent to q, p is closer to q, the insert position will be between p and q while (q! = NULL & n> q-> data) // If the linked list is not complete and the p node is smaller than n, find {p = q; q = p-> next ;} if (q = NULL) // if q is null, insert it directly to p as the last node. {p-> next = t ;} insert else // t between p and q {t-> next = q; p-> next = t ;}}} cin >> n ;}return ;} // output all nodes void out_list () {Node * p = head; cout <"the data in the linked list is:" <
                      
                       
Data <"; p = p-> next;} cout <
                       
                        
(6) Compile the void insert (int x) function to insert the node with the value of x to the ordered linked list created by make_list3.

---- Reference 1 ----

# Include
                         
                          
# Include
                          
                           
Using namespace std; struct Node {int data; struct Node * next;}; Node * head = NULL; void make_list3 (); // create an ordered linked list, each node ranges from small to large void out_list (); void insert (int x); // inserts the node with the value of x into the ordered linked list so that it is still ordered int main () {freopen ("input.txt", "r", stdin); // use redirection in debugging to facilitate make_list3 (); out_list (); insert (15); out_list (); return 0;} void make_list3 () {int n; Node * t, * p, * q; // p is used to point to the new Node, q points to cout at the end of the linked list <"enter a number of positive numbers (ended with 0 or a negative number) to create a linked list:" <
                           
                            
> N; while (n> 0) {t = new Node; t-> data = n; t-> next = NULL; if (head = NULL) // It is an empty linked list, and p can be head = t as the first node; else // after inserting p nodes, each node should be in order {if (n <= head-> data) // The newly added node should start with {t-> next = head; head = t;} // after finding a proper position, insert the node // at this time, the linked list already has at least one node, and the insertion node is not the first node else {p = head; q = p-> next; // p is adjacent to q, p is closer to q, the insert position will be between p and q while (q! = NULL & n> q-> data) // If the linked list is not complete and the p node is smaller than n, find {p = q; q = p-> next ;} if (q = NULL) // if q is null, insert it directly to p as the last node. {p-> next = t ;} insert else // t between p and q {t-> next = q; p-> next = t ;}}} cin >> n ;}return ;} void insert (int x) // insert the Node with the value of x into the ordered linked list, so that the Node is still ordered {Node * t, * p, * q; // p is used to point to the newly created Node. q points to the end of the chain table t = new Node; t-> data = x; t-> next = NULL; if (head = NULL) // It is an empty linked list, and p can be head = t as the first node; else // after inserting p nodes, each node should be in order {if (x <= head-> data) // The newly added node should be the first node {t -> Next = head; head = t;} // after finding a proper position, insert the node. // at this time, the linked list already contains at least one node, the insertion node is not the first node else {p = head; q = p-> next; // p is adjacent to q, and p is closer to q, the insert position will be between p and q while (q! = NULL & x> q-> data) // If the linked list is not complete and the p node is smaller than n, find {p = q; q = p-> next ;} if (q = NULL) // if q is null, insert it directly to p as the last node. {p-> next = t ;} insert else // t between p and q {t-> next = q; p-> next = t ;}} return ;} // output all nodes void out_list () {Node * p = head; cout <"the data in the linked list is:" <
                            
                             
Data <"; p = p-> next;} cout <
                             
                              
---- Reference 2 ----

In fact, makelist3 () can directly call the insert (int x) Implementation of the program as a whole, so that the program written is in line with the engineering principles. This inspired us that using functions to organize program structures should become a kind of consciousness.

# Include
                               
                                
# Include
                                
                                 
Using namespace std; struct Node {int data; struct Node * next;}; Node * head = NULL; void make_list3 (); // create an ordered linked list, each node ranges from small to large void out_list (); void insert (int x); // inserts the node with the value of x into the ordered linked list so that it is still ordered int main () {freopen ("input.txt", "r", stdin); // use redirection in debugging to facilitate make_list3 (); out_list (); insert (15); out_list (); return 0;} void make_list3 () {int n; cout <"Enter positive numbers (end with 0 or a negative number) to create a linked list:" <
                                 
                                  
> N; while (n> 0) {insert (n); // calling insert, makelist is incredibly simple. cin> n;} return;} void insert (int x) // Insert the Node with the value of x into the ordered linked list so that the Node is still ordered {Node * t, * p, * q; // p points to the new Node, q points to t = new Node at the end of the linked list; t-> data = x; t-> next = NULL; if (head = NULL) // empty linked list, p can be head = t as the first node; else // After the p node is inserted, each node should be in order {if (x <= head-> data) // The newly added node should start with {t-> next = head; head = t;} // after finding a proper position, insert the node // at this time, the linked list already has at least one node, and the insertion node is not the first node else {p = head; q = p-> next; // p is adjacent to q, and p is closer Q: The insert position is between p and q while (q! = NULL & x> q-> data) // If the linked list is not complete and the p node is smaller than n, find {p = q; q = p-> next ;} if (q = NULL) // if q is null, insert it directly to p as the last node. {p-> next = t ;} insert else // t between p and q {t-> next = q; p-> next = t ;}} return ;} // output all nodes void out_list () {Node * p = head; cout <"the data in the linked list is:" <
                                  
                                   
Data <"; p = p-> next;} cout <
                                   
                                    




=============================== Author he Lijian CSDN blog column ==================== ====

| = IT students growth guide column article category directory (occasionally updated) = |

| = C ++ online class column he Lijian Course Teaching Link (by course grade) = |

=============Pave the runway for IT cainiao to take off and enjoy the joy and passion of college with students ========



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.