728x90
반응형
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct _node
{
int data;
struct _node* next;
}Node;
typedef struct _stack
{
Node* tail;
int Number;
}Stack;
void Init(Stack* copy)
{
copy->tail = NULL;
copy->Number = 0;
}
void Push(Stack* copy, int data)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
if (copy->tail == NULL)
{
copy->tail = newNode;
newNode->next = newNode;
}
newNode->next = copy->tail->next;
copy->tail->next = newNode;
(copy->Number)++;
}
int Pop(Stack* copy)
{
if (copy->tail == NULL)
{
return -1;
}
else
{
Node* temp = copy->tail->next;
int temp_data = temp->data;
copy->tail->next = temp->next;
(copy->Number)--;
free(temp);
return temp_data;
}
}
int Peek(Stack* copy)
{
if (copy->tail == NULL)
{
return -1;
}
return copy->tail->next->data;
}
int Count(Stack* copy)
{
return copy->Number;
}
int Empty(Stack* copy)
{
if (copy->tail == NULL)
return TRUE;
else
return FALSE;
}
int main(void)
{
Stack box;
Init(&box);
Push(&box, 10);
Push(&box, 20);
Push(&box, 30);
printf("Current Number of Box: %d\n", Count(&box));
printf("Pop data: %d\n", Pop(&box));
printf("Current Number of Box: %d\n", Count(&box));
printf("Peek data: %d\n", Peek(&box));
return 0;
}
728x90
반응형
'Data Structure & Algorithm' 카테고리의 다른 글
[Python] Escape from maze (0) | 2022.10.25 |
---|---|
[Python] Flow control (0) | 2022.10.24 |
Maze.py (0) | 2022.10.21 |
Bidirectional Linked List(+ Merger) (0) | 2022.10.20 |
Circular Linked List(+ Merger) (0) | 2022.10.20 |