/*****************************************************************************
* *
* ------------------------------- stack.c -------------------------------- *
* *
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include "list.h"
#include "stack.h"
int select(void);
void search(void *data);
void print();
Stack *stack;
void main()
{
stack=(Stack *)malloc(sizeof(Stack));
stack_init(stack,NULL);
select();
}
int select(void)
{
void *add;
int input;
while(1)
{
printf("n");
printf(" ┏━━━ stack menu v1.0 ━━━━┓ n");
printf(" ┃ 1 -> stack init ┃n");
printf(" ┃ 2 -> stack insert ┃n");
printf(" ┃ 3 -> stack remove ┃n");
printf(" ┃ 4 -> stack search ┃n");
printf(" ┃ 5 -> stack print ┃n");
printf(" ┃ 0 -> Exit ┃n");
printf(" ┣━━━━━━━━━━━━━━━━┫n");
printf(" ┃select number ┃n");
printf(" ┗━━━━━━━━━━━━━━━━┛n");
printf(" ==> ");
scanf("%d",&input);
switch(input)
{
case 1:
stack_destroy(stack);
stack_init(stack,NULL);
system("cls");
break;
case 2:
system("cls");
printf(" Input data -> ");
scanf("%d",&add);
stack_push(stack,add);
printf(" Input Success n");
break;
case 3:
system("cls");
stack_pop(stack,&(stack->tail));
printf(" Data Remove Success n");
break;
case 4:
system("cls");
printf(" Input Data --> ");
scanf("%d",&add);
search(add);
break;
case 5:
system("cls");
print();
break;
case 0:
stack_destroy(stack);
return 0;
default:
system("cls");
printf(" You select wrong number Try to again selectnn");
break;
}
}
}
void print()
{
ListElmt *add;
add=(ListElmt *)malloc(sizeof(ListElmt));
add=stack->head;
printf("nnt TAIL n");
while(add != NULL)
{
printf("t┣━━ %d━━┫n",add->data);
add = list_next(add);
}
printf("t┗━━━━━┛ n");
printf("t HEAD nn");
return;
}
void search(void *data)
{
ListElmt *add;
add=(ListElmt *)malloc(sizeof(ListElmt));
add=stack->head;
if(add==NULL)
{
printf(" No data n");
return;
}
for(add=stack->head;add!=NULL;add=list_next(add))
{
if(add->data==data)
{
printf(" Search Ok n");
return;
}
}
printf(" Not Search Data n");
return;
}
/*****************************************************************************
* *
* ------------------------------ stack_push ------------------------------ *
* *
*****************************************************************************/
int stack_push(Stack *stack, const void *data) {
/*****************************************************************************
* *
* Push the data onto the stack. *
* *
*****************************************************************************/
return list_ins_next(stack, NULL, data);
}
/*****************************************************************************
* *
* ------------------------------ stack_pop ------------------------------- *
* *
*****************************************************************************/
int stack_pop(Stack *stack, void **data) {
/*****************************************************************************
* *
* Pop the data off the stack. *
* *
*****************************************************************************/
return list_rem_next(stack, NULL, data);
}
/*****************************************************************************
* *
* ------------------------------- stack.h -------------------------------- *
* *
*****************************************************************************/
#ifndef STACK_H
#define STACK_H
#include <stdlib.h>
#include "list.h"
/*****************************************************************************
* *
* Implement stacks as linked lists. *
* *
*****************************************************************************/
typedef List Stack;
/*****************************************************************************
* *
* --------------------------- Public Interface --------------------------- *
* *
*****************************************************************************/
#define stack_init list_init
#define stack_destroy list_destroy
int stack_push(Stack *stack, const void *data);
int stack_pop(Stack *stack, void **data);
#define stack_peek(stack) ((stack)->head == NULL ? NULL : (stack)->head->data)
#define stack_size list_size
#endif
* *
* ------------------------------- stack.c -------------------------------- *
* *
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include "list.h"
#include "stack.h"
int select(void);
void search(void *data);
void print();
Stack *stack;
void main()
{
stack=(Stack *)malloc(sizeof(Stack));
stack_init(stack,NULL);
select();
}
int select(void)
{
void *add;
int input;
while(1)
{
printf("n");
printf(" ┏━━━ stack menu v1.0 ━━━━┓ n");
printf(" ┃ 1 -> stack init ┃n");
printf(" ┃ 2 -> stack insert ┃n");
printf(" ┃ 3 -> stack remove ┃n");
printf(" ┃ 4 -> stack search ┃n");
printf(" ┃ 5 -> stack print ┃n");
printf(" ┃ 0 -> Exit ┃n");
printf(" ┣━━━━━━━━━━━━━━━━┫n");
printf(" ┃select number ┃n");
printf(" ┗━━━━━━━━━━━━━━━━┛n");
printf(" ==> ");
scanf("%d",&input);
switch(input)
{
case 1:
stack_destroy(stack);
stack_init(stack,NULL);
system("cls");
break;
case 2:
system("cls");
printf(" Input data -> ");
scanf("%d",&add);
stack_push(stack,add);
printf(" Input Success n");
break;
case 3:
system("cls");
stack_pop(stack,&(stack->tail));
printf(" Data Remove Success n");
break;
case 4:
system("cls");
printf(" Input Data --> ");
scanf("%d",&add);
search(add);
break;
case 5:
system("cls");
print();
break;
case 0:
stack_destroy(stack);
return 0;
default:
system("cls");
printf(" You select wrong number Try to again selectnn");
break;
}
}
}
void print()
{
ListElmt *add;
add=(ListElmt *)malloc(sizeof(ListElmt));
add=stack->head;
printf("nnt TAIL n");
while(add != NULL)
{
printf("t┣━━ %d━━┫n",add->data);
add = list_next(add);
}
printf("t┗━━━━━┛ n");
printf("t HEAD nn");
return;
}
void search(void *data)
{
ListElmt *add;
add=(ListElmt *)malloc(sizeof(ListElmt));
add=stack->head;
if(add==NULL)
{
printf(" No data n");
return;
}
for(add=stack->head;add!=NULL;add=list_next(add))
{
if(add->data==data)
{
printf(" Search Ok n");
return;
}
}
printf(" Not Search Data n");
return;
}
/*****************************************************************************
* *
* ------------------------------ stack_push ------------------------------ *
* *
*****************************************************************************/
int stack_push(Stack *stack, const void *data) {
/*****************************************************************************
* *
* Push the data onto the stack. *
* *
*****************************************************************************/
return list_ins_next(stack, NULL, data);
}
/*****************************************************************************
* *
* ------------------------------ stack_pop ------------------------------- *
* *
*****************************************************************************/
int stack_pop(Stack *stack, void **data) {
/*****************************************************************************
* *
* Pop the data off the stack. *
* *
*****************************************************************************/
return list_rem_next(stack, NULL, data);
}
/*****************************************************************************
* *
* ------------------------------- stack.h -------------------------------- *
* *
*****************************************************************************/
#ifndef STACK_H
#define STACK_H
#include <stdlib.h>
#include "list.h"
/*****************************************************************************
* *
* Implement stacks as linked lists. *
* *
*****************************************************************************/
typedef List Stack;
/*****************************************************************************
* *
* --------------------------- Public Interface --------------------------- *
* *
*****************************************************************************/
#define stack_init list_init
#define stack_destroy list_destroy
int stack_push(Stack *stack, const void *data);
int stack_pop(Stack *stack, void **data);
#define stack_peek(stack) ((stack)->head == NULL ? NULL : (stack)->head->data)
#define stack_size list_size
#endif
출처 - C로 구현한 알고리즘, 한빛미디어