Source code for creating a linked list

Source: Internet
Author: User

 

/* = ========================= */
/* Program Name: createlist. C */
/* Program Objective: To design a program that creates input data into a linked list */
/* Written by Kuo-Yu Huang. (want studio .)*/
/* Input: # go to dinner tomorrow */
/* = ========================= */

# Include <stdio. h>
# Include <malloc. h>
# Define max 10

Struct list/* node Structure Declaration */
{
Int number;
Char name [Max];
Struct list * next;
};
Typedef struct list node;
Typedef node * link;

/* Release the linked list */
Void free_list (link head)
{
Link pointer;/* node Declaration */
While (Head! = NULL)/* When the node is null, end the loop */
{
Pointer = head;
Head = head-> next;/* point to the next node */
Free (pointer );
}
}

/* Output linked list */
Void print_list (link head)
{
Link pointer;/* node Declaration */
Pointer = head;/* set the pointer to the first node */
While (pointer! = NULL)/* When the node is null, the end loop */
{
Printf ("# input data ##/ N ");
Printf ("Data number: % d/N", pointer-> number );
Printf ("Data name: % s/n", pointer-> name );
Pointer = pointer-> next;/* point to the next node */
}
}

/* Create a linked list */
link create_list (link head)
{< br> int datanum; /* Data ID */
char dataname [Max];/* Data name */
link new;/* node Declaration */
link pointer; /* node Declaration */
int I;
head = (Link) malloc (sizeof (node )); /* allocate memory */
If (Head = NULL)
printf ("memory allocate failure! /N ");/* Memory Allocation failure */
else
{
datanum = 1; /* Initial Data number */
printf ("Please input the data name:");
scanf ("% s", dataname );
head-> Number = datanum;/* define the first node data number */
for (I = 0; I <= max; I ++)
head-> name [I] = dataname [I];/* assign the value of dataname to name */
head-> next = NULL;
pointer = head;/* set the pointer to the first node */

while (1)
{< br> datanum ++;/* increasing data numbers */
New = (Link) malloc (sizeof (node);/* allocate memory */
printf ("Please input the data name:");
scanf ("% s ", dataname);
If (dataname [0] = '0')/* enter 0 to end */
break;
New-> Number = datanum;
for (I = 0; I {< br> New-> name [I] = dataname [I];
}< br> New-> next = NULL;
pointer-> next = new;/* connect the new node to the end of the original list */
pointer = new; /* The End Node of the list is a new node */
}< BR >}< br> return head;
}

/* Main Program */
void main ()
{
link head = NULL; /* node Declaration */
head = create_list (head);/* call the create linked list function */
If (Head! = NULL)
{< br> print_list (head);/* call the output linked list data function */
free_list (head ); /* call the release linked list function */
}< BR >}

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.