队列和栈

1、用数组实现大小固定的队列和栈

 

2、实现特殊的栈,包含栈的基本功能,同时实现返回栈中最小元素操作

 

3、队列和栈的相互实现

 

4、转圈打印矩阵

 

5、旋转方形矩阵

 

6、之字形打印矩阵

 

7、行列都排序的矩阵中找数

 

转载于:https://www.cnblogs.com/Luckidmi/p/11489645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中可以使用数组或链表来实现队列。 1. 队列 使用数组实现队列,需要定义一个指向队列头的指针front和一个指向队列尾的指针rear,队列的长度为maxsize。具体实现如下: ``` #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int front, rear; } Queue; void initQueue(Queue *q) { q->front = q->rear = 0; } int isQueueFull(Queue *q) { return (q->rear + 1) % MAXSIZE == q->front; } int isQueueEmpty(Queue *q) { return q->front == q->rear; } void enqueue(Queue *q, int x) { if (isQueueFull(q)) { printf("Queue is full\n"); return; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; } int dequeue(Queue *q) { if (isQueueEmpty(q)) { printf("Queue is empty\n"); return -1; } int x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return x; } ``` 其中,initQueue函数用于初始化队列;isQueueFull函数和isQueueEmpty函数分别用于判断队列是否已满和是否为空;enqueue函数用于入队;dequeue函数用于出队。注意,在队列满或空时,需要对应处理。 使用链表实现队列,需要定义一个链表节点Node,以及一个指向队列头节点和队列尾节点的指针head和tail。具体实现如下: ``` typedef struct Node { int data; struct Node *next; } Node; typedef struct { Node *head, *tail; } Queue; void initQueue(Queue *q) { q->head = q->tail = NULL; } int isQueueEmpty(Queue *q) { return q->head == NULL; } void enqueue(Queue *q, int x) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = x; newNode->next = NULL; if (isQueueEmpty(q)) { q->head = q->tail = newNode; } else { q->tail->next = newNode; q->tail = newNode; } } int dequeue(Queue *q) { if (isQueueEmpty(q)) { printf("Queue is empty\n"); return -1; } int x = q->head->data; Node *temp = q->head; q->head = q->head->next; if (q->head == NULL) { q->tail = NULL; } free(temp); return x; } ``` 其中,initQueue函数用于初始化队列;isQueueEmpty函数用于判断队列是否为空;enqueue函数用于入队;dequeue函数用于出队。注意,在队列为空时,需要对应处理。 2. 使用数组实现,需要定义一个指向顶的指针top,的长度为maxsize。具体实现如下: ``` #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int top; } Stack; void initStack(Stack *s) { s->top = -1; } int isStackFull(Stack *s) { return s->top == MAXSIZE - 1; } int isStackEmpty(Stack *s) { return s->top == -1; } void push(Stack *s, int x) { if (isStackFull(s)) { printf("Stack is full\n"); return; } s->top++; s->data[s->top] = x; } int pop(Stack *s) { if (isStackEmpty(s)) { printf("Stack is empty\n"); return -1; } int x = s->data[s->top]; s->top--; return x; } ``` 其中,initStack函数用于初始化;isStackFull函数和isStackEmpty函数分别用于判断是否已满和是否为空;push函数用于入pop函数用于出。注意,在满或空时,需要对应处理。 使用链表实现,需要定义一个链表节点Node,以及一个指向顶节点的指针top。具体实现如下: ``` typedef struct Node { int data; struct Node *next; } Node; typedef struct { Node *top; } Stack; void initStack(Stack *s) { s->top = NULL; } int isStackEmpty(Stack *s) { return s->top == NULL; } void push(Stack *s, int x) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = x; newNode->next = s->top; s->top = newNode; } int pop(Stack *s) { if (isStackEmpty(s)) { printf("Stack is empty\n"); return -1; } int x = s->top->data; Node *temp = s->top; s->top = s->top->next; free(temp); return x; } ``` 其中,initStack函数用于初始化;isStackEmpty函数用于判断是否为空;push函数用于入pop函数用于出。注意,在为空时,需要对应处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值