Skip to content

Commit

Permalink
使用模板实现Vector,以及使用Vector实现栈
Browse files Browse the repository at this point in the history
  • Loading branch information
MissYingYingHu committed May 13, 2018
1 parent 1f775a3 commit 5a25335
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 175 deletions.
73 changes: 73 additions & 0 deletions Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include"vector.h"
#include"vector.cpp"
using namespace std;
template<class T,template<class T> class Container> //模板的模板参数
//template<class T,class Container> //适配器模式
class Stack
{
public:
void Push(const T& value)
{
con.PushBack(value);
}
void Pop()
{
con.PopBack();
}
T& Top()
{
con.Front();
}
bool Empty()
{
con.Empty();
}
protected:
Container<T> con;
// Container con;
};

int main()
{
Stack<int,Vector> s; //可以避免少出错
// Stack<int,Vector<int> > s;
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5);
cout<<s.Empty()<<endl;
cout<<s.Top()<<" ";
s.Pop();
cout<<s.Top()<<" ";
s.Pop();
cout<<s.Top()<<" ";
s.Pop();
cout<<s.Top()<<" ";
s.Pop();
cout<<s.Top()<<endl;
s.Pop();
s.Pop();
cout<<s.Empty()<<endl;
cout<<s.Empty()<<endl;
Stack<string,Vector> s1;
s1.Push("aaaa");
s1.Push("BBBB");
s1.Push("CCCC");
s1.Push("DDDD");
s1.Push("EEEE");
cout<<s1.Empty()<<endl;
cout<<s1.Top()<<" ";
s1.Pop();
cout<<s1.Top()<<" ";
s1.Pop();
cout<<s1.Top()<<" ";
s1.Pop();
cout<<s1.Top()<<" ";
s1.Pop();
cout<<s1.Top()<<endl;
s1.Pop();
cout<<s1.Empty()<<endl;
cout<<s1.Empty()<<endl;
return 0;
}
228 changes: 53 additions & 175 deletions test.cpp
Original file line number Diff line number Diff line change
@@ -1,175 +1,53 @@
#include"DLinkList.h"
using namespace std;
#include<windows.h>
void LinkList::DisplayList(char* mag)
{
if(this->_head == NULL)
{
return;
}
cout<<mag;
Node* cur = _head->_next ;
cout<<"head ";
while(cur != _head)
{
cout<< cur->_data<<" ";
cur = cur->_next ;
}
cout<<endl;
}
//////////////////////////////Test////////////////////////////////////
#define TEST_HEADER printf("\n------------------%s------------------\n",__FUNCTION__)
void TestPushBack()
{
LinkList L1;
TEST_HEADER;
L1.PushBack (2);
L1.PushBack (3);
L1.PushBack (4);
L1.PushBack (5);
L1.DisplayList ("尾插四个节点:");
}
void TestPopBack()
{
LinkList L1;
TEST_HEADER;
L1.PushBack (2);
L1.PushBack (3);
L1.PushBack (4);
L1.PushBack (5);
L1.DisplayList ("尾插四个节点:");
L1.PopBack ();
L1.DisplayList ("尾删1个节点:");
L1.PopBack ();
L1.PopBack ();
L1.PopBack ();
L1.DisplayList ("尾删3个节点:");
}
void TestPushFront()
{
LinkList L1;
TEST_HEADER;
L1.PushFront (2);
L1.PushFront (3);
L1.PushFront (4);
L1.PushFront (5);
L1.DisplayList ("头插四个节点:");
}
void TestPopFront()
{
LinkList L1;
TEST_HEADER;
L1.PushBack (2);
L1.PushBack (3);
L1.PushBack (4);
L1.PushBack (5);
L1.DisplayList ("尾插四个节点:");
L1.PopFront ();
L1.DisplayList ("头删1个节点:");
L1.PopFront ();
L1.PopFront ();
L1.DisplayList ("头删2个节点:");
L1.PopFront ();
L1.DisplayList ("头删3个节点:");
}
void TestFind()
{
LinkList L1;
TEST_HEADER;
L1.PushFront (2);
ListNode* pos = L1.PushFront (3);
L1.PushFront (4);
L1.PushFront (5);
L1.DisplayList ("尾插四个节点:");
ListNode* Find = L1.Find (3);
cout<<"actual:"<<Find<<endl;
cout<<"expect:"<<pos<<endl;
ListNode* Find1 = L1.Find (7);
cout<<"actual:"<<Find1<<endl;
cout<<"expect:"<<NULL<<endl;
}
void TestInsertFront()
{
LinkList L1;
TEST_HEADER;
ListNode* pos1 = L1.PushFront (2);
L1.PushFront (3);
L1.PushFront (4);
ListNode* pos = L1.PushFront (5);
L1.DisplayList ("头插四个节点:");
L1.InsertFront (pos1,6);
L1.DisplayList ("插入一个节点:");
L1.InsertFront (pos,7);
L1.DisplayList ("插入一个节点:");
}
void TestInsertBack()
{
LinkList L1;
TEST_HEADER;
ListNode* pos1 = L1.PushFront (2);
L1.PushFront (3);
L1.PushFront (4);
ListNode* pos = L1.PushFront (5);
L1.DisplayList ("头插四个节点:");
L1.InsertBack (pos1,6);
L1.DisplayList ("插入一个节点:");
L1.InsertBack (pos,7);
L1.DisplayList ("插入一个节点:");
}
void TestErase()
{
LinkList L1;
TEST_HEADER;
ListNode* pos1 = L1.PushFront (2);
L1.PushFront (3);
L1.PushFront (4);
ListNode* pos = L1.PushFront (5);
L1.DisplayList ("头插四个节点:");
L1.Erase (pos1);
L1.DisplayList ("插入一个节点:");
L1.Erase(pos);
L1.DisplayList ("插入一个节点:");
}
void TestLinkList()
{
LinkList L1;
TEST_HEADER;
L1.PushFront (2);
L1.PushFront (3);
L1.PushFront (4);
L1.PushFront (5);
L1.DisplayList ("头插四个节点:");
LinkList L2(L1);
L2.DisplayList ("头插四个节点:");
}
void Test()
{
LinkList L1;
TEST_HEADER;
L1.PushFront (2);
L1.PushFront (3);
L1.PushFront (4);
L1.PushFront (5);
L1.DisplayList ("头插四个节点:");
LinkList L2;
L2.PushFront (4);
L2.PushFront (5);
L2.DisplayList ("头插两个节点:");
L2 = L1;
L2.DisplayList ("头插四个节点:");
}
int main()
{
TestPushBack();
TestPopBack();
TestPushFront();
TestPopFront();
TestFind();
TestInsertFront();
TestInsertBack();
TestErase();
TestLinkList();
Test();
system("pause");
return 0;
}
#include"vector.cpp"
#include<string>
using namespace std;

