Create a linked list instance

Source: Internet
Author: User

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;"             > #include <stdio.h> #include <stdlib.h>/* provide malloc () prototype */#include <string.h>    Provides strcpy prototype # tsize 45struct film{Char title[tsize];    int rating;             struct film * NEXT;    The next structure pointing to the list};int main (void) {struct film * head = NULL;    struct film * prev, * current;    Char input[tsize];//collects and stores information puts ("Enter first movie title:");        while (gets (input) = NULL && input[0]! = ' + ') {current = (struct film *) malloc (sizeof (struct film));        if (head = = NULL) head = current;        else Prev->next = current;        strcpy (Current->title,input);        Puts ("Enter your Rating<0-10>:");        scanf ("%d", ¤t->rating);        while (GetChar ()! = ' \ n ') continue;        Puts ("Enter next movie title (empty line to stop):");    prev = current;    } if (head = = NULL) {printf ("No Data entered!"); } ElSe printf ("Here is the movie list:\n");    current = head;        while (current = NULL) {printf ("Movie:%s Rating:%d\n", current->title,current->rating);    Current = current->next; } printf ("bye!!    \ n "); return 0;} </span>
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ></span>

There are three steps to creating a linked list:

1. Use the malloc () function to allocate enough space for a structure

2. Store the address of this structure

3. Copy the correct information into this structure


Create a linked list instance

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.