Tail insertion and various operations of a single-chain table at the leading node and no-leading Node

Source: Internet
Author: User

 

1. Single-chain table of the lead Node

Let's first talk about the linked list created by create (), which adopts the cycle method.

/*************************************** * The single-chain table of the leading node 13-1-1ex3.cpp1. create a single-chain table 2. length of the linked list. insert an element at position I (method) 4. because the program takes the lead in deleting the node with the specified serial number, the head of the deletable () and insertlist () functions has not been changed. Therefore, in the main function, after del and insertlist are used, the head is not changed. In printlist, the head can be passed in. If so, the two functions can be fully defined as the void type, instead of defining create () as node *, and then in main () define a pointer head in to convert head = create (); this head will not change in the future. It seems that the general operations on the linked list of the lead node do not change the head node. **************************************** * *********************/# Include <stdio. h> # include <stdlib. h> // # include <string. h> // # include <conio. h> typedef struct student {int data; struct student * Next; // struct student * pre;} node; int getlength (node * head) {int n = 0; node * P; P = head-> next; while (P! = NULL) {P = p-> next; n ++;} return N;} node * insertlist (node * head, int X, int I) // insert element method 1 {node * P, * q, * s; Int J = 1; P = head; if (I <1 | I> getlength (head) + 1) {exit (1);} s = (node *) malloc (sizeof (node); s-> DATA = x; while (j <= I) {q = P; P = p-> next; j ++;} s-> next = Q-> next; q-> next = s; return head ;} void insertlist2 (node * head, int X, int I) // insert element method 2 {node * P, * s; Int J = 1; P = head; if (I <1 | I> getlength (Head) + 1) // This part is more than the delete node function, because it can be inserted to the next node of the last node. However, you cannot delete the node function. You can only delete it to the last node {exit (1);} s = (node *) malloc (sizeof (node); S-> DATA = X; while (j <I) {P = p-> next; j ++;} s-> next = p-> next; P-> next = s; // return head;} node * del (node * head, int I) {node * P, * q; Int J = 1; P = head; if (I <1 | I> getlength (head) {exit (1) ;}while (j <I) {P = p-> next; j ++ ;} Q = p-> next; P-> next = Q-> next; free (Q); return head;} node * Create () {node * head, * P, * s; int X, cycle = 1; head = (node *) Malloc (sizeof (node); P = head; // replace it with the linked list of the leading node. It cannot be changed here either. Remember, it cannot be changed to P = head-> next; then printf ("\ nplease input the data: \ n"); While (cycle) {// printf ("\ nplease input the data: \ n"); scanf ("% d", & X); If (X! = 0) {S = (node *) malloc (sizeof (node); s-> DATA = x; // printf ("\ n % d ", s-> data); P-> next = s; // s-> pre = P; P = s;} else {cycle = 0 ;}} p-> next = NULL; // head = head-> next; // note that this statement is required for a single-chain table that does not take the lead node, because it follows the algorithm. The first number is actually assigned to head-> next // printf ("\ nhead-> data: % d \ n", head-> data); return head ;} void printlist (node * head) // print all elements of the linked list {node * P; P = head-> next; // 1) while (P! = NULL) {printf ("% 3d", p-> data); P = p-> next;} printf ("\ n");} int main () {node * P, * q, * t, * head; head = create (); // P = p-> next; // if a single-chain table of the leading node is created, in addition to the step at the end of the above function, this step is added to ensure that the linked table data is output from the next node of the head. Printf ("\ Nnow output the linklist: \ n"); printlist (head); insertlist2 (Head, 99,3); // q = Q-> next; printf ("\ Nnow output the new linklist after insert: \ n"); printlist (head);/* While (Q! = NULL) {printf ("% 3d", Q-> data); q = Q-> next;} printf ("\ n"); */del (Head, 3); printf ("\ Nnow output the new linklist after Delete: \ n"); printlist (head);/* t = T-> next; while (T! = NULL); {printf ("% 3d", T-> data); t = T-> next;} printf ("\ n "); */system ("pause"); Return 0 ;}

 

The running result is as follows:

1. Enter 1 2 3 4 5 6. Insert 99 at the third position and delete 3rd nodes (0 indicates the end Controller)

2. Enter 1 2. Insert 99 at 3rd locations and delete the third node (0 indicates the end Controller)

 

Let's talk about the linked list created by create2 (), which adopts the traditional getchar () method.

If you use create2 () to create a table, it is also very simple. The create2 () function can be directly added to this program. In addition, you can change the input type and output type in printlist to Char and % C, on the way, the yellow highlighted part is marked to be changed.

Let's take a look at the create2 () code:

Node * create2 () {datatype X; node * head = (node *) malloc (sizeof (node); node * s, * r; r = head; while (x = getchar ())! = '\ N') // note that this is entered by the getchar () character. It must be entered in order, not by space or press enter, otherwise it will be treated as a character {// getchar () and space as a character, which is different from scanf. For example, enter abcdefg and press Enter. Enter a B c d E F G and press Enter. S = (node *) malloc (sizeof (node); s-> DATA = x; r-> next = s; r = s; // R always points to the End Node} r-> next = NULL; return head ;}

 

In this case, both methods are good.

Note that characters are inserted during insertion. Don't forget to add ''. Insertlist2 (Head, 'U', 3 );

Running result:

1. Enter abcdefg

2. Enter 1234567

 

 

2. Single-Chain tables with no leading nodes

Note: The basic operations are the same, but there are some differences.

1. After p-> next = NULL in create (), add head = head-> next; that is, move the head of the header pointer back.

2. In printlist (), no P = head-> next; P = head directly.

3. In insertlist (), we need to discuss the situation in 3:

A) The inserted position is the first node (the node indicated by the head );

B) The inserted position is the intermediate node;

C) The inserted position is the end node.

4. In del (), we need to discuss it in two cases:

A) The inserted position is the first node (the node indicated by the head );

B) The inserted position is another node (intermediate node or tail node ).

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.