利用顺序表实现栈利用单链表实现队列

xiaoxiao2021-02-28  19

利用顺序表实现栈

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>#include<stdlib.h>#include<stack>#define stack_max_num 10struct SeqStack{ int MAXNUM; int t;//始终指向栈顶 int *s;};typedef struct SeqStack *PSeqStack;/*创建一个空的堆栈,存储方式为顺序表*/PSeqStack createEmptyStack_seq(int m){ PSeqStack pastack = (PSeqStack)malloc(sizeof (struct SeqStack)); if (pastack == NULL){ printf("out of spcae!\n"); return NULL; } pastack->s = (int *)malloc(sizeof (int)*m); if (pastack == NULL){ printf("out of space!\n"); return NULL; } pastack->MAXNUM = m; pastack->t = -1; return pastack;}/*****************************************************//*判断堆栈是否为空*/int isEmptyStack_seq(PSeqStack pastack){ if (pastack->t == (-1)) return 1; return 0;}/*****************************************************//*将x压入栈中,成功返回1,否则返回0;*/int push_seq(PSeqStack pastack, int x){ if (pastack->t >= pastack->MAXNUM){ printf("overflow!\n"); return 0; } pastack->t = pastack->t + 1; pastack->s[pastack->t] = x; return 1;}/*****************************************************//*将栈顶元素出栈,成功返回1,否则返回0;*/int pop_seq(PSeqStack pastack){ if (pastack->t == (-1)){ printf("empty stack!\n"); return 0; } pastack->t = (pastack->t) - 1; return 1;}/*****************************************************//*获取栈顶元素,成功返回栈顶元素,否则返回-1;*/int top_seq(PSeqStack pastack){ if (pastack->t == (-1)){ printf("empty stack!\n"); return -1; } return pastack->s[pastack->t];}/*****************************************************//********************主函数***************************/int main(){ PSeqStack stack_get = createEmptyStack_seq(stack_max_num); push_seq(stack_get, 0); push_seq(stack_get, 1); push_seq(stack_get, 2); push_seq(stack_get, 3); pop_seq(stack_get); int from_top = top_seq(stack_get); printf("%d\n", from_top); return 0;

}

利用单链表实现队列 

#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include <stdlib.h>typedef struct _nodequeue_ { int data; struct _nodequeue_ *next;} nodequeue_st;typedef struct _linkqueue_ { int total; int current; nodequeue_st *head; nodequeue_st *tail;} linkqueue_st;linkqueue_st *linkqueue_init(int size);nodequeue_st *linkqueue_node_create(int value);int linkqueue_enqueue(linkqueue_st *queue, int value);int linkqueue_isfull(linkqueue_st *queue);int linkqueue_dequeue(linkqueue_st *queue, int *value);int linkqueue_isempty(linkqueue_st *queue);int linkqueue_free(linkqueue_st *queue);int main(void){ linkqueue_st *queue = NULL; int value = 100; int size = 10; queue = linkqueue_init(size); while (-1 != linkqueue_enqueue(queue, value)) value++; while (-1 != linkqueue_dequeue(queue, &value)) printf("]", value); putchar('\n'); linkqueue_free(queue); return 0;}linkqueue_st *linkqueue_init(int size){ linkqueue_st *queue; queue = (linkqueue_st *)malloc(sizeof(linkqueue_st)); queue->total = size; queue->current = 0; queue->head = linkqueue_node_create(0); queue->tail = queue->head; return queue;}nodequeue_st *linkqueue_node_create(int value){ nodequeue_st *node = NULL; node = (nodequeue_st *)malloc(sizeof(nodequeue_st)); node->data = value; node->next = NULL; return node;}int linkqueue_enqueue(linkqueue_st *queue, int value){ nodequeue_st *node = NULL; if (linkqueue_isfull(queue)) return -1; node = linkqueue_node_create(value); node->next = queue->tail->next; queue->tail->next = node; queue->tail = node; queue->current++; return 0;}int linkqueue_dequeue(linkqueue_st *queue, int *value){ nodequeue_st *tmp = NULL; if (linkqueue_isempty(queue)) return -1; tmp = queue->head; *value = tmp->next->data; queue->head = queue->head->next; free(tmp); queue->current--; return 0;}int linkqueue_isfull(linkqueue_st *queue){ if (queue->current == queue->total) return 1; return 0;}int linkqueue_isempty(linkqueue_st *queue){ if (queue->current == 0) return 1; return 0;}int linkqueue_free(linkqueue_st *queue){ nodequeue_st *node = queue->head; nodequeue_st *tmp = NULL; while (node != NULL) { tmp = node; node = node->next; free(tmp); } free(queue); return 0;}

转载请注明原文地址: https://www.6miu.com/read-2300226.html

最新回复(0)