Linked list (C language implementation)

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>/*free-definition types, similar to C + + templates*//*the head of the list is Head,head meaningless: head->1st node->2nd node->null*/typedefinttype;structnode{type data; Node*next;};/*Create a linked list, an empty head node, back to head*/Node*Create () {Node* head = (node*)malloc(sizeof(Node)); Head->next =NULL; returnhead;}/*determines whether the linked list is empty. Empty table's head->next is null*/intIsEmpty (node*head) {    returnHead->next== NULL;//1 is empty}/*empty the list, but head still accounts for memory*/voidMakeempty (node*head) {Node* p =Head;  while(P! =NULL) {Node* del = p;//p stored underp = p->next;//store the nodes behind P         Free(DEL);//Delete the current p node} head->next = NULL;//important, because IsEmpty is based on->next==null.}/*Inserts an element at the specified position, n=0 before the first element in the head, =1 inserted after the 1th element*/voidInsert (Type Val, node* head,intN) {    /*new Node*/Node* New_node = (node*)malloc(sizeof(Node)); if(New_node = =NULL) {printf ("New_node malloc failed!\n"); return; } New_node->data =Val; New_node->next =NULL; /*Insert*/Node* p =Head;  while(n--) {p= p->Next; if(P->next = =NULL) {printf ("already end!\n");//at this point P is already the last node             Break; }} New_node->next = p->next;//It 's important here! P->next =New_node;}/*delete the specified location node*/voidDelete (node* Head,intN) {    /*if empty, return*/    if(IsEmpty (head)) {printf ("Empty now!"); return; }    Else{Node* p =Head;  while(n--) {p= p->Next; if(P->next = =NULL) {printf ("already end!\n");  Break; }} Node* tmp = p->next;//A linked list cannot be accessed, that is, traversing to P you want to know that the last of P does not work, you have to re-iterate, unless it is a doubly linked list. P->next = tmp->next;//The goal is to remove P->next. The back of the previous one. Delete the middle one.          Free(TMP); }}voidPrint (node*head) {    if(IsEmpty (head)) {printf ("already empty!\n"); }    Else{Node* p = head->Next;  while(P! =NULL) {printf ("%d\n", p->data); P= p->Next; }} printf ("\ n");}intMain () {Node* head=Create (); Insert (1, head,0); Insert (2, head,1); Insert (3, head,2);    Print (head); Insert (4, head,2);    Print (head); Delete (Head,1);    Print (head);    Makeempty (head);    Print (head); //Free (head);    return 0;}

This code, the comments are very complete, I believe I can see after many years to quickly understand the main thrust. But there's a problem, I'm used to setting the power off at return 0, or system ("pause"), first look at the results:

But once the process continues, it hangs:

Have met before, but the lack of experience is not solved. The problem must be on the makeempty.

void Makeempty (node* head) {    Node* p = head->next;      while (P! = NULL)    {        Node* del = p; // p stored under        p = p->next; // Store the Nodes        behind P  Free (DEL); // Delete the current p node     }    head->next = NULL; // important, because IsEmpty is based on->next==null}

Change to the above form.

At the beginning, if the head is assigned to P, the head memory is deleted, and then each node of the list is removed. If you want to delete head, add free (head) in front of return.

But why do I have to empty my head and not hang out, and the Loop del will hang up?

I think it's a matter of recycling mechanism. At main return, the main thread terminates, and the allocated variables are recycled and destroyed in the heap. However, it may be that the head pointer is broken with the actual memory block link, causing it to destroy the error that occurs before such a memory error occurs.

You can try the following:

 void  makeempty (node* head) {Node  * p    = head;   while  (P!= * del = P // p storage under  p = p->next; //         free  (DEL); //  Delete the current P-node    head- >next = NULL;  // } 

This way, as long as the print is not a problem. That is, if there is no head->next, you can. And with this, you release the memory, head does not, and the head of next to hang a null. Return, it thinks your head has meaning and is ready to be destroyed, but its memory has been freed, so repeated releases will go wrong.

I finally wrote and found the root of the problem.

Linked list (C language implementation)

Related Article

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.