C언어/문제풀다 하나씩
링크드리스트 int 넣기 myalloc함수 만들어서 넣기
mcdn
2020. 6. 23. 12:12
반응형
#include <iostream>
using namespace std;
struct node {
int abc;
node* next;
};
node* head;
node* last;
node buf[100];
int bufcnt;
node* myalloc() {
return &buf[bufcnt++];
}
void addnode(int c) {
if (head == NULL) {
head = myalloc();
head->abc = c;
last = head;
}
else {
last->next = myalloc();
last = last->next;
last->abc = c;
}
}
int main() {
int num;
cin >> num; //6
for (int i = 0; i < num;i++) {
int x;
cin >> x;
addnode(x);
}
for (node* p = head;p != NULL;p = p->next) {
cout << p->abc<<' ';
}
return 0;
}
새로 추가된 것
myalloc함수!
buf[bufcnt]의 주소값을 반환하고 이후 bufcnt에 1을 더한다.
NULL인 곳에 buf[bufcnt]주소값을 넣는 함수임
그래서 NULL인 head와 last->next에다가 myalloc()을 할당하는 것
그외엔 똑같다.
반응형