数据据结构(C++与C#比较)
栈
概念
Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构
C++
// 引入头文件
#include <stack>
常用函数
函数名 | 作用 |
---|---|
push | 入栈 |
pop | 出栈 |
top | 返回栈顶元素 |
empty | 检查栈是否为空 |
size | 返回栈中元素数量 |
例子
//定义一个栈
stack<int> s;
//向栈中压入10个元素(1~10)
for (int i = 1; i <= 10; i++)
{
s.push(i);
}
//输出
while(!s.empty())
{
cout << s.top() << endl;
s.pop();
}
C#
//引入命名空间
using System.Collections.Generic;
常用函数
函数名 | 作用 |
---|---|
push | 入栈 |
pop | 出栈 |
peek | 弹出元素,但不删除 |
count | 弹出元素,且删除 |
clear | 清空栈 |
ToArray | 复制 Stack 到一个新的数组中 |
Contains | 判断某个元素是否在 Stack 中 |
例子
//定义一个栈对象
Stack<int> s = new Stack<int>();
//压入10个元素(1~10)
for(int i=1;i<=10;i++)
{
s.Push(i);
}
//输出
while(s.Count !=0)
{
Console.WriteLine(s.Pop());
}
队列
概念
队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构。
C++
// 引入头文件
#include <queue>
常用函数
函数名 | 作用 |
---|---|
push | 入队 |
pop | 出队 |
front | 返回第一个元素 |
back | 返回最后一个元素 |
empty | 检查队列是否为空 |
size | 返回队列中元素的个数 |
例子
//定义一个队列
std::queue<int> q;
//入队10个元素(1~10)
for (int i = 1; i <= 10; i++)
{
q.push(i);
}
//翻转队列
//定义一个栈
std::stack<int> s;
while (!q.empty())
{
s.push(q.front());//
q.pop();
}
while (!s.empty())
{
q.push(s.top());
s.pop();
}
C#
//引入命名空间
using System.Collections.Generic;
常用函数
函数名 | 作用 |
---|---|
Clear | 清除所有元素 |
Contains | 判断某个元素是否在 Queue 中 |
Dequeue | 出队 |
Enqueue | 入队 |
ToArray | 复制 Queue 到一个新的数组中 |
TrimToSize | 设置容量为 Queue 中元素的实际个数 |
例子
//定义一个队列对象
Queue<int> q = new Queue<int>();
//入队10个元素(1~10)
for (int i = 1; i <= 10; i++)
{
q.Enqueue(i);
}
//输出
while (q.Count != 0)
{
Console.WriteLine(q.Dequeue());
}
两个队列实现栈
//StackTo2Queue.h
#include <queue>
#include <iostream>
class StackTo2Queue
{
public:
/*
**push** | 入栈
* *pop** | 出栈
* *top** | 返回栈顶元素
* *empty** | 检查栈是否为空
* *size** | 返回栈中元素数量
*/
void Push(int value);
void Pop();
int& Top();
bool empty();
int size();
private:
std::queue<int> mainQueue;
std::queue<int> tempQueue;
};
//StackTo2Queue.cpp
#include "StackTo2Queue.h"
void StackTo2Queue::Push(int value)
{
mainQueue.push(value);
}
void StackTo2Queue::Pop()
{
while(size() !=1)
{
//留一个元素在mainQueue中,其余放入TempQueue
tempQueue.push(mainQueue.front());
mainQueue.pop();
}
//删除mainQueue中的那一个元素
mainQueue.pop();
//把tempQueue中的元素重新放入mainQueue中
while (!tempQueue.empty())
{
mainQueue.push(tempQueue.front());
tempQueue.pop();
}
}
int & StackTo2Queue::Top()
{
return mainQueue.back();
}
bool StackTo2Queue::empty()
{
return mainQueue.empty();
}
int StackTo2Queue::size()
{
return mainQueue.size();
}
//测试
StackTo2Queue* stack = new StackTo2Queue();
for (int i = 1; i <= 10; i++)
{
stack->Push(i);
}
while (!stack->empty())
{
cout << stack->Top() << endl;
stack->Pop();
}
两个栈实现队列
//QueueTo2Stack
#include<iostream>
#include<stack>
#include<queue>
class QueueTo2Stack
{
//入队
void push(const int& val)
{
pushSt.push(val);
}
//出队
void pop()
{
//从进入栈出栈到输出栈
while (!pushSt.empty())
{
popSt.push(pushSt.top());
pushSt.pop();
}
//删除输出栈第一个元素
popSt.pop();
//输出栈的数据压入进入栈
while (!popSt.empty())
{
pushSt.push(popSt.top());
popSt.pop();
}
}
//判断是否为空
bool empty()
{
if (pushSt.empty())
{
return true;
}
else
{
return false;
}
}
//返回第一个元素的引用
int& front()
{
while (!pushSt.empty())
{
popSt.push(pushSt.top());
pushSt.pop();
}
int& n = popSt.top();
while (!popSt.empty())
{
pushSt.push(popSt.top());
popSt.pop();
}
return n;
}
//返回最后一个元素
int& back()
{
return pushSt.top();
}
//求大小
int size()
{
return pushSt.size();
}
}
//测试
int main()
{
//MyQueue* q = new MyQueue();
QueueTo2Stack q;
for (int i = 0; i < 4; i++)
{
q.push(i);
}
q.display();
cout << endl;
q.pop();
cout << "pop" << endl;
q.display();
cout << endl;
system("pause");
return 0;
}
来源:oschina
链接:https://my.oschina.net/u/4375351/blog/4494635