📘
Introduction_to_Algorithms
  • Introduction
  • 第6章:堆排序
    • 练习题
    • 思考题
      • 6-3 Young
  • 第7章:快速排序
    • 练习题
    • 思考题
      • 7-6 对区间的模糊排序
  • 第8章:线性时间排序
    • 练习题
      • 8.3-4 O(n)时间内对[0..n^-1]之间的n个数排序
    • 思考题
      • 8-3 排序不同长度的数据项
  • 第9章 排序和顺序统计学算法导论
    • 练习题
      • 9.1-1
      • 9.3-3 快速排序-最坏时间O(nlgn)
      • 9.3-6 nlgk时间求k分位数
      • 9.3-8 求两个数组的中位数
    • 思考题
      • 9-2-c-带权中位数
  • 第10章:栈和队列
    • 10.1 栈和队列
      • 练习题
      • 10.1-2 用一个数组实现两个栈
      • 10.1-5 双端队列
    • 10.2 链表
      • 练习题
      • 10.2-5 环形链表实现字典操作INSERT、DELETE、SEARCH
    • 10.3 指针和对象实现
      • 练习题
      • 10.3-4 紧凑的多重数组
    • 思考题
  • 第11章 散列表
  • [第12章]
    • [笔记和代码]
      • 12-2 基数树
  • 第14章
    • 笔记和代码
      • 14.3-6 MIN GAP
  • 第15章:动态规划
    • 练习题
    • 思考题
      • 15-2 整齐打印
      • 15-3 编辑距离
  • 第20章:斐波那契堆
    • 练习题
    • 思考题
  • ACM解题报告
    • 1043 八数码问题
    • 1053 Entropy
    • 1114 Piggy-Bank 完全背包
    • 1133 catalan数二维变种
    • 1166 敌兵布阵
    • 1281 棋盘游戏 二分匹配与增广链
    • 1394 Minimum Inversion Number
    • 1394 用线段树求逆序数
    • 1787 欧拉函数之在线算法
    • 2438 三分查找
    • 2473 Junk-Mail Filter 并查集的删除
    • 2824 欧拉公式之打表算法
    • 2855 Fibonacci Check-up 矩阵的应用
    • 2642 Stars 二维树状数组
    • 2888 二维RMQ
    • 3033 分组背包之每组至少选一
    • 3307 A^B = C mod D,已知A,C,D,求解B
    • 3465 Life is a Line 用归并排序求逆序数
    • 3483 A Very Simple Problem 数论+矩阵的应用
    • 3518 后缀数组
    • 3579 中国剩余定理(ACM/不互质的情况)
    • 后缀数组
    • B-树
    • 排序算法总结
Powered by GitBook
On this page
  • 一、概念
  • 1.栈
  • 2.队列
  • 二、代码
  1. 第10章:栈和队列

10.1 栈和队列

一、概念

1.栈

(1)栈实现了后进先出操作。 在栈的数组实现中,栈顶指针指向栈顶元素,插入时先修改指针再插入,删除时先取栈顶元素再修改指针。 当top[S]=0时,栈中空的。

(2)数组栈的结构: int top;//栈顶指针 int *s];//指向栈数组

(3)在栈上实现的操作 STACK-EMPTY(S)//判断栈是否为空 PUSH(S, x) //把x压入到栈顶 POP(S) //取出并返回栈顶元素

2.队列

(1)队列实现了先进先出操作。 在队列的数组实现中,队列的头指针指向队列首元素,删除时先取队列首元素再修改指针,队列的尾指针指向队尾元素的下一个元素,插入时先插入再修改指针 当top[S]=0时,栈中空的。

(2)数组队列的结构: int tail;//队列尾,指向最新进入的元素 int head;//队列头,指向最先出的元素 int length;//队列的长度 int *s;//指向数组队列

(3)在队列上实现的操作 ENQUEUE(Q, x) //把x插入到队列尾 DEQUEUE(Q) //取出队列首元素并返回

二、代码

//10.1栈和队列
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

/*****************栈*******************************************************/
struct stack
{
    int top;//栈顶指针
    int *s;//数组
    stack(int size):top(0){s = new int[size+1];}
//    ~stack(){delete []s;}
};
void Print(stack S)
{
    int i;
    //从栈底到栈顶的顺序输出
    for(i = 1; i <= S.top; i++)
        cout<<S.s[i]<<' ';
    cout<<endl;
}
//检查一个栈是否为空
bool Stack_Empty(stack &S)
{
    if(S.top == 0)
        return true;
    else
        return false;
}
//入栈
void Push(stack &S, int x)
{
    S.top++;
    S.s[S.top] = x;
}
//出栈
int Pop(stack &S)
{
    if(Stack_Empty(S))
    {
        cout<<"underflow"<<endl;
        exit(0);
    }
    else
    {
        S.top--;
        return S.s[S.top+1];
    }
}
/*****************队列********************************************************/
struct queue
{
    int tail;//队列头指针
    int head;//队列尾指针
    int length;//队列长度
    int *s;//数组
    queue(int size):tail(1),head(1),length(size){s = new int[size+1];}
};
//从队列头到队列尾输出
void Print(queue Q)
{
    int i;
    if(Q.tail >= Q.head)
    {
        for(i = Q.head; i < Q.tail;i++)
            cout<<Q.s[i]<<' ';
        cout<<endl;
    }
    //因为循环的原因,队列尾可能在队列头的前面
    else
    {
        for(i = Q.head; i <= Q.length; i++)
            cout<<Q.s[i]<<' ';
        for(i = 1; i < Q.tail; i++)
            cout<<Q.s[i]<<' ';
        cout<<endl;
    }
}
//判断队列是否为空
bool Queue_Empty(queue Q)
{
    if(Q.tail == Q.head)
        return 1;
    return 0;
}
//入队列
void Enqueue(queue &Q, int x)
{
    Q.s[Q.tail] = x;
    if(Q.tail == Q.length)
        Q.tail = 1;
    else Q.tail++;
}
//出队列
int Dequeue(queue &Q)
{
    int x = Q.s[Q.head];
    if(Q.head == Q.length)
        Q.head = 1;
    else Q.head++;
    return x;
}
Previous第10章:栈和队列Next练习题

Last updated 5 years ago