单链表//elemtype为结构体形式输入

#include
using namespace std;
typedef struct
{
     
	string name;
	int year;
}elemtype;
typedef struct lnode
{
     
	elemtype data;
	lnode* next;
}*linklist;
elemtype haha[10];
int creatlist_r(linklist& l, int n)
{
     
	linklist p,r;
	l = new lnode;
	l->next = NULL;
	r = l;
	for (int i = 0; i < n; i++)
	{
     
		p = new lnode;
		cin >> haha[i].name>> haha[i].year;
		p->next = NULL;
		r->next = p;
		r = p;
	}
	return 0;
}
void pr_(linklist l)
{
     
	int n=0;
	lnode* p = l->next;
	while (p)
	{
     
		p = p->next; ++n;
	}
	for (int i = 0; i < n; i++)
	{
     
		cout << haha[i].name << "  " << haha[i].year << endl;
	}
}
int main()
{
     
	linklist haha;
	int n;
	cout << "请输出要输入的个数:";
	cin >> n;
	creatlist_r(haha, n);
	pr_(haha);
}

你可能感兴趣的:(数据结构)