내 입맛대로 stack 구현하기
1. empty
2. push
3. pop
4. size
push를 할 때 자신의 입맛대로 매개변수 만들어서 넣으시면 됩니다.
node에는 해당 문제에 따라서 저장할 값을 추가적으로 넣어주시면 됩니다.
#include <stdbool.h>
#include <stdlib.h>
typedef long long ll;
typedef struct node{
char value;
int cnt;//몇 개 쌓아왔는지
struct node* pre;
}node;
typedef struct stack{
node *top;
ll size;
}stack;
ll size(stack* s){
return s->size;
}
node* top(stack*s){
return s->top;
}
bool empty(stack*s){
return s->size==0;
}
void push(stack*s,char value,int cnt){
node* newv=(node *)malloc(sizeof(node));
newv->value=value;
newv->cnt=cnt;
newv->pre=s->top;
s->top=newv;
s->size++;
}
void pop(stack*s){
node*removes=s->top;
s->top=s->top->pre;
s->size--;
free(removes);
}
void init(stack*s){
s->top=NULL;
s->size=0;
}
'자료구조 > by C' 카테고리의 다른 글
c언어 double linked list 구현 (0) | 2023.06.28 |
---|---|
c언어 vector 구현 (0) | 2023.06.24 |
c언어 queue 구현 (0) | 2023.06.23 |