diff --git a/Queue/SeqQueue.c b/Queue/SeqQueue.c new file mode 100644 index 0000000..4ff12cf --- /dev/null +++ b/Queue/SeqQueue.c @@ -0,0 +1,51 @@ +#include + +#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; +} \ No newline at end of file diff --git a/Stack/LinkStack.c b/Stack/LinkStack.c index 52c3a0d..5332ac1 100644 --- a/Stack/LinkStack.c +++ b/Stack/LinkStack.c @@ -1,42 +1,50 @@ #include #include -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; + } +} \ No newline at end of file diff --git a/main.c b/main.c index cf02599..8ea00d8 100644 --- a/main.c +++ b/main.c @@ -1,23 +1,49 @@ #include -#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');