当前位置:   article > 正文

数据结构--栈和队列_数据结构栈和队列

数据结构栈和队列

使用C语言封装栈和队列:

struct node{

int value;
node *pre, *next;

};


struct mystack{
node *head, *tail;
int sz;
void stack_init(){
head = tail = NULL;
sz = 0;
}
void stack_push(int v){
if (sz == 0){
head = (node *)malloc(sizeof(node));
head->value = v;
tail = head;
head->pre = NULL;
head->next = NULL;
}
else {
node *tmp = (node *)malloc(sizeof(node));
tmp->value = v;
tail->next = tmp;
tmp->pre = tail;
tmp->next = NULL;
tail = tmp;
}
sz++;
}
void stack_pop(){
if (sz == 0){
puts("EMPTY");
}
else if (sz == 1){
sz--;
head = tail = NULL;
}
else {
sz--;
tail = tail->pre;
tail->next = NULL;
}
}
int stcak_top(){
return tail->value;
if (sz == 0){
puts("EMPTY");
}
else if (sz == 1){
printf("%d\n", tail->value);
sz--;
head = tail = NULL;
}
else {
sz--;
tail = tail->pre;
tail->next = NULL;
}
}

};


struct myqueue{
node *head,*tail;
int sz;
void queue_init(){
head = tail = NULL;
sz=0;
}
void queue_push(int v){
if (sz == 0){
head = (node *)malloc(sizeof(node));
head->value = v;
tail = head;
head->pre = NULL;
head->next = NULL;
}
else {
node *tmp = (node *)malloc(sizeof(node));
tmp->value = v;
tail->next = tmp;
tmp->pre = tail;
tmp->next = NULL;
tail = tmp;
}
sz++;
}
void queue_pop(){
if (sz == 0){
puts("EMPTY");
}
else if (sz == 1){
sz--;
head = tail = NULL;
}
else {
sz--;
head=head->next;
head->pre=NULL;
}
}
int queue_front(){
return head->value;
}
}; 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/975684
推荐阅读
相关标签
  

闽ICP备14008679号