用两个栈(C++)实现插入排序
2017-10-17 16:46
435 查看
用栈实现插入排序时,我们先将存放该数据的栈排序到另一个栈中,最后在将另外一个栈的内容倒放到当前栈中。图如下:
实现:
#pragma once
template <typename E> class AStack {
private:
int maxSize; // Maximum size of stack
int top; // Index for top element
E *listArray; // Array holding stack elements
public:
AStack(int size = 20) // Constructor
{
maxSize = size; top = 0; listArray = new E[size];
}
~AStack() { delete[] listArray; } // Destructor
void clear() { top = 0; } // Reinitialize
void push(const E& it) { // Put "it" on stack
listArray[top++] = it;
}
E pop() { // Pop top element
return listArray[--top];
}
const E& topValue() const { // Return top element
return listArray[top - 1];
}
int length() const { return top; } // Return length
void insertSort()
{
AStack<int>L1;
while (length()>0) //when this is empty,circle break.
{
E element = pop();
int count = 0; // to record how many element in this stack, which has been pushed into.
if (L1.length() == 0) //if L1 is empty, push element into 12.
{
L1.push(element);
}
else
{
if (element > L1.topValue())
L1.push(element);
else
{
while (element < L1.topValue()&&L1.length()!=0)
{
push(L1.pop()); //make L1's elements into the current stack when they are minor to the element.
count++;
}
L1.push(element);
while (count != 0)
{
L1.push(pop());
count--;
}
}
}
}
for (int i = 0; L1.length() > 0; i++)
push(L1.pop());
}
};
main函数:
#include"AStack.h"
#include<iostream>
using namespace std;
int main()
{
AStack<int>L1;
L1.push(2);
L1.push(1);
L1.push(9);
L1.push(5);
L1.push(7);
L1.push(11);
L1.push(35);
L1.push(0);
L1.insertSort();
while (L1.length() > 0)
cout << L1.pop() << endl;
}
截屏:
实现:
#pragma once
template <typename E> class AStack {
private:
int maxSize; // Maximum size of stack
int top; // Index for top element
E *listArray; // Array holding stack elements
public:
AStack(int size = 20) // Constructor
{
maxSize = size; top = 0; listArray = new E[size];
}
~AStack() { delete[] listArray; } // Destructor
void clear() { top = 0; } // Reinitialize
void push(const E& it) { // Put "it" on stack
listArray[top++] = it;
}
E pop() { // Pop top element
return listArray[--top];
}
const E& topValue() const { // Return top element
return listArray[top - 1];
}
int length() const { return top; } // Return length
void insertSort()
{
AStack<int>L1;
while (length()>0) //when this is empty,circle break.
{
E element = pop();
int count = 0; // to record how many element in this stack, which has been pushed into.
if (L1.length() == 0) //if L1 is empty, push element into 12.
{
L1.push(element);
}
else
{
if (element > L1.topValue())
L1.push(element);
else
{
while (element < L1.topValue()&&L1.length()!=0)
{
push(L1.pop()); //make L1's elements into the current stack when they are minor to the element.
count++;
}
L1.push(element);
while (count != 0)
{
L1.push(pop());
count--;
}
}
}
}
for (int i = 0; L1.length() > 0; i++)
push(L1.pop());
}
};
main函数:
#include"AStack.h"
#include<iostream>
using namespace std;
int main()
{
AStack<int>L1;
L1.push(2);
L1.push(1);
L1.push(9);
L1.push(5);
L1.push(7);
L1.push(11);
L1.push(35);
L1.push(0);
L1.insertSort();
while (L1.length() > 0)
cout << L1.pop() << endl;
}
截屏:
相关文章推荐
- c++实现一个比较两个string类型的版本号的小demo
- 两个栈来实现一个队列的C++代码
- 插入排序(正序、倒序)-c++代码实现及运行实例结果
- 两个栈来实现一个队列的C++代码(某公司社会早笔试题)
- 剑指offer第三十三题【两个链表的第一个公共结点】c++实现
- C++实现的插入排序
- 算法 插入排序 C++实现
- c++实现两个大整数相加(一)
- 用C++实现单件模式【包含两个char*变量】
- 用两个队列实现栈(C++模板)
- linux c/c++ 编程之-----创建管道文件,并实现两个进程间的信息传递。
- C++ 用两个栈实现一个队列
- C/C++实现插入排序
- 插入排序(正序、倒序)-c++代码实现及运行实例结果
- 【c/c++】完成端口服务器中转实现两个客户端之间通信
- C++实现两个已经排序的链表进行合并
- 左神的书——《程序员代码面试指南》之打印两个有序链表的公共部分 c++实现
- C++实现两个字符串之间的Levenshtein Distance(编辑距离)
- C++面试题:两个队列实现一个栈