큐 를 나름 짜봤는데 종료가 되버리네요.
총알탄
단순 연결리스트로 큐를 짜봤는데 에러메시지는 없지만 프로그램을 실행하고 나면 에러가 뜹니다.
어디가 잘못 됐는지 도통 봐도 모르겠네요.
어디가 잘못됐는지 알려주시면 고맙겠습니다.
질문 내용 :
#include stdio.h
#include stdlib.h
typedef struct Node{
int data;
struct Node *next;
}node;
node *rear,*front;
void init(){node *front=(node*)malloc(sizeof(node));
front-next=NULL;
rear=front;
}
void queue(){
node *a,*temp;
int i;
temp=front;
a=(node*)malloc(sizeof(node));
printf(input data:);
scanf(%d,&i);
a-data=i;
if(temp==NULL){
temp-next=a;
a-next=NULL;
rear=a;
}
else{
rear-next=a;
a-next=NULL;
}
}
void del(){
node *a;
a=front-next;
if(a==NULL){
printf(There in no data\n);
}
front-next=a-next;
free(a);
}
void print(){
node *temp;
temp=front-next;
while(temp!=NULL){
printf(%5d,temp-data);
temp=temp-next;
}
}
main(){
int n;
init();
while(1){
printf(\n\n);
printf([1]queue [2] del :);
scanf(%d,&n);
if(n==1){
queue();
print();
}
else if(n==2){
del();
print();
}
else { break; }
}
}
-
라라
node *rear,*front;
여기에 있는 front는 전연변수이고
node *front=(node*)malloc(sizeof(node));
여기에 있는 front는 지역변수이고
서로 다른 변수입니다. -
중국드립
temp=front-next;
에서 문제 한 개.
if(temp==NULL){
temp-next=a;
a-next=NULL;
rear=a;
}
else{
rear-next=a;
a-next=NULL;
}
에서 한 개가 있습니다.
정확히 뭔지는 저도 구조체가 약한 사람이라서 모르겠습니다.