forked from xyzbaihaiping/DataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cd8e297
Showing
21 changed files
with
1,791 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#define _CRT_SECURE_NO_WARNINGS | ||
#include "Queue.h" | ||
#include<string> | ||
void test() | ||
{ | ||
Queue<string> q1; | ||
q1.Push("111111111111"); | ||
q1.Push("222222222222"); | ||
q1.Push("333333333333"); | ||
q1.Push("444444444444"); | ||
q1.Push("555555555555"); | ||
Queue<string> q2(q1); | ||
Queue<string> q3; | ||
q3=q1; | ||
cout << q3.Size() << endl; | ||
while (!q3.Empty()) | ||
{ | ||
cout <<q3.Front()<< endl; | ||
q3.Pop(); | ||
} | ||
|
||
} | ||
|
||
int main() | ||
{ | ||
test(); | ||
getchar(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#pragma once | ||
#include<iostream> | ||
#include<assert.h> | ||
#include<string> | ||
using namespace std; | ||
|
||
template<class T> | ||
struct Node | ||
{ | ||
Node(const T& d) | ||
:_data(d) | ||
,_next(NULL) | ||
{} | ||
T _data; | ||
Node<T>* _next; | ||
}; | ||
template<class T> | ||
class Queue | ||
{ | ||
public: | ||
Queue() | ||
:_head(NULL) | ||
,_tail(NULL) | ||
, _size(0) | ||
{} | ||
~Queue() | ||
{ | ||
while (!Empty()) | ||
{ | ||
Pop(); | ||
} | ||
} | ||
Queue(const Queue<T>& q) | ||
:_head(NULL) | ||
,_tail(NULL) | ||
, _size(0) | ||
{ | ||
Node<T>* cur = q._head; | ||
while (cur) | ||
{ | ||
Push(cur->_data); | ||
cur = cur->_next; | ||
} | ||
} | ||
Queue<T>& operator=(Queue<T> q) | ||
{ | ||
swap(_head, q._head); | ||
swap(_tail, q._tail); | ||
_size = q._size; | ||
return *this; | ||
} | ||
void Push(const T& d) | ||
{ | ||
Node<T>* NewNode = new Node<T>(d); | ||
if (_head == NULL) | ||
{ | ||
_head = NewNode; | ||
_tail = NewNode; | ||
} | ||
else | ||
{ | ||
_tail->_next = NewNode; | ||
_tail = NewNode; | ||
} | ||
_size++; | ||
} | ||
void Pop() | ||
{ | ||
if (_head == NULL) | ||
return; | ||
if (_head == _tail) | ||
{ | ||
delete _head; | ||
_head = NULL; | ||
_tail = NULL; | ||
} | ||
else | ||
{ | ||
Node<T>* del = _head; | ||
_head = _head->_next; | ||
delete del; | ||
} | ||
_size--; | ||
} | ||
T& Front() const | ||
{ | ||
assert(_head); | ||
return _head->_data; | ||
} | ||
T& Back() const | ||
{ | ||
assert(_tail); | ||
return _tail->_data; | ||
} | ||
bool Empty() const | ||
{ | ||
return _head == NULL; | ||
} | ||
size_t Size() const | ||
{ | ||
return _size; | ||
} | ||
protected: | ||
Node<T>* _head; | ||
Node<T>* _tail; | ||
size_t _size; | ||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#define _CRT_SECURE_NO_WARNINGS | ||
#include "Stack.h" | ||
#include<string> | ||
int main() | ||
{ | ||
Stack<string> s; | ||
s.Push("1111111111111"); | ||
s.Push("2222222222222"); | ||
s.Push("3333333333333"); | ||
s.Push("4444444444444"); | ||
s.Push("5555555555555"); | ||
Stack<string> s1(s); | ||
Stack<string> s2; | ||
s2 = s; | ||
while (!s2.Empty()) | ||
{ | ||
cout << s2.Top() << " "; | ||
s2.Pop(); | ||
} | ||
getchar(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#pragma once | ||
#include<iostream> | ||
|
||
using namespace std; | ||
|
||
template<class T> | ||
class Stack | ||
{ | ||
public: | ||
Stack(); | ||
~Stack(); | ||
Stack(const Stack<T>& s); | ||
//Stack<T>& operator=(Stack<T> s); | ||
Stack<T>& operator=(const Stack<T>& s); | ||
void Push(const T& d); | ||
void Pop(); | ||
T& Top(); | ||
bool Empty(); | ||
size_t Size(); | ||
protected: | ||
void _CheckCapacity(); | ||
protected: | ||
T* _a; | ||
size_t _top; | ||
size_t _capacity; | ||
}; | ||
template<class T> | ||
void Stack<T>::_CheckCapacity() | ||
{ | ||
if (_a == NULL) | ||
{ | ||
_capacity = 3; | ||
_a = new T[_capacity]; | ||
return; | ||
} | ||
if (_top == _capacity) | ||
{ | ||
_capacity += 3; | ||
T* tmp = new T[_capacity]; | ||
for (size_t i = 0; i < _top; i++) | ||
{ | ||
tmp[i] = _a[i]; | ||
} | ||
delete[] _a; | ||
_a = tmp; | ||
} | ||
} | ||
|
||
template<class T> | ||
Stack<T>::Stack() | ||
:_a(0) | ||
, _top(0) | ||
, _capacity(0) | ||
{} | ||
|
||
template<class T> | ||
Stack<T>::~Stack() | ||
{ | ||
if (_a) | ||
{ | ||
delete[] _a; | ||
} | ||
} | ||
template<class T> | ||
Stack<T>::Stack(const Stack<T>& s) | ||
:_a(new T[_top]) | ||
,_top(s._top) | ||
,_capacity(s._capacity) | ||
{ | ||
|
||
for (size_t i = 0; i < _top; i++) | ||
{ | ||
_a[i] = s._a[i]; | ||
} | ||
|
||
} | ||
|
||
//template<class T> | ||
//Stack<T>& Stack<T>::operator=(Stack<T> s) | ||
//{ | ||
// swap(s._a,_a); | ||
// _top = s._top; | ||
// _capacity = s._capacity; | ||
// return *this; | ||
//} | ||
template<class T> | ||
Stack<T>& Stack<T>::operator=(const Stack<T>& s) | ||
{ | ||
if (this != &s) | ||
{ | ||
T* tmp = new T[s._top]; | ||
for (size_t i = 0; i < s._top; i++) | ||
{ | ||
tmp[i] = s._a[i]; | ||
} | ||
delete[] _a; | ||
_a = tmp; | ||
_capacity = s._top; | ||
_top = s._top; | ||
} | ||
return *this; | ||
} | ||
|
||
template<class T> | ||
void Stack<T>::Push(const T& d) | ||
{ | ||
_CheckCapacity(); | ||
_a[_top++] = d; | ||
} | ||
|
||
template<class T> | ||
void Stack<T>::Pop() | ||
{ | ||
assert(_top > 0) | ||
--_top; | ||
} | ||
|
||
template<class T> | ||
T& Stack<T>::Top() | ||
{ | ||
assert(_top>0) | ||
return _a[_top - 1]; | ||
} | ||
template<class T> | ||
bool Stack<T>::Empty() | ||
{ | ||
if (_top == 0) | ||
return true; | ||
else | ||
return false; | ||
} | ||
|
||
template<class T> | ||
size_t Stack<T>::Size() | ||
{ | ||
return _top; | ||
} |
Binary file not shown.
Oops, something went wrong.