The chain that needs to be practiced more

#include<stdio.h>
#include<string.h>
#include<malloc.h>

struct Student{
	char name[10];
	struct Student *next;
} ;

// create linked list
struct Student * create()
{
	// head points to the first element
	// current points to the currently processed element
	// next points to the new, next element
	struct Student *head, *current, *next;
	// store name
	char str[10];
	// store the judgment symbol
	char flag;

	printf("Please enter student name:\n");
	scanf("%s", str);
	// Eliminate the interference of carriage return symbols
	getchar();

	// Apply for a piece of dynamic memory as the storage place for the first element
	head = (struct Student *)malloc( sizeof(struct Student) );
	strcpy( head->name, str );

	current = head;

	printf("Whether to continue input: (Y/N)");
	scanf("%c", &flag);

	while( flag != 'N' )
	{
		printf("Please enter the student's name\n");
		scanf("%s", str);
		// Eliminate the interference of carriage return symbols
		getchar();

		// Apply for a piece of dynamic memory as a new element storage place
		next = (struct Student *)malloc( sizeof(struct Student) );
		strcpy( next->name, str );

		// pointer to the previous element points to the next element
		current->next = next;

		// The current pointer points to the next element
		current = next;
		
		printf("Whether to continue input: ");
		scanf("%c", &flag);
	}

	// At the end of the loop, the current pointer points to the last element, at this time assign the pointer of the last element to NULL
	current->next = NULL;

	return head;
}

// traverse the linked list
void list(struct Student *p)
{
	while(1)
	{
		printf("%s \n", p->name);

		if( p->next != NULL )
		{
			p = p->next;
		}else
		{
			break;
		}
	}
}

// insert linked list
void insert(struct Student *p)
{
	// insert the element to be inserted
	// current currently processed element
	struct Student *insert, *current;
	char str[10];
	int position;

	current = p;
	
	printf("\nPlease enter the name of the student to be inserted\n");
	scanf("%s", str);
	getchar();

	insert = (struct Student *)malloc( sizeof(struct Student) );
	strcpy( insert->name, str );

	printf("The position to be inserted is:\n");
	scanf("%d", &position);

	if( position > 0 )
	{
		// Need to insert the position of position - 1

		while(position > 1)
		{
			current = current->next;
			position--;
		}

		// The inserted element points to the next element of the current element
		insert->next = current->next;

		// The next element of the current element points to the inserted element
		current->next = insert;		

	}else if ( position == 0 )
	{
		// Insert before the first element;
		p = insert;
		insert->next = current;
	}
	
	printf("The latest member list after inserting element is:\n");
	list(p);
}

int main(void)
{
	struct Student *p;
	p = create();

	printf("\nThe current list element is:\n");
	list(p);

	// loop insert
	while(1)
	{
		insert(p);
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324472451&siteId=291194637