C언어/문제풀다 하나씩
문자열에서 숫자 찾기 - 링크드리스트
mcdn
2020. 4. 12. 00:08
반응형
#include <iostream>
#include <cstring>
using namespace std;
struct Node {
char a;
Node* next;
};
Node* head;
Node* last;
void addnode(int abc) {
if (head == NULL) {
head = new Node();
head->a = abc;
last = head;
}
else {
Node* temp;
temp = new Node();
temp->a = abc;
temp->next = head; // AGAIN! its not head->next
head = temp;
}
}
int main() {
char sent[16];
cin >> sent;
int len = strlen(sent);
for (int i = 0; i < len;i++) {
if (sent[i] >= '0' && sent[i] <= '9') {
addnode(sent[i]-48);
}
} // 9-9-9-1
int answer = 0;
int count = 1;
for (Node* p = head; p != NULL;p = p->next) {
answer += (p->a) * count;
count *= 10;
}
cout << answer + 5;
}
Node* temp;
temp = new Node();
temp->a = abc;
temp->next = head; // AGAIN! its not head->next
head = temp;
여기서 또 head->next 해서 계속 에러남
head 는 노드포인터라서 head면 자동으로 가리키게됨.
반응형