Skip to content

Commit

Permalink
Linked Struct
Browse files Browse the repository at this point in the history
연결리스트를 사용해서 스택 구현
  • Loading branch information
hyejun0608 committed Sep 22, 2020
1 parent 6691e66 commit 1d1a284
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Linked Struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>

int isEmpty();
void push(int data);
void pop();
void print();

typedef struct Node* Nodepointer;
typedef struct Node {
int data;
Nodepointer link;
} Node;
Nodepointer top = NULL;

int main() {
push(1);
push(2);
print();
pop();
print();
pop();

return 0;
}

// 스택이 비어있는지 확인하는 함수
int isEmpty() {
if (!top) return 1;
else return 0;
}
// 스택을 생성하고 값을 넣어주는 함수
// 스택이 공백일 때, 아닐 때 둘 다 사용할 수 있다
void push(int value) {
Nodepointer temp = (Nodepointer)malloc(sizeof(Nodepointer));
temp->data = value;
temp->link = top;
top = temp;
}
// 스택을 삭제하는 함수
void pop() {
if (isEmpty()) return printf("Stack is empty!\n");
int item;
Nodepointer temp = top;
item = top->data;
top = top->link;
free(temp);
return item;
}
// 스택 데이터 값을 출력하는 함수
void print() {
for (Nodepointer ptr = top; ptr != NULL; ptr = ptr->link) {
printf("%d\n", ptr->data);
}
}

0 comments on commit 1d1a284

Please sign in to comment.