자료구조_c언어_연결링크_linkedStack02

2021. 1. 31. 00:24개발하는중/자료구조

728x90
반응형

전에 작성한 연결링크 다음 연습니다. 옛날에 테스트하며 연습한거에요~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
 
struct Node {
    char name[50];
    int price;
    struct Node *link;
};
 
struct Node *top = NULL;
int total = 0;
 
void push();
void pop();
void show();
 
int main() {
    int m;
    while (1) {
        system("cls");
        puts("#####상품관리#####");
        printf("1. 전체목록\n2. 상품등록\n3. 상품판매\n4. 누적판매대금\n");
        scanf("%d"&m);
        if (m == 1) {
            show();
        }
        else if (m == 2) {
            push();
        }
        else if (m == 3) {
            pop();
        }
        else if (m == 4) {
            printf("누적 판매대금은 %d원\n", total);
            system("pause");
        }
        else {
            printf("존재하지않는메뉴");
            system("pause");
        }
 
 
    }
}
void pop() {
    struct Node *temp = top;
    if (temp != NULL) {
        printf("%s 이/가 팔렸습니다. 판매금액: %d\n", temp->name, temp->price);
        total = total + temp->price;
        top = temp->link;
        free(temp);
        
    }
    else {
        puts("더 이상 처리할 데이터가 없습니다.");
    }
    system("pause");
}
 
void push() {
    struct Node *tp = (struct Node*)malloc(sizeof(struct Node));
    printf("상품이름:");
    scanf("%s", tp->name);
    printf("상품가격:");
    scanf("%d"&tp->price);    // name은 배열이라 &안붙여도됨 price는 int정수형 이기떄문에 &붙여줘야함
    tp->link = NULL;
    
    if (top == NULL) {
        top = tp;
    }
    else {
        tp->link = top;        // top가 첫번째 노드 가지고있음 tp->link에 첫번째 주소값 주고 
        top = tp;            // 지금 만들어진 tp노드데이터값을 top에 준다.
    }
}
 
void show() {
    struct Node *= top;
    while (1) {
        if (i != NULL) {
            printf("상품정보[%s/%d원]\n", i->name, i->price);
            i = i->link;    // 제일 나중에 쓴 데이터 출력후 해당 link i에 준다.
        }
        else {
            break;
        }
        system("pause");
 
    }
}
cs
728x90