C-Language list-Create a linked list and assign values from the keyboard __c language

Source: Internet
Author: User
The list is C language learning more difficult part of the content, the following example shows the creation of a linked list and from the input to assign value, I hope to be useful to everyone.
#include <stdio.h>
#include <stdlib.h>

struct link
{
    int num;
    struct link *next;
int main ()
{
    int n,i;
    struct link *head,*p;
    printf ("How many numbers does you want to input:");
    scanf ("%d", &n);
    head= (struct link*) malloc (sizeof link);//Create Header node
    struct (p= struct) link* (malloc (sizeof link));/apply for the first node
    head->next=p;//chain Joint node and first node
    printf ("Please input numbers:\n");
    for (i=0;i<n;i++)
    {
        scanf ("%d", &p->num);
        p->next= (struct link*) malloc (sizeof link);/Request next node
        struct connect the node to the next node
    p=p->next;// head->next;//because the first loop has moved the list to the end, this is where you move the list to the first node to start printing for
    (i=0;i<n;i++)
    {
        printf ("%d", P->num) ;
        p=p->next;
    }
    return 0;
}

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.