c++新建单链表

#include
using namespace std;
struct Node{
	int data;
	Node *next;
};

int main()
{
	Node *head,*p,*r;//头指针,节点,尾指针
	int x;
	cin>>x;
	head = new Node;//申请头结点
	r = head;
	while(x!=-1){
		p = new Node;
		p->next=NULL;
		p->data=x;
		r->next=p;
		r=p;
		cin>>x;
	}
	p=head->next;
	while(p->next!=NULL){
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<p->data<<" ";
	return 0;
}

你可能感兴趣的:(逻辑练习,c++,算法,数据结构)