最近学习数据结构与算法,讲到链表和栈,为了满足自己的好奇心和小小的成就感还有巩固知识,便根据栈的特点和功能,自构建栈类(分别使用数组和链表实现)
在用链表实现时发现用结构体老是报错有问题,想到类本身就是结构体的演化,而且占据的内存是一样的,为什么要执着于用结构体呢,
带着想法度娘了一下,发现这篇博客就是用的类模板,所以验证了心里的想法用类来代替结构体,
因知单纯看书看别人的代码是不够的,自己敲出来的才是自己的,所以把自己写的代码分享出来,也方便以后查看回忆
数组实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include<iostream> #include<string.h> #include<stdlib.h> using namespace std; template<class T> class mystack{ private: int top; int maxn; T *data; public: mystack(){ top=-1; maxn=100; data=new T(maxn); memset(data,0,sizeof(data)); } bool isEmpty(){ return top==-1; } bool isFull(){ return top==maxn-1; } bool push(T temp){ if(top<maxn){ data[++top]=temp; return true; } return false; } T pop(){ T t=data[top]; data[top]=0; top--; return t; } T gettop(){ T t=data[top]; return t; } ~mystack(){ delete data; } }; int main(){ mystack<int> s; cout<<s.isEmpty()<<endl; cout<<s.isFull()<<endl; int a=100; int b=200; s.push(a); cout<<"top1="<<s.gettop()<<endl; s.push(b); cout<<"top2="<<s.gettop()<<endl; cout<<"pop1="<<s.pop()<<endl; cout<<"isempty? "<<s.isEmpty()<<endl; cout<<"pop2="<<s.pop()<<endl; cout<<"isempty? "<<s.isEmpty()<<endl; return 0; }
|
2.单向链表是实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| #include<iostream> #include<string.h> #include<stdlib.h> using namespace std; template<class T> class Node{ private: T data; Node *pre; public: Node(){} Node(T t,Node* _pre){ data = t; pre = _pre; }
T getData(){ return data; } void setData(T t){ data = t; } Node* getPre(){ return pre; } void setPre(Node *_pre){ pre = _pre; } ~Node(){
}
};
template<class T> class mystack2{ private: Node<T>* top; Node<T>* Temp; public: mystack2(){ top = NULL; Temp = NULL;
} bool isEmpty(){ return top == NULL; } bool isFull(){ return top != NULL; } bool push(T temp){ Temp = new Node<T>(temp, NULL); if (top != NULL){ Temp->setPre(top); } top = Temp; return true; }
T pop(){ T t = top->getData();
Temp = top; top = top->getPre(); delete Temp;
return t; } T gettop(){ T t = top->getData(); return t; } ~mystack2(){ while (top) { Temp = top; top = top->getPre(); delete Temp;
}
if (Temp){
delete Temp; Temp=NULL; } if (top){
delete top; top=NULL; } } };
int main(){ mystack2<int> s; cout<<s.isEmpty()<<endl; cout<<s.isFull()<<endl; int a=100; int b=200; s.push(a); cout<<"top1="<<s.gettop()<<endl; s.push(b); cout<<"top2="<<s.gettop()<<endl; cout<<"pop1="<<s.pop()<<endl; cout<<"isempty? "<<s.isEmpty()<<endl; cout<<"pop2="<<s.pop()<<endl; cout<<"isempty? "<<s.isEmpty()<<endl;
s.push(a); cout<<"top3="<<s.gettop()<<endl; s.push(b); s.push(a); s.push(b);
return 0; }
|
如果看不懂可以看下面带注释的
1.数组实现:`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include<iostream> #include<string.h> #include<stdlib.h> using namespace std; template<class T> class mystack{ private: int top; int maxn; T *data; public: mystack(){ top=-1; maxn=100; data=new T(maxn); memset(data,0,sizeof(data)); } bool isEmpty(){ return top==-1; } bool isFull(){ return top==maxn-1; } bool push(T temp){ if(top<maxn){ data[++top]=temp; return true; } return false; } T pop(){ T t=data[top]; data[top]=0; top--; return t; } T gettop(){ T t=data[top]; return t; } int length(){ return top+1; } ~mystack(){ delete data; } }; int main(){ mystack<int> s; cout<<s.isEmpty()<<endl; cout<<s.isFull()<<endl; int a=100; int b=200; s.push(a); cout<<"top1="<<s.gettop()<<endl; s.push(b); cout<<"top2="<<s.gettop()<<endl; cout<<"pop1="<<s.pop()<<endl; cout<<"isempty? "<<s.isEmpty()<<endl; cout<<"pop2="<<s.pop()<<endl; cout<<"isempty? "<<s.isEmpty()<<endl; return 0; }
|
2.链表实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| #include<iostream> #include<string.h> #include<stdlib.h> using namespace std;
template<class T> class Node{ private: T data; Node *pre; public: Node(){} Node(T t,Node* _pre){ data = t; pre = _pre; }
T getData(){ return data; } void setData(T t){ data = t; } Node* getPre(){ return pre; } void setPre(Node *_pre){ pre = _pre; } ~Node(){
}
};
template<class T> class mystack2{ private: Node<T>* top; Node<T>* Temp; public: mystack2(){ top = NULL; Temp = NULL; } bool isEmpty(){ return top == NULL; }
bool isFull(){ return false; } bool push(T temp){ Temp = new Node<T>(temp, NULL); if (top != NULL){ Temp->setPre(top); } top = Temp; return true; } T pop(){ T t = top->getData(); Temp = top; top = top->getPre(); delete Temp;
return t; } T gettop(){ T t = top->getData(); return t; } ~mystack2(){ while (top) { Temp = top; top = top->getPre(); delete Temp;
}
if (Temp){
delete Temp; Temp=NULL; } if (top){
delete top; top=NULL; } } };
int main(){ mystack2<int> s; cout<<s.isEmpty()<<endl; cout<<s.isFull()<<endl; int a=100; int b=200; s.push(a); cout<<"top1="<<s.gettop()<<endl; s.push(b); cout<<"top2="<<s.gettop()<<endl; cout<<"pop1="<<s.pop()<<endl; cout<<"isempty? "<<s.isEmpty()<<endl; cout<<"pop2="<<s.pop()<<endl; cout<<"isempty? "<<s.isEmpty()<<endl;
s.push(a); cout<<"top3="<<s.gettop()<<endl; s.push(b); s.push(a); s.push(b);
return 0; }
|