#include <stdio.h>
#include<string.h>
#include <malloc.h> /*The header file containing the dynamic memory allocation function*/
#define N 10 /*N is the number of people*/
typedef struct node
{
char name [20];
struct node *link; /** link is a pointer of node structure type, so it can be used to save the address of the next node,
such as: the following p->link=s;s is the current allocation The address of the node, p will be given the address of the previous node (eg: p=h;p=s),
so p->link=s, which is the linked list of the previous node (p->link) Save the address of the next node in the (s) */
}stud;
stud * create(int n) /* Function to create a singly linked list, parameter n is the number of people */
{
stud *p,*h,*s; / * *h saves the pointer of the header node, *p points to the previous node of the current node, *s points to the current node*/
int i; /*counter*/
if((h=(stud *)malloc( sizeof(stud)))==NULL) /* allocate space and check */
{
printf("Cannot allocate memory space!");
return 0;
}
h->name[0]='\0'; /* Empty the data field of the header node*/
h->link=NULL; /* Empty the link field of the header node*/
p =h; /*p points to the header node*/
for(i=0;i<n;i++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL) /*Allocation New storage space and check */
{
printf("Cannot allocate memory space!");
return 0;
}
p->link=s; /* Assign the address of s to the link field of the node pointed to by p, so that The nodes pointed to by p and s are connected*/
printf("Please enter the name of the %d person:\t",i+1);
gets(s->name);/*at the current node s The name is stored in the data field of */
s->link=NULL;
p=s;
}
return(h);
}
int main()
{
int number; /*The variable that holds the number of people*/
stud *head; /*head is Pointer to save the address of the header node of the singly linked list */
number=N;
head=create(number); /*Assign the address of the newly created singly linked list header to head*/
return 0;
#include<string.h>
#include <malloc.h> /*The header file containing the dynamic memory allocation function*/
#define N 10 /*N is the number of people*/
typedef struct node
{
char name [20];
struct node *link; /** link is a pointer of node structure type, so it can be used to save the address of the next node,
such as: the following p->link=s;s is the current allocation The address of the node, p will be given the address of the previous node (eg: p=h;p=s),
so p->link=s, which is the linked list of the previous node (p->link) Save the address of the next node in the (s) */
}stud;
stud * create(int n) /* Function to create a singly linked list, parameter n is the number of people */
{
stud *p,*h,*s; / * *h saves the pointer of the header node, *p points to the previous node of the current node, *s points to the current node*/
int i; /*counter*/
if((h=(stud *)malloc( sizeof(stud)))==NULL) /* allocate space and check */
{
printf("Cannot allocate memory space!");
return 0;
}
h->name[0]='\0'; /* Empty the data field of the header node*/
h->link=NULL; /* Empty the link field of the header node*/
p =h; /*p points to the header node*/
for(i=0;i<n;i++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL) /*Allocation New storage space and check */
{
printf("Cannot allocate memory space!");
return 0;
}
p->link=s; /* Assign the address of s to the link field of the node pointed to by p, so that The nodes pointed to by p and s are connected*/
printf("Please enter the name of the %d person:\t",i+1);
gets(s->name);/*at the current node s The name is stored in the data field of */
s->link=NULL;
p=s;
}
return(h);
}
int main()
{
int number; /*The variable that holds the number of people*/
stud *head; /*head is Pointer to save the address of the header node of the singly linked list */
number=N;
head=create(number); /*Assign the address of the newly created singly linked list header to head*/
return 0;
}
//The bold words are added by myself, please correct me if there are any mistakes, thank you~