diff options
| author | garhve <git@garhve.com> | 2022-12-05 19:43:39 +0800 | 
|---|---|---|
| committer | garhve <git@garhve.com> | 2022-12-05 19:43:39 +0800 | 
| commit | c6bc541ab58363d783e60a007e80e9bf9e231fda (patch) | |
| tree | a59c7ed0d05225c5876f3e5e919d4f6ed0c447ff /c/dataStructure/栈和队列/队列 | |
initialize
Diffstat (limited to 'c/dataStructure/栈和队列/队列')
| -rwxr-xr-x | c/dataStructure/栈和队列/队列/循环队列/main.c | 0 | ||||
| -rwxr-xr-x | c/dataStructure/栈和队列/队列/循环队列/queue.c | 53 | ||||
| -rwxr-xr-x | c/dataStructure/栈和队列/队列/循环队列/queue.h | 17 | ||||
| -rwxr-xr-x | c/dataStructure/栈和队列/队列/链队列/main.c | 28 | ||||
| -rwxr-xr-x | c/dataStructure/栈和队列/队列/链队列/queue.c | 75 | ||||
| -rwxr-xr-x | c/dataStructure/栈和队列/队列/链队列/queue.h | 23 | 
6 files changed, 196 insertions, 0 deletions
diff --git a/c/dataStructure/栈和队列/队列/循环队列/main.c b/c/dataStructure/栈和队列/队列/循环队列/main.c new file mode 100755 index 0000000..e69de29 --- /dev/null +++ b/c/dataStructure/栈和队列/队列/循环队列/main.c diff --git a/c/dataStructure/栈和队列/队列/循环队列/queue.c b/c/dataStructure/栈和队列/队列/循环队列/queue.c new file mode 100755 index 0000000..2c7f781 --- /dev/null +++ b/c/dataStructure/栈和队列/队列/循环队列/queue.c @@ -0,0 +1,53 @@ +#include "queue.h" + +bool InitQueue(Queue * q) +{ +    q->base = (int *) malloc (sizeof(int) * MAXSIZE); +    if (!q->base) +    { +        fprintf(stderr,"Failed to allocate memory!\n"); +        return false; +    } + +    q->front = q->rear = 0; +} + +bool EnQueue(Queue * q, int e) +{ +    if ((q->rear + 1)%MAXSIZE == q->front) +    { +        fprintf(stderr,"Queue is full!\n"); +        return false; +    } + +    q->base[q->rear] = e; +    q->rear = (q->rear + 1) % MAXSIZE; + +    return true; +} + +bool DeQueue(Queue * q, int * e) +{ +    if (q->front == q->rear) +    { +        fprintf(stderr,"Queue is empty"); +        return false; +    } + +    *e = q->base[q->front]; +    q->front = (q->front + 1) % MAXSIZE; + +    return true; +} + +int LengthQueue(Queue * q) +{ +    return (q->rear - q->front + MAXSIZE) % MAXSIZE; +} + +int GetHead(Queue * q) +{ +    if (q->front != q->rear) +        return q->base[q->front]; +    return -1; +}
\ No newline at end of file diff --git a/c/dataStructure/栈和队列/队列/循环队列/queue.h b/c/dataStructure/栈和队列/队列/循环队列/queue.h new file mode 100755 index 0000000..11806b5 --- /dev/null +++ b/c/dataStructure/栈和队列/队列/循环队列/queue.h @@ -0,0 +1,17 @@ +#include<stdio.h> +#include<stdlib.h> +#include<stdbool.h> + +#define MAXSIZE 5 + +typedef struct queue { +    int * base; +    int front; +    int rear; +} Queue; + +bool InitQueue (Queue * q); +bool EnQueue (Queue * q, int e); +bool DeQueue (Queue * q, int * e); +int  LengthQueue (Queue * q); +int  GetHead (Queue * q);
\ No newline at end of file diff --git a/c/dataStructure/栈和队列/队列/链队列/main.c b/c/dataStructure/栈和队列/队列/链队列/main.c new file mode 100755 index 0000000..db5ae9a --- /dev/null +++ b/c/dataStructure/栈和队列/队列/链队列/main.c @@ -0,0 +1,28 @@ +#include "queue.h" + +int main(void) +{ +    ElemType e; +    pqueue head, tail; +    InitQueue(&head); +    InitQueue(&tail); + +    while ((e = getchar()) != '\n') +    { +        EnQueue(&tail, &e); +        if (EmptyQueue(&head)) +            head = tail; +    } + +    PrintQueue(&head); +    ElemType retval; + +    DeQueue(&head, &retval); +    printf("'%c' quit queue\n",retval); + +    PrintQueue(&head); + +    RsQueue(&head); + +    return 0; +} diff --git a/c/dataStructure/栈和队列/队列/链队列/queue.c b/c/dataStructure/栈和队列/队列/链队列/queue.c new file mode 100755 index 0000000..94361d1 --- /dev/null +++ b/c/dataStructure/栈和队列/队列/链队列/queue.c @@ -0,0 +1,75 @@ +#include "queue.h" + +void InitQueue(pqueue * q) +{ +    *q = NULL; +} + +bool EmptyQueue(pqueue * qhead) +{ +    if (!*qhead) +        return true; +    return false; +} + +bool EnQueue(pqueue * qtail, ElemType * e) +{ +    pqueue new = (pqueue) malloc (sizeof(Queue)); +    if (!new) +        return false; +    new->data = *e; +    new->next = NULL; + +    if (!*qtail) +        *qtail = new; +    else +    { +        (*qtail)->next = new; +        *qtail = new; +    } + +    return true; +} + +bool DeQueue(pqueue * qhead, ElemType * e) +{ +    pqueue tmp = *qhead; +    if (!*qhead) +        return false; +    *e = tmp->data; +    *qhead = (*qhead)->next; +    free(tmp); + +    return true; +} + +bool RsQueue (pqueue * qhead) +{ +    if (EmptyQueue(qhead)) +        return false; +    pqueue tmp = *qhead; + +    while (!*qhead) +    { +        tmp = (*qhead)->next; +        free(*qhead); +        *qhead = tmp; +    } + +    return true; +} + +bool PrintQueue (pqueue * qhead) +{ +    if (EmptyQueue(qhead)) +        return false; +    pqueue tmp = *qhead; + +    while (tmp) +    { +        printf("%c", tmp->data); +        tmp = tmp->next; +    } +    putchar('\n'); +    return true; +} diff --git a/c/dataStructure/栈和队列/队列/链队列/queue.h b/c/dataStructure/栈和队列/队列/链队列/queue.h new file mode 100755 index 0000000..e444632 --- /dev/null +++ b/c/dataStructure/栈和队列/队列/链队列/queue.h @@ -0,0 +1,23 @@ +#ifndef _QUEUE_H +#define _QUEUE_H + +#include<stdio.h> +#include<stdlib.h> +#include<stdbool.h> + +typedef char ElemType; + +typedef struct queue { +    ElemType data; +    struct queue * next; +} Queue; +typedef Queue * pqueue; + +void InitQueue  (pqueue * q); +bool EnQueue    (pqueue * qtail, ElemType * e); +bool DeQueue    (pqueue * qhead, ElemType * e); +bool RsQueue    (pqueue * qhead); +bool EmptyQueue (pqueue * qhead); +bool PrintQueue (pqueue * qhead); + +#endif  | 
