[Data Structure "C language" linked list creation and traversal

Source: Internet
Author: User

The first write code of the blog, a new contact with the novice, to come here mainly to record themselves, convenient for their later browsing, but also welcome correction. Let's start with a simple, dynamic list creation and traversal.

#include <stdio.h>#include<stdlib.h>#include<malloc.h>//Define a node for a linked listtypedefstructlnode{intdata; structLnode *Next;} *linklist;//Create a linked list functionlinklist createlist () {linklist ltail, lhead, p; intI, length,input; Lhead= (linklist) malloc (sizeof(Lnode));//Create a table header node    if(! Lhead) Exit (0);//determine if the table header was created successfullyLtail = Lhead;//Define a footerLtail->next = NULL;//the pointer at the end of the footer assigns nullprintf"Please enter the number of nodes you need: \ n"); scanf_s ("%d", &length);  for(i =0; i < length; i++) {printf ("Please enter data for%d nodes", i +1); scanf_s ("%d", &input); P= (linklist) malloc (sizeof(Lnode));//to create a new node        if(!p) Exit (0); P->data = input;//assigns the input value to the new nodeP->next = NULL;//because the new node is going to be the footer of the list, its pointer is given NULL.Ltail->next = p;//connect the footer to the new nodeLtail = p;//assign a new node to Ltail to become the new footer    }    returnLhead;//return to table header}//traversing linked list functionsvoidtravellist (linklist Head) {linklist P= head->next;//start traversing from the next node of the linked list head node     while(P! = NULL)//stop traversal When the list is empty{printf ("%d\t", p->data); P= p->next;//move one node backwards    }}intMain () {linklist head; Head=createlist ();    Travellist (head); return 0;}

Creation and traversal of the [data structure] "C language" 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.