实现栈和队列(链式和顺序式)

基于顺序表实现的栈

#include"Stack.h"
#include<stdio.h>
void SeqStackInit(SeqStack* Stack){
    if (Stack == NULL){
        return;
    }
    Stack->size = 0;
}
void SeqStackPush(SeqStack* Stack, StackType value){
    if (Stack == NULL){
        return;
    }
    if (Stack->size == SEQSTACKMAXSIZE){
        return;
    }
    Stack->data[Stack->size] = value;
    Stack->size++;
}
void SeqStackPop(SeqStack* Stack){
    if (Stack == NULL){
        return;
    }
    if (Stack->size == 0){
        return;
    }
    --Stack->size;
}

int SeqStackFindTop(SeqStack* Stack,StackType* value){//输出型参数
    if (Stack == NULL||Stack->size==0){
        return 0;
    }
    *value = Stack->data[Stack->size-1];
    return 1;
}
void SeqStackDestroy(SeqStack* Stack){
    if (Stack == NULL){
        return;
    }
    Stack->size = 0;
}
size_t SeqSize(SeqStack* Stack){
    return Stack->size;
}
void StackDisplay(SeqStack* Stack){
    if (Stack == NULL){
        return;
    }
    printf("【栈底】");
    for (size_t i = 0; i < SEQSTACKMAXSIZE; i++){
        printf("%c ", Stack->data[i]);
    }
    printf("【栈顶】");
}

基于链表实现的栈


#include"Stack.h"
#include<stdio.h>
void LinkStackInit(LinkStack* stack){
    if (stack== NULL){
        return;
    }
    stack->head.data = '\0';
    stack->head.next = NULL;
}
LinkNode* Create(LinkType value){
    LinkNode* new_node = (LinkNode*)malloc(sizeof(LinkNode));
    new_node->data = value;
    new_node->next = NULL;
    return new_node;
}
void LinkStackPush(LinkStack* stack, LinkType value){
    if (stack == NULL ){
        return;
    }
    LinkNode* new_node = Create(value);
    new_node->next = stack->head.next;//链表采用头插的方式
    stack->head.next = new_node;
}
void LinkNodeDestroy(LinkNode* node){
    free(node);
}
void LinkStackPop(LinkStack* stack){
    if (stack == NULL){
        return;
    }
    if (stack->head.next == NULL){
        return;
    }
    LinkNode* to_delete = stack->head.next;
    stack->head.next=to_delete->next;
    LinkNodeDestroy(to_delete);
}

int LinkStackFindTop(LinkStack* stack, LinkType* value){
    if (stack == NULL){
        return;
    }
    if (stack->head.next == NULL){
        return 0;
    }
    *value = stack->head.next->data;
    return 1;
}
void LinkStackDestroy(LinkStack* stack){
    LinkType tmp;
    while (LinkStackFindTop(stack, &tmp)){
        LinkStackPop(stack);
    }
}
void LinkStackDisplay(LinkStack* stack){
    if (stack == NULL){
        return;
    }
    printf("[栈顶]");
    LinkNode* cur = stack->head.next;
    while(cur != NULL){
        printf("%c ", cur->data);
        cur = cur->next;
    }
    printf("[栈底]");
}

基于顺序表实现的队列

#include"Queue.h"
void QueueInit(SeqQueue* seq){
    if (seq == NULL){
        return;
    }
    seq->_size = 0;
    //通过size来区分那部分是无关的
}
void QueuePush(SeqQueue* seq, QueueType value){
    if (seq == NULL){
        return;
    }
    seq->data[seq->_size++] = value;
}
void QueuePop(SeqQueue* seq){
    if (seq == NULL){
        return;
    }
    if (seq->_size == 0){
        return;
    }
    seq->_size--;
}
int FindHeadElem(SeqQueue* seq, QueueType* value){
    if (seq == NULL){
        return 0;
    }
    if (seq->_size == 0){
        return 0;
    }
    *value = seq->data[0];
    return 1;
}
int FindTailElem(SeqQueue* seq, QueueType* value){
    if (seq == NULL){
        return 0;
    }
    if (seq->_size == 0){
        return 0;
    }
    *value = seq->data[seq->_size-1];
    return 1;
}
void QueueDestroy(SeqQueue* seq){
    if (seq == NULL){
        return;
    }
    seq->_size = 0;
}
size_t  SeqQueueSize(SeqQueue* seq){
    if (seq == NULL){
        return (size_t )-1;
    }
    return seq->_size;
}

基于链表实现的队列

#include"Queue.h"
void QueueInit(LinkQueue* LQ){
    if (LQ == NULL){
        return;
    }
    LQ->head = NULL;
    LQ->tail = NULL;
}
LinkQueueNode* CreateLinkNode(LinkQueueType value){
    LinkQueueNode* ptr = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
    ptr->data = value;
    ptr->next = NULL;
}
void QueuePush(LinkQueue* LQ, LinkQueueType value){
    if (LQ == NULL){
        return;
    }
    if (LQ->head == NULL){
        LQ->head = LQ->tail =CreateLinkNode(value);
        return;
    }
    LQ->tail->next = CreateLinkNode(value);
    LQ->tail = LQ->tail->next;
}
void LinkQueueNodeDestroy(LinkQueueNode* ptr){
    free(ptr);
}
void QueuePop(LinkQueue* LQ){
    if (LQ == NULL){
        return;
    }
    if (LQ->head->next == NULL){
        LinkQueueNodeDestroy(LQ->head);
        LQ->tail =LQ->head= NULL;
        return;
    }
    LinkQueueNode* to_delete = LQ->head;
    LQ->head = to_delete->next;
    LinkQueueNodeDestroy(to_delete);

}
int FindHeadElem(LinkQueue* LQ,LinkQueueType* value){
    if (LQ == NULL){
        return 0;
    }
    if (LQ->head == NULL){
        return 0;
    }
    *value = LQ->head->data;
    return 1;
}
int FindTailElem(LinkQueue* LQ, LinkQueueType* value){
    if (LQ == NULL){
        return 0;
    }
    if (LQ->head == NULL){
        return 0;
    }
    *value = LQ->tail->data;
    return 1;
}
void QueueDestroy(LinkQueue* LQ){
    if (LQ == NULL){
        return;
    }
    LQ->head = LQ->tail = NULL;
}

void LQDisplay(LinkQueue*LQ){
    if (LQ->head == NULL){
        return;
    }
    LinkQueueNode* res = LQ->head;
    printf("队首");
    while (res != NULL){
        printf("%c ", res->data);
        res = res->next;
    }
    printf("队尾");
}

猜你喜欢

转载自blog.csdn.net/cx2479750196/article/details/80493848