一个数组实现三个栈的程序,是一个公司的面试题,思考了很久才写出来看来还是比较笨的。原创!!!!
第一个栈在左边,从数组左面开始增长,一个从数组中间开始向右增长,一个从数组右面开始向左增长。支持随意大小数组
模板支持整数,浮点数,字符。
// stack3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
//#define USE_TYPE int
#define USE_TYPE char
#define USE_TYPE float
#define USE_TYPE int
//元素类型
USE_TYPE Array[25]; //数组
/*
栈定义
第一个栈从左向右增长
第二个栈从中间开始向右增长 并对着两边元素的位置做相对的移动
第三个栈从右向左移动
*/
template <class T>
class Stack
{
public:
Stack(int num);
T Pop();
void Push(T value);
T * Stack::Pointer();
int Stack::lenth();
void LeftMove(int index);//中间的栈左移
void RightMove(int index);//中间的栈右移
//void Modify(int index);
private:
T * Pcurrent;
int len;
int index;
//int size;
};
template <class T>
int Stack<T>::lenth()
{
return len;
}
template <class T>
void Stack<T>::LeftMove(int index)
{
if (index == 0 || index == 1)
{
T *pstart = Pcurrent - len;
T * m = new T[len];
memcpy(m, pstart, len * sizeof(T));
memset(pstart, 0, len* sizeof(T));
memcpy(pstart - 1, m, len* sizeof(T));
Pcurrent -= 1;
delete []m;
}
if (index == 2)
{
T* m = new T[len];
memcpy(m, Pcurrent, len* sizeof(T));
memset(Pcurrent, 0, len* sizeof(T));
memcpy(Pcurrent - 1, m, len * sizeof(T));
Pcurrent -= 1;
delete []m;
}
}
template <class T>
void Stack<T>::RightMove(int index)
{
if (index == 0 || index == 1)
{
T *pstart = Pcurrent - len;
T * m = new T[len];;
memcpy(m, pstart, len * sizeof(T));
memset(pstart, 0,len * sizeof(T));
memcpy(pstart + 1, m, len * sizeof(T));
Pcurrent += 1;
delete[]m;
}
if (index == 2)
{
T * m = new T[len];
memcpy(m, Pcurrent, len * sizeof(T));
memset(Pcurrent, 0, len * sizeof(T));
memcpy(Pcurrent + 1, m, len * sizeof(T));
Pcurrent += 1;
delete[]m;
}
};
// 返回栈指针
template <class T>
T * Stack<T>::Pointer()
{
return Pcurrent;
}
/*
按照输入参数来初始化三个栈
第一个参数0 指针在左边
第二个参数1 指针在中间
第三个参数2 指针在右边
*/
template <class T>
Stack<T>::Stack(int num)
{
index = num;
len = 0;
if (num == 0)
{
Pcurrent = &(T)Array[0];
//size = sizeof(Array) / sizeof(int)/3;
}
else if (num == 1)
{
Pcurrent = &(T)Array[sizeof(Array) / sizeof(T) / 2 ];
//size = sizeof(Array)/ sizeof(int) - 2 * sizeof(Array) / sizeof(int) / 3;
}
else if (num == 2)
{
Pcurrent = &(T)Array[sizeof(Array) / sizeof(T) - 1];
//size = sizeof(Array) / sizeof(int) / 3;
}
else
{
printf("parameter error!\n");
}
}
//Stack stack1 = Stack(<int>1);
//Stack stack1 = Stack(1);
//Stack stack2 = Stack(2);
//出栈函数
template <class T>
T Stack<T>::Pop()
{
if (len > 0)
{
if (index == 0 || index == 1)
{
Pcurrent--;
T num = *Pcurrent;
*Pcurrent = 0;
return num;
}
if (index == 2)
{
//int num = *Pcurrent;
Pcurrent++;
T num = *Pcurrent;
*Pcurrent = 0;
return num;
}
else
{
return -1;
}
}
else
{
printf("stack index :%d no item to pop", index);
return -1;
}
}
//压栈函数
template <class T>
void Stack<T>::Push(T value)
{
if (index == 0 || index == 1)
{
*Pcurrent = value;
Pcurrent++;
len++;
}
else
{
*Pcurrent = value;
Pcurrent--;
len++;
}
}
Stack<USE_TYPE> stack0(0);
Stack<USE_TYPE> stack1(1);
Stack<USE_TYPE> stack2(2);
//判断栈一是否能压栈
bool Stack0CouldPush(Stack<USE_TYPE> stack0, Stack<USE_TYPE> stack1, Stack<USE_TYPE> stack2)
{
return (stack1.Pointer() - stack0.Pointer() >= 1);
}
//判断栈二是否能压栈
bool Stack1CouldPush(Stack<USE_TYPE> stack0, Stack<USE_TYPE> stack1, Stack<USE_TYPE> stack2)
{
return (stack2.Pointer() - stack1.Pointer() >= 0);
}
//判断栈三是否能压栈
bool Stack2CouldPush(Stack<USE_TYPE> stack0, Stack<USE_TYPE> stack1, Stack<USE_TYPE> stack2)
{
return (stack2.Pointer() - stack1.Pointer() >= 0);
}
//判断栈二是否应该向左移动 判断距离栈一和栈二指针的距离
bool Stack1NeedLeftMove(Stack<USE_TYPE> stack0, Stack<USE_TYPE> stack1, Stack<USE_TYPE> stack2)
{
//return ((int)(stack2.Pointer() - stack0.Pointer()) / 2 > (int)stack1.Pointer() + stack1.lenth());
int dis1 = ((int)stack2.Pointer() - (int)stack1.Pointer())/sizeof(USE_TYPE);
int dis2 = ((int)stack1.Pointer() - (int)stack1.lenth()* sizeof(USE_TYPE) - (int)stack0.Pointer())/ sizeof(USE_TYPE);
return (dis2 - dis1 > 0);
}
//判断栈二是否应该向右移动 判断距离栈一和栈二指针的距离
bool Stack1NeedRightMove(Stack<USE_TYPE> stack0, Stack<USE_TYPE> stack1, Stack<USE_TYPE> stack2)
{
int dis1 = ((int)stack2.Pointer() - (int)stack1.Pointer())/ sizeof(USE_TYPE);
int dis2 = ((int)stack1.Pointer() - (int)stack1.lenth()* sizeof(USE_TYPE) - (int)stack0.Pointer())/ sizeof(USE_TYPE);
//bool m = (((int)stack2.Pointer() + (int)stack0.Pointer()) / 2 < (int)(stack1.Pointer() + stack1.lenth()/2));
return (dis1 - dis2 > 0);
}
//判断栈二是否可以向左移动
bool Stack1CouldLeftMove(Stack<USE_TYPE> stack0, Stack<USE_TYPE> stack1, Stack<USE_TYPE> stack2)
{
int m = stack1.Pointer() - stack0.Pointer() - stack1.lenth();
return (stack1.Pointer() - stack0.Pointer() - stack1.lenth() >= 0);
}
//判断栈二是否可以向右移动
bool Stack1CouldRightMove(Stack<USE_TYPE> stack0, Stack<USE_TYPE> stack1, Stack<USE_TYPE> stack2)
{
int m = stack2.Pointer() - stack1.Pointer();
return (stack2.Pointer() - stack1.Pointer() >= 0);
}
//栈是否满了
bool Isfull()
{
bool m = (stack0.lenth() + stack1.lenth() + stack2.lenth() >= (sizeof(Array) / sizeof(USE_TYPE)));
return m;
}
/*
int Stack0push(Stack stack0, Stack stack1, Stack stack2,int value)
{
if (Stack0CouldPush(stack0, stack1, stack2))
{
stack0.Push(value);
}
}
int Stack1push(Stack stack0, Stack stack1, Stack stack2, int value)
{
if (Stack1CouldPush(stack0, stack1, stack2))
{
stack1.Push(value);
}
}
*/
//栈一压栈
void Stack0push(USE_TYPE value)
{
if (!Isfull())
{
if (Stack1NeedRightMove(stack0, stack1, stack2))
{
if (Stack1CouldRightMove(stack0, stack1, stack2))
stack1.RightMove(1);
}
/*
if (Stack1NeedLeftMove(stack0, stack1, stack2))
{
if (Stack1CouldLeftMove(stack0, stack1, stack2))
stack1.LeftMove(1);
}
*/
if (Stack0CouldPush(stack0, stack1, stack2))
{
stack0.Push(value);
}
else
{
if (Stack1NeedRightMove(stack0, stack1,stack2))
{
if (Stack1CouldRightMove(stack0, stack1, stack2))
stack1.RightMove(1);
}
if (Stack1NeedLeftMove(stack0, stack1, stack2))
{
if (Stack1CouldLeftMove(stack0, stack1, stack2))
stack1.LeftMove(1);
}
stack0.Push(value);
}
}
else
{
printf("stack is full!\n");
}
}
//栈二出栈
int Stack1pop()
{
return (stack1.Pop());
}
//栈三出栈
int Stack2pop()
{
return (stack2.Pop());
}
//栈一出栈
int Stack0pop()
{
return (stack0.Pop());
}
//栈二压栈
void Stack1push(USE_TYPE value)
{
if (!Isfull())
{
while(Stack1NeedRightMove(stack0, stack1, stack2))
{
if (Stack1CouldRightMove(stack0, stack1, stack2))
{
stack1.RightMove(1);
if (Stack1CouldPush(stack0, stack1, stack2))
{
stack1.Push(value);
return;
}
}
}
while(Stack1NeedLeftMove(stack0, stack1, stack2))
{
stack1.LeftMove(1);
if (Stack1CouldLeftMove(stack0, stack1, stack2))
{
if (Stack1CouldPush(stack0, stack1, stack2))
{
stack1.Push(value);
return;
}
}
}
if (Stack1CouldRightMove(stack0, stack1, stack2))
{
stack1.RightMove(1);
if (Stack1CouldPush(stack0, stack1, stack2))
{
stack1.Push(value);
return;
}
}
if (Stack1CouldLeftMove(stack0, stack1, stack2))
{
stack1.LeftMove(1);
if (Stack1CouldPush(stack0, stack1, stack2))
{
stack1.Push(value);
return;
}
}
}
else
{
printf("stack is full!\n");
}
}
//栈一压栈
void Stack2push(USE_TYPE value)
{
if (!Isfull())
{
/*
if (Stack1NeedRightMove(stack0, stack1, stack2))
{
if (Stack1CouldRightMove(stack0, stack1, stack2))
stack1.RightMove(1);
}
*/
if (Stack1NeedLeftMove(stack0, stack1, stack2))
{
if (Stack1CouldLeftMove(stack0, stack1, stack2))
stack1.LeftMove(1);
}
if (Stack2CouldPush(stack0, stack1, stack2))
{
stack2.Push(value);
}
else
{
if (Stack1NeedRightMove(stack0, stack1, stack2))
{
if (Stack1CouldRightMove(stack0, stack1, stack2))
stack1.RightMove(1);
}
if (Stack1NeedLeftMove(stack0, stack1, stack2))
{
if (Stack1CouldLeftMove(stack0, stack1, stack2))
stack1.LeftMove(1);
}
stack2.Push(value);
}
}
else
{
printf("stack is full!\n");
}
}
int main()
{
Stack0push(8);
Stack0push(8);
Stack0push(8);
Stack0push(8);
Stack0push(8);
Stack0push(8);
Stack0push(8);
Stack0push(8);
Stack0push(8);
Stack2push(2);
Stack2push(2);
Stack2push(2);
Stack2push(2);
Stack2push(2);
Stack2push(2);
Stack2push(2);
Stack2push(2);
Stack2push(2);
Stack2push(3);
Stack1push(1);
Stack1push(1);
Stack1push(1);
Stack1push(1);
Stack1push(1);
Stack1push(1);
Stack1push(1);
Stack2push(8);
Stack2push(5);
int m = Stack2pop();
printf("stackpoded :%d",m);
m = Stack2pop();
printf("stackpoded :%d", m);
system("pause");
}