1.用数组实现一个栈,用了一个多小时····不过还算顺利,全部代码如下
#include<iostream>
using namespace std;
template<typename T >
class arrayStack{
public:
arrayStack(){
capacity = 5;
size = 0;
list = new T[capacity];
}
~arrayStack(){
delete list;
list = nullptr;
}
void push(const T& elem);
void pop();
T& top() const;
int getSize() const;
bool empty() const;
void checkCapacity();
private:
T* list;
int capacity;
int size;
};
template<typename T> void arrayStack<T>::push(const T& elem){
checkCapacity();
list[size] = elem;
size++;
}
template<typename T> void arrayStack<T>::checkCapacity(){
if (size == capacity)
{
T* newlist = new T[capacity + 5];
for (int i = 0; i < size; i++)
{
newlist[i] = list[i];
}
list = newlist;
}
}
template<typename T> void arrayStack<T>::pop(){
if (size == 0)
return;
size--;
}
template<typename T> T& arrayStack<T>::top() const{
if (size == 0)
throw "EmptyStackException";
return(list[size - 1]);
}
template<typename T> int arrayStack<T>::getSize() const{
return size;
}
template<typename T> bool arrayStack<T>::empty() const{
return(0 == size);
}
void main(){
arrayStack<char*> stack;
stack.push("a");
cout << stack.top() << endl;
stack.push("g");
stack.push("e");
stack.push("a");
stack.push("g");
stack.push("e");
cout << stack.getSize();
cout << stack.top();
stack.push("y");
cout << stack.top();
system("pause");
return;
}
不过有小伙伴知道arrayStack<char*> stack;
这句话为什么是char* 而不是 char呢
下一步用链表实现一个栈
2.存储任意类型的栈,不过这个程序在指针上出现了问题,编译能通过,由于在这道任务上花费了很长时间,这个有待后续解BUG。有兴趣的小伙伴可以帮忙解决一下这个问题。
#include<iostream>
using namespace std;
template<typename T>
class stackNode{
public:
T data;
stackNode *next;
stackNode(){};
stackNode(T e){
data = T;
}
};
template<typename T>
class linkStack
{
public:
linkStack();
~linkStack();
void pop();
void push(const T& elem);
bool stackEmpty()const;
int getSize()const;
T& getTop() const;
private:
int size;
stackNode<T>* buttom;
stackNode<T>* top;
};
template<typename T> linkStack<T>::linkStack(){
size = 0;
stackNode<T>* buttom = top = NULL;
}
template<typename T> linkStack<T>::~linkStack(){
stackNode<T> *ptr1 = buttom;
stackNode<T> *ptr2 = buttom;
while (ptr1 != NULL)
{
ptr2 = ptr1;
ptr1 = ptr1->next;
delete ptr2;
}
delete ptr1;
delete buttom;
delete top;
ptr1 = ptr2 = buttom = top = NULL;
size = 0;
}
template<typename T>void linkStack<T>::pop(){
if (stackEmpty())
return;
if (getSize() == 1)
{
buttom = top = NULL;
size = 0;
}
stackNode<T> *ptr = buttom;
while (ptr->next != top)
{
ptr = ptr->next;
}
delete top;
top = ptr;
size--;
delete ptr;
ptr = NULL;
}
template<typename T> void linkStack<T>::push(const T& elem){
stackNode<T> *ptr = new stackNode<T>;
if (ptr == NULL)
return;
ptr->data = elem;
ptr->next = NULL;
if (top == NULL)
{
buttom = top = ptr;
return;
}
top->next = ptr;
top = ptr;
delete ptr;
ptr = NULL;
size++;
}
template<typename T>bool linkStack<T>::stackEmpty()const{
return (0 == getSize());
}
template<typename T>int linkStack<T>::getSize()const{
return size;
}
template<typename T>T& linkStack<T>::getTop() const{
return top->data;
}
void main(){
linkStack<char*> stack;
stack.push("4");
/*stack.push(5);
stack.push(5);
cout << stack.getSize()<<endl;
cout << stack.getTop()<<endl;
stack.push(8);
stack.push(3);
stack.push(9);
stack.push(3);
stack.pop();
stack.pop();
cout << stack.getTop()<<endl;
cout << stack.getSize()<<endl;*/
cout << "hello" << endl;
system("pause");
return;
}
3.模拟浏览器的进退
这个也出现了上面的问题,在访问内存上有差错,有待解决
#include<iostream>
#include<stack>
#include<string>
using namespace std;
class browser{
public:
browser(){
stack<string>* currentPage = new stack<string>;
currentPage->push("This is page D");
currentPage->push("This is page C");
currentPage->push("This is page B");
currentPage->push("This is page A");
stack<string>* currentPage2 = new stack<string>;
cout << currentPage->top();
}
~browser()
{
delete currentPage;
delete currentPage2;
currentPage = nullptr;
currentPage2 = nullptr;
}
void forward(){
/*if ((currentPage->size()) == 1)
{
cout << "this is the end" << endl;
return;
}*/
string m = currentPage->top();
currentPage2->push(m);
currentPage->pop();
cout << currentPage->top();
return;
}
void back(){
if (currentPage2->size() == 1)
{
cout << "this is the begin" << endl;
return;
}
currentPage->push(currentPage2->top());
currentPage2->pop();
cout << currentPage->top();
}
private:
stack<string>* currentPage;
stack<string>* currentPage2;
};
class A{
public:
A()
{
i = 1;
}
int i;
};
void main(){
/* A* a = new A;
if (a == NULL)
cout << "aaaa" << endl;
cout << a->i;*/
browser* Browser = new browser;
if (Browser == NULL)
cout<<"sssss";
Browser->forward();
/*Browser->forward();
Browser->forward();
Browser->forward();
Browser->back();
Browser->back();
Browser->back();
Browser->back();
delete Browser;
Browser = nullptr;*/
cout << "hello" << endl;
system("pause");
}
关于队列的编程真的没有时间再手打了,队列的知识与栈还有链表相似。
4.斐波那契数列
这个不难
#include<iostream>
using namespace std;
int solution(int num){
if (num < 1)
return 0;
if (num == 1 || num == 2)
return 1;
int count = 2;
int pre = 1;
int result = 1;
while (count < num)
{
int mid = result;
result += pre;
pre = mid;
count++;
}
return result;
}
void main()
{
int k = solution(6);
int i = solution(5);
int j = solution(4);
cout << k << i << j << endl;
system("pause");
}
求n的阶乘就不打了,挺简单的
5.求一个数据集合的全排列
我选择了字符型集合数组作为参数
采用递归的思想,当cur = 0时,把字符串中所有元素都换到第一个元素中去,再组合上后面的所有可能,往后推导类似,代码如下
#include<iostream>
using namespace std;
void swap(char *ptr1, char *ptr2)
{
char tmp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = tmp;
}
void solution(char *data, int cur, int length)
{
if (cur == length){
for (int i = 0; i < length; i++)
{
cout << data[i];
}
cout << endl;
}
for (int j = cur; j < length; j++){
swap(&data[cur], &data[j]);
solution(data, cur + 1, length);
swap(&data[cur], &data[j]);
}
}
void main(){
char Data[] = { "fenhy" };
solution(Data, 0, 5);
system("pause");
}
6.LeetCode习题
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
很明显这道题是运用栈的经典题目,使用匹配的规则,代码如下
#define _SCL_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<stack>
#include<algorithm>
using namespace std;
class Solution {
public:
bool isValid(string s) {
stack<char> Stack;
int length = s.length();
char* arr = new char[length+1];
s.copy(arr, length);
arr[length] = '\0';
for (int i = 0; i < length; i++)
{
if (Stack.size() == 0)
Stack.push(arr[i]);
else if (isRight(Stack.top(), arr[i])){
Stack.pop();
}
else
{
Stack.push(arr[i]);
}
}
return Stack.size() == 0;
}
bool isRight(char a, char b){
return (a == '(' && b == ')') || (a == '[' && b == ']') || (a == '{' && b == '}');
}
};
void main(){
string str1("()[]{}");
string str2("{}(()");
Solution sol;
int a = sol.isValid(str1);
int b = sol.isValid(str2);
cout << a <<b<< endl;
system("pause");
}