C++ 建立一个简单链表


#include<iostream>
using namespace std;

struct Node {
	int num;
	int score;
	Node* next;
};
Node* Create()
{
	
	int num, score;
	Node* p1,*p2, * head;
	p1 = p2=  new Node;
	head = NULL;
	while (cin >> num, cin >> score)
	{
		p1 = new Node;
		p1->num = num; p1->score = score;
		p1->next = NULL;

		if (head == NULL)
		{
			head = p1;
			p2 = p1;
		}
		else
		{
			p2->next = p1;
			p2 = p1;
		}
	}
	p2->next = NULL;
	return head;
}
//显示
void display(Node *head) 
{
	Node* temp = head;
	int num, score;
	while(temp != NULL)
	{
		num = temp->num; score = temp->score;
		cout << "[num=" << num << ",score=" << score << "]" << endl<<endl;
		temp = temp->next;
	}

}

int main()
{
	Node *root;
	root=Create();
	display(root);
   
}

猜你喜欢

转载自blog.csdn.net/laocooon/article/details/121896998
今日推荐