add SeqQueue.c

main
lwy 2 years ago
parent c3368fc6b2
commit f4f585d901

@ -0,0 +1,51 @@
#include <stdio.h>
#define QueueSize 100
typedef struct {
int data[QueueSize];
int front;
int rear;
} SeqQueue;
// 初始化队列
void initQueue(SeqQueue *queue) {
queue->front = queue->rear = 0; // 队列头指针和队列尾指针都指向数组下标为 0 的位置
}
// 判断队列是否为空
int queueEmpty(SeqQueue *queue) {
if (queue->front == queue->rear) {
return 1; // 队列为空
} else {
return 0; // 队列不为空
}
}
// 判断队列是否已满
int queueFull(SeqQueue *queue) {
if (queue->rear == QueueSize) {
return 1; // 队列已满
} else {
return 0; // 队列未满
}
}
// 入队
void enQueue(SeqQueue *queue, int x) {
if (queueFull(queue)) {
printf("Queue is full\n"); // 队列已满,无法插入新元素
return;
}
queue->data[queue->rear++] = x; // 将新元素插入到队列尾部,并将队列尾指针向后移动一个位置
}
// 出队
int deQueue(SeqQueue *queue) {
if (queueEmpty(queue)) {
printf("Queue is empty\n"); // 队列为空,无法删除元素
return -1;
}
int x = queue->data[queue->front++]; // 取出队列头元素,并将队列头指针向后移动一个位置
return x;
}

@ -1,42 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkStack
{
typedef struct LinkStack {
char data;
struct LinkStack *next;
} LinkStack;
LinkStack *push(LinkStack *top, char a)
{
LinkStack *line = (LinkStack *)malloc(sizeof(LinkStack));
LinkStack *push(LinkStack *top, char a) {
LinkStack *line = (LinkStack *) malloc(sizeof(LinkStack));
line->data = a;
line->next = top;
top = line;
return top;
}
LinkStack *pop(LinkStack *top)
{
if (top)
{
LinkStack *pop(LinkStack *top) {
if (top) {
LinkStack *p = top;
top = top->next;
printf("current = %c \n", p->data);
if (top)
{
printf("top = %c\n", top->data);
}
else
{
printf("old top = %c; ", p->data);
if (top) {
printf("new top = %c\n", top->data);
} else {
printf("empty\n");
}
free(p);
}
else
{
} else {
printf("Stack Empty\n");
return top;
}
return top;
}
int stackEmpty(LinkStack *top) {
if (top == NULL) {
return 1; //链栈为空
} else {
return 0; // 链栈不为空
}
}
char getTop(LinkStack* top) {
if (stackEmpty(top)) {
printf("stack empty");
exit(0);
} else {
return top->data;
}
}

@ -1,23 +1,49 @@
#include <stdio.h>
#include "LinearList/SeqList.c"
#include "LinearList/LinkList.c"
//#include "LinearList/SeqList.c"
//#include "LinearList/LinkList.c"
//#include "Stack/SeqStack.c"
#include "Stack/LinkStack.c"
//#include "Stack/LinkStack.c"
#include "Queue/SeqQueue.c"
int main() {
LinkStack *stack = NULL;
stack = push(stack, 'a');
stack = push(stack, 'b');
stack = push(stack, 'c');
stack = push(stack, 'd');
stack = pop(stack);
stack = pop(stack);
stack = pop(stack);
stack = pop(stack);
stack = pop(stack);
return 0;
//顺序队列
SeqQueue seqQueue;
initQueue(&seqQueue);
enQueue(&seqQueue,1);
enQueue(&seqQueue,2);
// deQueue(&seqQueue);
int x = deQueue(&seqQueue);
printf("Delete element: %d\n", x);
int y = deQueue(&seqQueue);
printf("Delete element: %d\n", y);
// 再次判断队列是否为空
if (queueEmpty(&seqQueue)) {
printf("Queue is empty\n");
} else {
printf("Queue is not empty\n");
}
//链栈
// LinkStack *stack = NULL;
// stack = push(stack, 'a');
// stack = push(stack, 'b');
// stack = push(stack, 'c');
// stack = push(stack, 'd');
//
// printf("top->data = %c\n",getTop(stack)); // d
// printf("top->data = %c\n",getTop(stack->next)); // c
//
// stack = pop(stack);
// stack = pop(stack);
// stack = pop(stack);
// stack = pop(stack);
// pop(stack);
// pop(stack);
// printf("%d", stackEmpty(stack));
//顺序栈
// SeqStack seqStack;
// initStack(&seqStack);
// push(&seqStack, 'a');

Loading…
Cancel
Save