단순 연결 리스트 관련 문제인데 어디서 예외처리가 안된 건가요?ㅠㅠ
조회수 753회
#include <iostream>
#include <string>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int e) {
this->data=e;
this->next=NULL;
}
};
class SLinkedList {
public:
Node* head;
Node* tail;
SLinkedList() {
head=NULL;
tail=NULL;
}
void addFront(int x) {
Node* newNode=new Node(x);
if (head == NULL) {
head=tail=newNode;
}
else {
newNode->next=head;
head=newNode;
}
}
int removeFront() {
if (head == NULL) {
return -1;
}
else {
Node* tmp=head;
head=head->next;
int s=tmp->data;
delete tmp;
return s;
}
}
int front() {
if(head==NULL){return -1;}
else {
return head->data;
}
}
int empty() {
if(head==NULL){return 1;}
else {
return 0;
}
}
};
int main() {
int cnt=0; // 첫번째 줄의 명령어의 수 N
string str; // 두번째 줄의 명령어 문자열
int cmd=0; // 명령어 문자열의 매개변수
SLinkedList list;
cin >> cnt;
if (cnt >= 1 && cnt <= 100000) {
for (int i = 0; i < cnt; i++) {
cin>>str;
if (str == "addFront") {
cin >> cmd;
if (cmd >= 1 && cmd <= 100000) {
list.addFront(cmd);
}
}
else if (str == "removeFront") {
int s = list.removeFront();
cout << s << endl;
}
else if (str == "front") {
int s=list.front();
cout << s << endl;
}
else if (str == "empty") {
int s = list.empty();
cout << s << endl;
}
}
}
return 0;
}
입출력은 예시대로 나오는데 채점만 하면 이게 틀렸다고 나와서 골머리를 앓고 있습니다... 정말 쉬운 문제인거 같은데 어디서 예외처리가 낫길래 이모양인가요?ㅠㅠ
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력