회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
런타임에러(AccessNullPointer) 헬프
28279번 - 덱 2
glaceon0214
2년 전
0
여러 예제를 입력해봐도 답은 잘 나오는데 어디서 널 포인터를 참조했는지 모르겠읍니다...
#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct DequeNode{ struct DequeNode *llink, *rlink; int data; }DequeNode; typedef struct{ DequeNode *front, *rear; int cnt; // 노드 개수 카운팅 }DequeType; int isEmpty(DequeType* DQ){ if(DQ->front == NULL) return 1; else return 0; } void push_front(DequeType* DQ, int X){ DequeNode* newNode = (DequeNode*)malloc(sizeof(DequeNode)); newNode->data = X; newNode->llink = NULL; if(isEmpty(DQ)){ DQ->front = newNode; DQ->rear = newNode; newNode->rlink = NULL; } else{ DQ->front->llink = newNode; newNode->rlink = DQ->front; DQ->front = newNode; } DQ->cnt++; } void push_rear(DequeType* DQ, int X){ DequeNode* newNode = (DequeNode*)malloc(sizeof(DequeNode)); newNode->data = X; newNode->rlink = NULL; if(isEmpty(DQ)){ DQ->front = newNode; DQ->rear = newNode; newNode->llink = NULL; } else{ DQ->rear->rlink = newNode; newNode->llink = DQ->rear; DQ->rear = newNode; } DQ->cnt++; } int pop_front(DequeType* DQ){ DequeNode* temp = DQ->front; int n; if(isEmpty(DQ)) return -1; else if(DQ->front == DQ->rear){ // 노드가 1개인 경우 n = temp->data; DQ->front = NULL; DQ->rear = NULL; free(temp); } else{ n = temp->data; DQ->front = DQ->front->rlink; DQ->front->llink = NULL; free(temp); } DQ->cnt--; return n; } int pop_rear(DequeType* DQ){ DequeNode* temp = DQ->rear; int n; if(isEmpty(DQ)) return -1; else if(DQ->front == DQ->rear){ // 노드가 1개인 경우 n = temp->data; DQ->front = NULL; DQ->rear = NULL; free(temp); } else{ n = temp->data; DQ->rear = DQ->rear->llink; DQ->rear->rlink = NULL; free(temp); } DQ->cnt--; return n; } int countNode(DequeType* DQ){ return DQ->cnt; } int peek_front(DequeType* DQ){ if(isEmpty(DQ)) return -1; else return DQ->front->data; } int peek_rear(DequeType* DQ){ if(isEmpty(DQ)) return -1; else return DQ->rear->data; } int main(){ int N; scanf("%d", &N); DequeType* DQ = (DequeType*)malloc(sizeof(DequeType)); DQ->front = NULL; DQ->rear = NULL; DQ->cnt = 0; for(int i=0; i<N; i++){ int quest; scanf("%d", &quest); if(quest == 1){ int n; scanf("%d", &n); push_front(DQ, n); } else if(quest == 2){ int n; scanf("%d", &n); push_rear(DQ, n); } else if(quest == 3) printf("%d\n", pop_front(DQ)); else if(quest == 4) printf("%d\n", pop_rear(DQ)); else if(quest == 5) printf("%d\n", countNode(DQ)); else if(quest == 6) printf("%d\n", isEmpty(DQ)); else if(quest == 7) printf("%d\n", peek_front(DQ)); else if(quest == 8) printf("%d\n", peek_rear(DQ)); } return 0; }
댓글을 작성하려면
로그인
해야 합니다.
glaceon0214 2년 전
여러 예제를 입력해봐도 답은 잘 나오는데 어디서 널 포인터를 참조했는지 모르겠읍니다...