Simple implementation tutorial of one-way linked list in C language and C Language

Source: Internet
Author: User

Simple implementation tutorial of one-way linked list in C language and C Language
Simple implementation of a C-language one-way linked list

# Include
 
  
# Include
  
   
// Define a struct representing the linked list node struct node {int data; // the data stored in the node struct node * next; // pointer to the next node }; // For the convenience of providing a new node type name Lnode typedef struct node Lnode; int main () {// define three pointers 1. head: Header pointer; 2.P: obtains the new memory space each time through this pointer; // 3. p1: Used as the memory connection node; Lnode * head, * p1, * p; // gets the header node. At this time, the three pointers point to the same memory space head = (Lnode *) malloc (sizeof (Lnode); p = head; p1 = head; // enter an integer and save it to scanf ("% d", & p-> data) in the header node data ); // set to exit the loop when the data field data is 0, that is, when the user inputs 0, and the linked list is established and ended while (p-> data! = 0) {// get new memory space p = (Lnode *) malloc (sizeof (Lnode); // connection node p1-> next = p; // use p1 to trace the current last node p1 = p; // store the node data scanf ("% d", & p-> data );}; // point the pointer variable at the end node to NULL p-> next = NULL; // Point p1 to the header node for the traversal Link Table p1 = head; // The traversal chain table while (p1-> next! = NULL) {printf ("% d \ n", p1-> data); p1 = p1-> next;}; return 0 ;}
  
 

Compile and run the code, and enter1, 2, 3, 4, 5, 0The running result is as follows:

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.