스택을 이용한 큐 구현 ㅠ
옆집오빠야
스택을 이용한 큐 구현 ㅠ
말 그대로 스택을 이용한 큐 구현이에요 ㅠ
p3
p 23
p 4
o 4
o 23...
푸쉬인지 팝인지 표시하고 푸쉬또는 팝한 데이터를 함께출력하는거에요질문 내용 :
#includestdio.h
#includemalloc.h
typedef int element;
typedef struct stacknode{
element item;
struct stacknode *link;
}stacknode;
typedef struct{
stacknode *top;
}linkedstacktype;
void init(linkedstacktype *s)
{
s-top=null;
}
int is_empty(linkedstacktype *s)
{
return (s-top == null);
}
int is_full(linkedstacktype *s)
{
return 0;
}
void push(linkedstacktype *s, element item)
{
stacknode *temp=(stacknode *)malloc(sizeof(stacknode));
if( temp == null){
fprintf(stderr,메모리 할당에러\n);
return;
}
else{
temp-item = item;
temp-link = s-top;
s-top = temp;
}
}
element pop(linkedstacktype *s)
{
if(is_empty(s)){
fprintf(stderr,스택이 비어있음\n);
return 0;
}
else{
stacknode *temp = s-top;
element item = temp-item;
s-top = s-top-link;
free(temp);
return item;
}
}
void carry(linkedstacktype *a, linkedstacktype *b)
{
stacknode *tmp = a-top;
if(!(a-top == null) && (b-top == null)){
b-top = a-top;
a-top = a-top-link;
b-top-link = null;
}
while(!(a-top == null) && !(b-top == null)){
a-top = a-top-link;
tmp-link = b-top;
b-top = tmp;
}
}
void main()
{
linkedstacktype a, b;
push(&a, 10);
push(&a, 20);
push(&a, 30);
carry(&a, &b);
printf(&d\n, pop(&b));
}
여기서 모가문제일까요 ㅠㅠ 컴파일은되는데....
-
보라나
제가 어떻게 도와드려야 합니까.. 짜달라는건 아니시겠죠..?
그리고 쪽지로 질문은 하지 말아주시길...
carry가 만약 a노드들을 전부 b로 옮기는 것이라면..
한개 옮기고 링크 끊고 한개 옮기고 링크 끊고 하지 마시고..
a의 첫 노드, 끝 노드 등의 링크만 조정하시길.. -
조으다
그럼 어떻게 수정해야되는거죠 ㅠ
-
모이
LinkedStackType a = { 0 }, b = { 0 };
이렇게 초기화를...
쓰레기로 차있으니 top 멤버가 NULL 값을 가지리라 기대할 수 없습니다..
그리고 carry 함수도 조금 문제가 있는 것 같은데... -
파도
ㅠㅠ 부탁드려여