Establishment of a single necklace table

Source: Internet
Author: User

The linked list is one of the most basic data structures, and the steps to create a single necklace are as follows

    1. Defining a linked list node
    2. Defines three pointers--the head pointer, the tail pointer, and the current node pointer. and separately request memory, initialize
    3. The judge is not the head pointer, if it is, then the current node is assigned to the head pointer, the trailing pointer is null, if the current is not the head pointer, append the current node after the tail pointer
    4. After the insert operation is completed, update the successor of the tail and tail pointers
    5. Re-apply a piece of memory for the new extension node to use

Define a simple node structure body

1 struct Node 2 {3     int data; 4     struct Node *Next; 5 } Node;

Finally returns the head pointer of the linked list

1Node *Create ()2 {3     intN;4     inti;5Node *head=NULL;6Node *tail;7Node *p;8Tail = (Node *)malloc(sizeof(Node));9p = (Node *)malloc(sizeof(Node));Ten     if(p==NULL) One     { Aprintf"Fail to create a new node ....."); -         returnNULL; -     } the     Else -     { -          for(i=0; i<Ten; i++) -         { +P->data=i; -             if(head==NULL) +             { AHead=p; attail->next=NULL; -             } -             Else -             { -tail->next=p; -             } inTail=p; -tail->next=NULL; top = (Node *)malloc(sizeof(Node)); +         } -     } the     returnhead; *}

Problem

The first time the linked list is written, the value of the tail node is always output each time the linked list is output. Later found that the original is a pointer to the problem.

Is that each time a node is added, not a new request for a piece of unused memory, but continue to use P, and P and head are pointing to the head node (line 22nd), so the use of P and head can change the value of the head node, so each time you change p, it actually changes the value pointed by the head.

Therefore, a new memory should be requested for the new node .

Establishment of a single necklace table

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.