The data structure realizes the head interpolation and the tail interpolation

I really don’t want to deal with the brain-burning thing like data structure. I only learned that there is a data structure for a semester. In order to learn the C language, I bit my head.

Head insertion method:

#include <stdio.h>
#include <stdlib.h>

struct list
{
    
    
	int data;
	struct list *next;
};

int main()
{
    
    
	struct list *inPut,*head;
	head->next = NULL;
	int i,n;
	printf("请输入排序数字:\n");
	for(i=0;i<5;i++){
    
    
		inPut = (struct list*)malloc(sizeof(struct list));
		scanf("%d",&inPut->data);

		inPut->next = head->next;
		head->next = inPut;
	}
	while((head->next)){
    
    

		printf("%d ",(head->next)->data);
		(head->next) = (head->next)->next;
	}
	printf("\n");
	
	return 0;
}

Tail insertion method:

#include <stdio.h>
#include <stdlib.h>

struct list
{
    
    
	int data;
	struct list *next;
};

int main()
{
    
    
	int i;	
	struct list *head,*tail,*input;

	head->next = NULL;
	tail = head;

	for(i=0; i<5; i++){
    
    
		input = (struct list*)malloc(sizeof(struct list));
		scanf("%d",&input->data);

		input->next = tail->next;
		tail->next = input;
		tail = input;
	}

	while((head->next)){
    
    
		printf("%d ",(head->next)->data);
		(head->next) = (head->next)->next;
	}
	printf("\n");

	return 0;
}

The second method of tail interpolation:

	for(i=0; i<5; i++){
    
    
		input = (struct list*)malloc(sizeof(struct list));
		scanf("%d",&input->data);
		
		tail->next = input;
		tail = input;
	}
	tail->next = NULL;

Guess you like

Origin blog.csdn.net/zouchengzhi1021/article/details/113747255