Linked List (2)-basic operations on one-way linked list (create, delete, print, node count statistics)

Source: Internet
Author: User
1. pointer Interaction
Two pointers are directed to the frontend and the successor nodes respectively, and are moved on the one-way linked list. When the Pointer Points to the knots to be processed, the frontend of the node also points.
2. an unordered one-way linked list is created, and the values of the data fields are different. the pointer pmin points to the minimum node and the pointer Prem points to the front node of the minimum node:
Code snippet:
For (P = head; P; q = P, P = p-> next)
{
If (pmin-> DATA> P-> data)
{
Pmin = P;
Prem = Q;
}
}
3. One-way linked list deletion algorithm
Note: The Node units allocated using the malloc function must be released using the free function. After free (p), the units pointed to by P are released, and P is assigned a random value by the system, P can only be automatically cleared after the program runs successfully.
Delete the header node: Head = head-> next; free (pdel );
Delete non-header nodes: PPRE-> next = pdel-> next; free (pdel );
4. Example
Note: The most basic operation of a one-way linked list is to create a linked list, delete an element, print the linked list, count the number of linked lists, and delete the linked list.

# Include <stdio. h> # include <malloc. h> # define null0typedef struct node {int data; struct node * Next;} elemsn; elemsn * creat_link (int ms ); // reverse create a linked list void print_link (elemsn * head); // output the one-way linked list elemsn * delete_node (elemsn * head, int X ); // delete a node int count_link (elemsn * head) in the linked list; // calculate the number of nodes in the one-way linked list. elemsn * clear_link (elemsn * head); // Delete the int main () in the linked list () {elemsn * head; int MS, X; printf ("Please input node number:"); scanf ("% D ", & MS); Head = creat_link (MS); print_link (head); printf (" Please input delete node: "); scanf (" % d ", & X); Head = delete_node (Head, x); print_link (head); printf ("link Member is: % d \ n", count_link (head )); head = clear_link (head);} elemsn * creat_link (int ms) {elemsn * H = NULL, * P; int I, X; for (I = 0; I <MS; I ++) {printf ("Please input data:"); scanf ("% d", & X); P = (elemsn *) malloc (sizeof (elemsn )); p-> DATA = x; P-> Ne XT = H; H = P;} return h;} void print_link (elemsn * head) {for (; head = head-> next) {printf ("% d", head-> data);} printf ("\ n");} elemsn * delete_node (elemsn * head, int X) {elemsn * P = NULL, * q = NULL; If (null = head) {return NULL;} For (P = head; P & P-> data! = X; q = P, P = p-> next); // P & P-> data! = X cannot switch location if (null = P) // The Node {return head;} If (null = q) to be deleted is not found) // The header node {head = head-> next;} else {q-> next = p-> next;} Free (p); return head;} to be deleted ;} int count_link (elemsn * head) {int COUNT = 0; For (; head; count ++, head = head-> next); Return count ;} elemsn * clear_link (elemsn * head) {elemsn * P; while (head) {P = head-> next; free (head); Head = P;} return head ;}

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.