zoukankan      html  css  js  c++  java
  • 3、【线性结构】栈、队列

    一、栈的介绍

    栈(stack),是一种线性存储结构,它有以下几个特点:
      (1) 栈中数据是按照"后进先出(LIFO, Last In First Out)"方式进出栈的。
      (2) 向栈中添加/删除数据时,只能从栈顶进行操作。

    栈通常包括的三种操作:push、peek、pop。
      push -- 向栈中添加元素。
      peek -- 返回栈顶元素。
      pop  -- 返回并删除栈顶元素的操作。

    二、C++ 实现栈

    C++的STL中本身就包含了stack类,基本上该stack类就能满足我们的需求,所以很少需要我们自己来实现。本部分介绍2种C++实现。
      1. C++实现一:数组实现的栈,能存储任意类型的数据。
      2. C++实现二:C++的 STL 中自带的"栈"(stack)的示例。

    1、数组实现的栈,能存储任意类型的数据

    ArrayStack.h

     1 #ifndef ARRAY_STACK_HXX
     2 #define ARRAY_STACK_HXX
     3 
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 template<class T> class ArrayStack{
     9     public:
    10         ArrayStack();
    11         ~ArrayStack();
    12 
    13         void push(T t);
    14         T peek();
    15         T pop();
    16         int size();
    17         int isEmpty();
    18     private:
    19         T *arr;
    20         int count;
    21 };
    22 
    23 // 创建“栈”,默认大小是12
    24 template<class T>
    25 ArrayStack<T>::ArrayStack()
    26 {
    27     arr = new T[12];
    28     if (!arr)
    29     {
    30         cout<<"arr malloc error!"<<endl;
    31     }
    32 }
    33 
    34 // 销毁“栈”
    35 template<class T>
    36 ArrayStack<T>::~ArrayStack()
    37 {
    38     if (arr)
    39     {
    40         delete[] arr;
    41         arr = NULL;
    42     }
    43 }
    44 
    45 // 将val添加到栈中
    46 template<class T>
    47 void ArrayStack<T>::push(T t)
    48 {
    49     //arr[count++] = val;
    50     arr[count++] = t;
    51 }
    52 
    53 // 返回“栈顶元素值”
    54 template<class T>
    55 T ArrayStack<T>::peek()
    56 {
    57     return arr[count-1];
    58 }
    59 
    60 // 返回“栈顶元素值”,并删除“栈顶元素”
    61 template<class T>
    62 T ArrayStack<T>::pop()
    63 {
    64     int ret = arr[count-1];
    65     count--;
    66     return ret;
    67 }
    68 
    69 // 返回“栈”的大小
    70 template<class T>
    71 int ArrayStack<T>::size()
    72 {
    73     return count;
    74 }
    75 
    76 // 返回“栈”是否为空
    77 template<class T>
    78 int ArrayStack<T>::isEmpty()
    79 {
    80     return size()==0;
    81 }
    82 
    83 #endif

    main.cpp

     1 #include <iostream>
     2 #include "ArrayStack.h"
     3 using namespace std;
     4 
     5 int main() 
     6 {
     7     int tmp=0;
     8     ArrayStack<int> *astack = new ArrayStack<int>();
     9 
    10     cout<<"main"<<endl;
    11 
    12     // 将10, 20, 30 依次推入栈中
    13     astack->push(10);
    14     astack->push(20);
    15     astack->push(30);
    16 
    17     // 将“栈顶元素”赋值给tmp,并删除“栈顶元素”
    18     tmp = astack->pop();
    19     cout<<"tmp="<<tmp<<endl;
    20 
    21     // 只将“栈顶”赋值给tmp,不删除该元素.
    22     tmp = astack->peek();
    23 
    24     astack->push(40);
    25 
    26     while (!astack->isEmpty())
    27     {
    28         tmp = astack->pop();
    29         cout<<tmp<<endl;
    30     }
    31 
    32     return 0;
    33 }

    说明关于"栈的声明和实现都在头文件中"的原因,是因为栈的实现利用了C++模板,而"C++编译器不能支持对模板的分离式编译"。需要说明的是,采用C++模板实现的;但是,默认数组的大小只有12,而且该实现不支持动态扩展。

    2. C++的 STL 中自带的"栈"(stack)的示例

    mian.cpp

     1 #include <iostream>
     2 #include <stack>
     3 using namespace std;
     4 
     5 /**
     6  * C++ 语言: STL 自带的“栈”(stack)的示例。
     7  *
     8  * @author skywang
     9  * @date 2013/11/07
    10  */
    11 int main ()
    12 {
    13     int tmp=0;
    14     stack<int> istack;
    15 
    16     // 将10, 20, 30 依次推入栈中
    17     istack.push(10);
    18     istack.push(20);
    19     istack.push(30);
    20 
    21     // 将“栈顶元素”赋值给tmp,并删除“栈顶元素”
    22     istack.pop();
    23 
    24     // 只将“栈顶”赋值给tmp,不删除该元素.
    25     tmp = istack.top();
    26 
    27     istack.push(40);
    28 
    29     while (!istack.empty())
    30     {
    31         tmp = istack.top();
    32         istack.pop();
    33         cout<<tmp<<endl;
    34     }
    35 
    36     return 0;
    37 }

    三、队列的介绍

    队列(Queue),是一种线性存储结构。它有以下几个特点:
      (1) 队列中数据是按照"先进先出(FIFO, First-In-First-Out)"方式进出队列的。
      (2) 队列只允许在"队首"进行删除操作,而在"队尾"进行插入操作。
    队列通常包括的两种操作:入队列 和 出队列。

    四、队列的C++实现

    C++的STL中本身就包含了list类,基本上该list类就能满足我们的需求,所以很少需要我们自己来实现。本部分介绍2种C++实现。
    1. 数组实现的队列,能存储任意类型的数据。

    ArrayQueue.h

     1 #ifndef ARRAY_QUEUE_HXX
     2 #define ARRAY_QUEUE_HXX
     3 
     4 #include <iostream>
     5 using namespace std;
     6 
     7 template<class T> class ArrayQueue{
     8     public:
     9         ArrayQueue();
    10         ~ArrayQueue();
    11 
    12         void add(T t);
    13         T front();
    14         T pop();
    15         int size();
    16         int is_empty();
    17 
    18     private:
    19         T *arr;
    20         int count;
    21 };
    22 
    23 // 创建“队列”,默认大小是12
    24 template<class T>
    25 ArrayQueue<T>::ArrayQueue()
    26 {
    27     arr = new T[12];
    28     if (!arr)
    29     {
    30         cout<<"arr malloc error!"<<endl;
    31     }
    32 }
    33 
    34 // 销毁“队列”
    35 template<class T>
    36 ArrayQueue<T>::~ArrayQueue()
    37 {
    38     if (arr)
    39     {
    40         delete[] arr;
    41         arr = NULL;
    42     }
    43 }
    44 
    45 // 将val添加到队列的末尾
    46 template<class T>
    47 void ArrayQueue<T>::add(T t)
    48 {
    49     arr[count++] = t;
    50 }
    51 
    52 
    53 // 返回“队列开头元素”
    54 template<class T>
    55 T ArrayQueue<T>::front()
    56 {
    57     return arr[0];
    58 }
    59 
    60 // 返回并删除“队列末尾的元素”
    61 template<class T>
    62 T ArrayQueue<T>::pop()
    63 {
    64     int i = 0;;
    65     T ret = arr[0];
    66 
    67     count--;
    68     while (i++<count)
    69         arr[i-1] = arr[i];
    70 
    71     return ret;
    72 }
    73 
    74 // 返回“队列”的大小
    75 template<class T>
    76 int ArrayQueue<T>::size()
    77 {
    78     return count;
    79 }
    80 
    81 // 返回“队列”是否为空
    82 template<class T>
    83 int ArrayQueue<T>::is_empty()
    84 {
    85     return count==0;
    86 }
    87 
    88 
    89 #endif

    main.cpp

     1 #include <iostream>
     2 #include "ArrayQueue.h"
     3 using namespace std;
     4 
     5 
     6 int main()
     7 {
     8     int tmp=0;
     9     ArrayQueue<int> *astack = new ArrayQueue<int>();
    10 
    11     // 将10, 20, 30 依次推入队列中
    12     astack->add(10);
    13     astack->add(20);
    14     astack->add(30);
    15 
    16     // 将“队列开头元素”赋值给tmp,并删除“该元素”
    17     tmp = astack->pop();
    18     cout<<"tmp="<<tmp<<endl;
    19 
    20     // 只将“队列开头的元素”赋值给tmp,不删除该元素.
    21     tmp = astack->front();
    22     cout<<"tmp="<<tmp<<endl;
    23 
    24     astack->add(40);
    25 
    26     cout<<"is_empty()="<<astack->is_empty()<<endl;
    27     cout<<"size()="<<astack->size()<<endl;
    28     while (!astack->is_empty())
    29     {
    30         tmp = astack->pop();
    31         cout<<tmp<<endl;
    32     }
    33 
    34     return 0;
    35 }

    2.C++的 STL 中自带的"队列"(list)的示例。

    main.cpp

     1 #include <iostream>
     2 #include <queue>
     3 using namespace std;
     4 
     5 /**
     6  * C++ : STL中的队列(queue)的演示程序。
     7  *
     8  * @author skywang
     9  * @date 2013/11/07
    10  */
    11 int main ()
    12 {
    13     int tmp=0;
    14     queue<int> iqueue;
    15 
    16     // 将10, 20, 30 依次加入队列的末尾
    17     iqueue.push(10);
    18     iqueue.push(20);
    19     iqueue.push(30);
    20 
    21     // 删除队列开头的元素
    22     iqueue.pop();
    23 
    24     // 将“队列开头的元素”赋值给tmp,不删除该元素.
    25     tmp = iqueue.front();
    26     cout<<"tmp="<<tmp<<endl;
    27 
    28     // 将40加入到队列的末尾
    29     iqueue.push(40);
    30 
    31     cout << "empty()=" << iqueue.empty() <<endl;
    32     cout << "size()=" << iqueue.size() <<endl;
    33     while (!iqueue.empty()) 
    34     {
    35         tmp = iqueue.front();
    36         cout<<tmp<<endl;
    37         iqueue.pop();  
    38     }
    39 
    40     return 0;
    41 }
  • 相关阅读:
    How to create jar for Android Library Project
    Very large tabs in eclipse panes on Ubuntu
    64bit Ubuntu, Android AAPT, R.java
    Linux(Ubuntu)下如何安装JDK
    Configure xterm Fonts and Colors for Your Eyeball
    建立、配置和使用Activity——启动其他Activity并返回结果
    建立、配置和使用Activity——使用Bundle在Activity之间交换数据
    建立、配置和使用Activity——启动、关闭Activity
    建立、配置和使用Activity——Activity
    异步任务(AsyncTask)
  • 原文地址:https://www.cnblogs.com/Long-w/p/9777951.html
Copyright © 2011-2022 走看看