Second understanding single linked list and its reversal (reverse)

Source: Internet
Author: User


What is a linked list, this data structure is made up of a group of node, which together represents a sequence. The list is the most common and simplest data structure, and it is the basis for implementing other data structures such as stack, queue, etc.


A linked list is easier to insert and delete than an array.


Node can be defined as follows:

typedef int ELEMENT_TYPE;TYPEDEF struct node *node_ptr;struct node {element_type element;node_ptr next;};

In addition to the question of the head node, I propose to add the head node for the following reasons:

1. No head node, delete the first node, accidentally lost the list2. There is no intuitive way to insert a head. 3. The usual delete operation, you must first find the previous node, if there is no head node, delete the first node is not the same.


Next focus on the implementation of single-linked list reversal, which is often a problem, the following is the C language implementation:

void List_reverse (list L) {if (L->next = = null) Return;node_ptr p = l->next, first = L->next;while (P! = null & ;& P->next! = NULL) {Node_ptr Next_node = P->next;p->next = Next_node->next;next_node->next = First;fi rst = Next_node;} L->next = First;}

As for the other operations are not listed, you can focus on my github:

Https://github.com/booirror/data-structures-and-algorithm-in-c


Second understanding single linked list and its reversal (reverse)

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.