코드가 안돌아갑니다
조회수 535회
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 5
#define MALLOC(p,s) \
if (!((p) = malloc(s))) { \
fprintf(stderr, "Insufficient memory"); \
exit(1); \
}
typedef struct Node {
int data;
struct listNode *link;
}listNode;
listNode *top[MAX_STACK_SIZE];
void stackFull() {
fprintf(stderr, "Stack is full, cannot add element");
exit(1);
}
void stackEmpty() {
fprintf(stderr, "Stack is Empty");
exit(1);
}
int *push(int i, int a) //연결 스택에서의 삽입
{
if (i == 6)
stackFull();
listNode *temp;
MALLOC(temp, sizeof(*temp));
temp->data = a;
temp->link = top[i];
top[i] = temp;
return temp;
}
int *pop(int i) //연결 스택에서의 삭제
{
listNode *temp = top[i];
int item;
if (!temp)
stackEmpty();
item = temp->data;
top[i] = temp->link;
free(temp);
return temp;
}
void showStack(listNode *b) // 스택에 있는 값들을 출력
{
printf("The list contains: ");
for (; b; b = b->link)
printf("%4d", b->data);
printf("\n");
}
void main() {
int select = 0;
int a,i;
listNode *b[MAX_STACK_SIZE];
while (select != 4) {
printf("\n1. 스택에 값 넣기\n");
printf("2. 스택에서 값 꺼내기\n");
printf("3. 스택에 저장된 값 확인\n");
printf("4. 종료\n\n");
printf("입력 : ");
scanf_s("%d", &select);
getchar(); //입력버퍼 지우기
switch (select) {
case 1: // 입력한 값을 스택에 저장한다.
print("몇 번째 스택에 저장하실건가요? : \n");
scanf_s("%d", &i);
printf("저장할 값을 입력하세요 : ");
scanf_s("%d", &a);
b[i]= push(i,a);
break;
case 2: // 스택에 저장된 정수값을 가져온다.
print("몇 번째 스택에서 가져 오실건가요? : \n");
scanf_s("%d", &i);
b[i] = pop(i, a);
printf("가져온 값 : %d", b[i]->data);
break;
case 3: // 스택에 저장된 값들을 보여준다.
showStack(b);
break;
}
}
}
뭐땜에 안돌아가는지 수정해주세요 ㅠㅠ c로 돌려야합니다
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력