편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2021.05.10

    C언어 스택 연결리스트 모든 temp의 link값이 0만 들어갑니다.


    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 10 //스택의 최대수
    
    /*스택의 정의*/
    typedef struct {
        int key;
    }element;
    typedef struct stack *stackpoint;
    typedef struct stack {
        element data;
        stackpoint link;
    } stack;
    stackpoint top[MAX];
    void push(int i, element item);
    element pop(int i);
    void stackEmpty();
    
    void main() {
        int i = 0, n;
        element item, x;
        for (i = 0; i <= 1; i++) 
            top[i] = NULL ;
        for (i = 0; i <= 1; i++) {
            printf("%d 번째 값입력 :", i+1);
            scanf_s("%d", &item);
            push(i, item);
        }
        printf("제거할 스택의 위치를 입력해주세요 : ");
        scanf_s("%d", &n);
        x = pop(n);
        printf("제거된 값은 %d 입니다. ", x);
    }
    
    //스택을 연결리스트에 넣는 함수
    void push(int i, element item) {
        stackpoint temp;
        temp = (stackpoint)malloc(sizeof(stackpoint));
        temp->data = item;
        temp->link = top[i];
        top[i] = temp;
    }
    
    //스택을 빼는 함수
    element pop(int i) {
        stackpoint temp = top[i];
        element items;
        if (!temp)
            stackEmpty();
        items = temp->data;
        top[i] = temp->link;
        free(temp);
        return items;
    }
    void stackEmpty() {
        printf("스택이 비어있습니다.\n");
        exit(EXIT_FAILURE);
    }
    

    어떤식으로 프로그램을 만드려고 했냐면 top배열을 통해 함수의 주소를 받아 다음 들어오는 스택에 link에 그 주소를 넣어서 연결하는 프로그램??

    글로 뭔가 제 말을 표현 못하는 거같은데 죄송합니다.

    그런 프로그램을 짜봤는데 모든 temp의 link값이 0만 들어갑니다. 어떻게 하면 연결리스트를 구성할 수있을까요? 책보고 혼자 공부하는 고등학생이라 잘모르겠어서 질문드립니다.

  • 프로필 알 수 없는 사용자님의 편집
    날짜2021.05.10

    연결리스트를 이용한 배열


    include

    include

    define MAX 10 //스택의 최대수

    /스택의 정의/ typedef struct { int key; }element; typedef struct stack *stackpoint; typedef struct stack { element data; stackpoint link; } stack; stackpoint top[MAX]; void push(int i, element item); element pop(int i); void stackEmpty();

    void main() { int i = 0, n; element item, x; for (i = 0; i <= 1; i++) top[i] = NULL ; for (i = 0; i <= 1; i++) { printf("%d 번째 값입력 :", i+1); scanf_s("%d", &item); push(i, item); } printf("제거할 스택의 위치를 입력해주세요 : "); scanf_s("%d", &n); x = pop(n); printf("제거된 값은 %d 입니다. ", x); }

    //스택을 연결리스트에 넣는 함수 void push(int i, element item) { stackpoint temp; temp = (stackpoint)malloc(sizeof(stackpoint)); temp->data = item; temp->link = top[i]; top[i] = temp; }

    //스택을 빼는 함수 element pop(int i) { stackpoint temp = top[i]; element items; if (!temp) stackEmpty(); items = temp->data; top[i] = temp->link; free(temp); return items; } void stackEmpty() { printf("스택이 비어있습니다.\n"); exit(EXIT_FAILURE); }

    어떤식으로 프로그램을 만드려고 했냐면 top배열을 통해 함수의 주소를 받아 다음 들어오는 스택에 link에 그 주소를 넣어서 연결하는 프로그램?? 글로 뭔가 제 말을 표현 못하는 거같은데 죄송합니다. 그런 프로그램을 짜봤는데 모든 temp의 link값이 0만 들어갑니다. 어떻게 하면 연결리스트를 구성할 수있을까요?...... 책보고 혼자 공부하는 고등학생이라 잘모르겠어서 질문드립니다 ㅠ