链表创建(尾插法)

#include<iostream>
#include<malloc.h>
#include<cstdio>
using namespace std;
struct List{
	int data;
	List* next;
}*list,*end;
int len = 0;
void List_made(){
	int t;
	list = new List;
	list->next = NULL;
	List *p;
	List *head = new List;
	head = list;
	while(scanf("%d" , &t) && t != 0){
		if(len == 0){
			p = new List;
			p->data = t;
			p->next = list;
			head = p;
		}
		else{
			p = new List;
			p->data = t;
			p->next = head;
			head = p;
		}len++;
	
	}
	end = head;

}
void read_list(){
	List *p = new List;
	p = end;
	while(p->next!=NULL){
		printf("%d\n" , p->data);
		p = p->next;
	}
}
int main()
{
	List_made();
	read_list();
}

猜你喜欢

转载自blog.csdn.net/peachhhh/article/details/108076229