c语言中的stack和queue

stack

#include <stack>

stack<int> s;

s.push(a);

while(!s.empty)

{

    cout<<s.top();

    s.pop();

}

 

int size=s.size();

queue

#include <queue>

queue<string> Q;

 

Q.push(s);

while(!Q.empty())

{

    cout<<Q.front();

    Q.pop();

}

 

cout << "The first element is " << Q.front()

     << " and the last element is " << Q.back() << endl;

 

int size=Q.size();

  • 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(&quot;Queue is full\n&quot;); return; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; } int dequeue(Queue *q) { if (isQueueEmpty(q)) { printf(&quot;Queue is empty\n&quot;); 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(&quot;Queue is empty\n&quot;); 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(&quot;Stack is full\n&quot;); return; } s->top++; s->data[s->top] = x; } int pop(Stack *s) { if (isStackEmpty(s)) { printf(&quot;Stack is empty\n&quot;); 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(&quot;Stack is empty\n&quot;); 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、付费专栏及课程。

余额充值