数据结构--栈和队列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nifecat/article/details/79902145

使用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;
}
}; 

猜你喜欢

转载自blog.csdn.net/nifecat/article/details/79902145