void Test()
{
Vector<int> v;
cout<<v.Empty()<<endl;
//cout<<v.Front()<<endl;
v.Insert(0,1);
v.Insert(0,2);
v.Insert(0,3);
v.Show("插入三个元素");
v.PushBack(4);
v.Show("尾插一个元素");
cout<<v.Front()<<endl;
Vector<int> v1;
v1.Insert(0,1);
v1.Insert(0,2);
v1.Insert(0,4);
v1.Show("插入三个元素");
v1 = v;
v1.Show("赋值v:");
Vector<int> v2(v1);
v2.Show("拷贝构造:");
v.Erase(0);
v.Show("删除首元素后");
v.Erase(5);
cout<<v.Empty()<<endl;
v.Show("删除尾元素后");
Vector<string> s;
s.Show("尾插5个字符串:");
s.PushBack("aaa");
s.Show("尾插5个字符串:");
s.PushBack("bbbb");
s.PushBack("ccccc");
s.PushBack("dddddd");
s.PushBack("eeeeeee");
s.Show("尾插5个字符串:");
Vector<string> s1(s);
s1.Show("拷贝构造:");
Vector<string> s2;
s2 = s;
s2.Show("赋值:");
cout<<s2.Front()<<endl;
}

int main()
{
Test();
return 0;
}

0 comments on commit 5a25335

Please sign in to comment.