C언어/문제풀다 하나씩

head->next = new Node() 해주기

mcdn 2020. 4. 10. 19:05
반응형

#include <iostream>
#include <cstring>
using namespace std;

struct Node {
	int a;
	Node* next;
};
int main() {
	Node* head = new Node();
	head->a = 3;
	head->next = new Node();
	head->next->a = 5;
	head->next->next = new Node();
	head->next->next->a = 4;
	head->next->next->next = new Node();
	head->next->next->next->a = 2;
	Node* last = head;
	while (last != NULL) {
		cout << last->a<<" ";
		last = last->next;

	}


}

output : 3 5 4 2

 

주의할점

Node*head 를 만들때 new Node() 꼭 해주기

 

head->next->a 전에

head->next = new Node() 해주기

 

 

 

 

반응형