データ構造は、ヘッド補間とテール補間を実現します

データ構造のような頭の痛いものは本当に扱いたくありません。学期のデータ構造があることを知っただけです。C言語を学ぶために、頭をかみました。

ヘッド挿入方法:

#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;
}

テール挿入方法:

#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;
}

テール補間の2番目の方法:

	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;

おすすめ

転載: blog.csdn.net/zouchengzhi1021/article/details/113747255