Data structure--single linked list

Source: Internet
Author: User

Recently review data structure, strengthen the foundation of their own. In the review of the problems encountered here to take notes.
A single linked list is a chain-access data structure that stores data elements in a linear table with an arbitrary set of storage units of addresses.
Definition of a single linked list:

typedefstruct LNode{    int data;               // data 中存在结点的数据域    struct LNode *next;     // 指向后继结点的指针}LNode;                     // 定义单链表结点类型

The construction method of single-linked list (tail interpolation method and head interpolation method)
(1) Tail interpolation method

voidCreatelistr (Lnode *&c,intA[],intN) {Lnode *s, *r;//s to point to the new application node, R always points to the terminal node of C    intI C = (Lnode *) malloc (sizeof(Lnode *));//apply for C's head node spaceC-Next =NULL; R = C;//r points to the head node of C, because at this point the head node of C is the terminal node of C.     for(i=0; i<n;i++) {s = (Lnode *) malloc (sizeof(Lnode *));//s Point to the new application nodeS--data = a[i]; R-Next = s;//r accepting new nodesR = r, next;The//r points to the terminal node to accommodate the next coming node .} r, Next =NULL;//All elements are loaded in list C, and the pointer field of the terminal node of C is set to null}

(2) Head insertion method

voidintint n){    LNode *s;    int i;    C = (LNode *)malloc(sizeof//申请C的头结点空间    C -> next = NULL;    for (i=0;i<n;i++){        s = (LNode *)malloc(sizeof//s指向新申请结点        s -> data = a[i];         //s 所指向新结点的指针域 next 指向 C 中的开始结点        //头结点的指针域 next 指向 s 结点, 使得 s 结点成为了新的开始结点    }}

Linked list Delete (delete p->next)

nextnextnextnext;free(q);

A list of interpolation (head interpolation and tail interpolation) and deletion is the basic knowledge of all linked lists, many operations are composed of three basic operations.

The following is a list of integrated topics (Leetcode above)
A single-linked list of two non-negative integers (one-digit integers) that stores the values of the corresponding elements in two single-linked lists into a single-linked list

Input: [2, 4, 3]
[5, 6, 4]
Output: [7, 0, 8]

Title Analysis: (according to the title description and input and output instances) the value of each element in the single linked list is added, if less than 10, the resulting value is the desired value, if the added value is greater than 10, then the required value is the number of digits, and the top ten of the 1 to be added to the next operation.
Thought analysis: You can create a single-linked list with a length that is the maximum length in the input list. Because the single-linked list does not provide the length of the property, then only through the variable to get the maximum length, which will increase the burden of running, so when traversing the single-linked list is empty, if a single-linked list is empty, the other is not empty, then the empty single-linked list value can be considered to be 0, This will not affect the final calculation, and the maximum length can be reached in a single traversal. It is important to note that if the last value is greater than 10 after traversing, you need to add a node at the end to save the resulting 10-digit number.
Code implementation (c + +)

#include <iostream>using namespace STD;//Definition for singly-linked list.structListNode {intVal    ListNode *next; ListNode (intx): Val (x), Next (NULL) {}};//Use a template to define a function getarraylen that returns the length of an array of arraysTemplate<classT>intGetarraylen (t&Array)  {return(sizeof(Array) /sizeof(Array[0]));}classSolution { Public:/** * Create a linked list without a head node by using the tail interpolation method.listnode* Createliste (int Array[],intN) {ListNode *r, *s; listnode*List= (ListNode *)malloc(sizeof(ListNode *));List->next = NULL; R =List; for(inti =0; I < n; ++i) {s = (ListNode *)malloc(sizeof(ListNode *)); S--val =Array[i];            R-Next = s;        r = r->next; } r->next = NULL;return List->next; } listnode* addtwonumbers (listnode* L1, listnode* L2) {//list does not contain head nodes.        intFlag =0;//Definition identifier, initialized to 0, list value added greater than 10 to 1, otherwise 0listnode* p = L1;        listnode* q = L2; listnode* L3 = (listnode*)malloc(sizeof(listnode*));//Apply for L3 's head node space (you can create a linked list of lead nodes, then return to L3->next, remove the head node.)L3-next = NULL; listnode* r = L3;//r points to L3 's always last node        intValp, Valq;//p,q The value of the corresponding position of the linked list         while(P! = NULL | | Q! = NULL) {//One is not empty, continue (get maximum length)Valp = (p==null?0:p->val);//P is null, the corresponding value is 0; otherwise, the value of the P-linked listValq = (q==null?0: Q->val); listnode* s = (listnode*)malloc(sizeof(listnode*));//s Point to the new application node            if(Valp + VALQ + Flag <Ten){//Less than 10 of casesS--val = (Valp + VALQ + flag); Flag =0;//Less than 10 set to 0, does not affect the next calculation}Else{//Case greater than 10S--val = (Valp + VALQ + flag)%Ten;//Take a single digit valueFlag =1;//greater than 10 set to 1, next calculation plus 1}//Chain list tail interpolation methodR-Next = s; R = r, next;if(P! = NULL) p = P-Next;If the//p is not empty, move backward one position            if(q = NULL) q = q-Next; }if(Flag = =1){//When the last result is greater than 10listnode* s = (listnode*)malloc(sizeof(listnode*));//re-apply for new node.            //Insert a new node into the L3 linked list using the tail interpolation method .S--val = flag;            R->next = s;        r = r->next; } r, next = NULL;//All elements are loaded into the linked list L3, and the pointer field of the L3 Terminal node is set to null        returnl3->next;//Return the L3 linked list to remove the head node.}};intMainintargcConst Char* argv[]) {//Test Cases    intA[] = {2,4,3};intB[] = {5,6,4};//int a[] = {3};//int b[] = {7,6};////int a[] = {5};//int b[] = {5};////int a[] = {3, 8};//int b[] = {2};    //Calculate the length of the array    intAlength = Getarraylen (a);intBlength = Getarraylen (b); Solution S;//tail interpolation to get a single linked list of headless nodeslistnode* L1 = S.createliste (A, alength); listnode* L2 = S.createliste (b, blength);//Calculation of two single-linked list andlistnode* L3 = s.addtwonumbers (L1, L2); listnode* r = L3; while(R!=null) {cout<<r->val<<",";    r = r->next; }cout<<endl;return 0;}

Data structure--single linked list

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.