Playing with C라는 책에 있는 Linked_List를 이용한 Stack 구현 방법을 간단하게 표현하기 위해서 작성 중이다.


 Linked_List을 이용하여 Stack을 구현하면, 장점은 배열의 크기를 초과하여도 문제 없는 장점이 있다.


 void_Linked_List에서 계속 오류가 나기 때문에 문제점이 무엇인지 확인 하기 위해서 지금 이 소스를 보고 분석 중이다.

 왜 문제가 생기는지 알 수 있다면, 그걸 보고 고쳐서 사용할 수 있다고 생각한다.


List.h 

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>
 
struct _node{
    int data;
    struct _node* next;
};
 
struct _node* mkList();
int addFront(struct _node**,int data);
int delFront(struct _node**);
 
 
cs


List.c

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
#include "list.h"
 
/**
*/
struct _node* mkList(){
    struct _node* lp = (struct _node*)NULL;
    return lp;
}
 
/**
*/
int addFront(struct _node** lp, int data){
 
    struct _node* n = NULL;
 
    n = (struct _node*)malloc(sizeof(struct _node));
 
    if(n==NULL){
        printf("Error");
        return 1;
    }
 
    n->data = data;
    n->next = *lp;
    *lp = n;
 
    return 0;
}
 
/**
*/
int delFront(struct _node** lp){
 
    struct _node* head = NULL;
    int data = NULL;
 
    if(*lp==NULL){
        printf("Error is Emplty List");
        return 1;
    }
 
    head = *lp;
 
    *lp = head->next;
    data = head->data;
 
    free(head);
 
    return data;
}
 
 
 
cs


main.c

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
#include "list.h"
 
int main()
{
    int data = 0;
    int n = 0, i = 0;
    struct _node** stacks=NULL;
 
    stacks = (struct _node**)malloc(sizeof(struct _node*));
 
    printf("몇개의 정수를 입력하고 싶으십니까?");
    scanf("%d", &n);
 
    if(n<=0){
        printf("Error Numbers");
        return 1;
    }
 
    *stacks = mkList();
 
    printf("%d개의 정수를 입력해주세요.",n);
    for(i=0;i<n;++i){
        scanf("%d", &data);
        addFront(stacks,data);
    }
 
    printf("당신이 입력한 정수를 역순으로 출력합니다.\n");
    while(!*stacks==NULL){
        data = delFront(stacks);
        printf(" %d",data);
    }
 
    return 0;
}
 
cs


Posted by JunkMam
,