C language-single-chain table (1), single-chain table in C Language

Source: Internet
Author: User

C language-single-chain table (1), single-chain table in C Language

Define nodes:

typedef struct Node {int data;Node* pNext;}NODE, *PNODE;

For details, PNode represents the struct Node *. The above form can also be written in the following form with the same meaning.

typedef struct Node {int data;Node* pNext;}*PNODE,NODE ;

  

Algorithm operations

1. Create a linked list

// 1. initialize the linked list PNODE create_list (void) {int len, val; printf ("% s", "Enter the length of the linked list \ n "); scanf ("% d", & len); PNODE pHead = (PNODE) malloc (sizeof (PNODE); // create a header node PNODE pTail = pHead; // always point to the last pTail-> pNext = NULL; for (int I = 0; I <len; I ++) {printf ("Enter the value of variable % d", I); scanf ("% d", & val); PNODE p = (PNODE) malloc (sizeof (PNODE); if (NULL = p) {printf ("memory allocation failed");} p-> data = val; p-> pNext = NULL; pTail-> pNext = p; // The End Node points to Ptail = p; // pTail is the end node} return pHead ;}

2. Display linked list data

// 2. Output void show_list (PNODE pHead) {PNODE p = pHead-> pNext; // The first Node Address while (p! = NULL) {printf ("% d \ n", p-> data); p = p-> pNext;} printf ("\ n"); return ;}

3. Run the test

Int main () {PNODE pHead = NULL; // represents Struct Node * pHead = NULL; pHead = create_list (); // show_list (pHead); return 0 ;}

  

 

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.