栈
栈:一种基于后进先出策略的集合类型;
在链表的表示方法下,栈在同一端进出元素,所以只需要一个指针域即可。
class stack{ | |
stack() | 创建一个空栈 |
void Empty() | 判断栈是否为空 |
void size() | 栈长 |
void push(string item) | 进栈 |
string pop() | 出栈 |
void trave() | 遍历栈 |
void clear() | 清空栈 |
bool find(string item) | 查找元素是否存在 |
}; |
class stack
{
struct Node
{
string data;
Node* next;
};
private:
Node* first = NULL;
int N = 0;
public:
bool Empty(){ return N == 0; }
int size(){ return N; }
void push(string item)
{
Node* oldfirst = first;
first = new Node();
first->data = item;
first->next = oldfirst;
N++;
}
string pop()
{
if (Empty())exit(0);
Node* curr = first;
string item = first->data;
first = first->next;
delete curr;
N--;
return item;
}
void trave()
{
Node* curr = first;
for (int i = 0; i < N; ++i)
{
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
void clear()
{
for (int i = 0; i < N; ++i)
{
Node* curr = first;
first = first->next;
delete curr;
}
N = 0;
}
bool find(string item)
{
Node* curr = first;
for (int i = 0; i < N; ++i)
if (curr->data == item)return true;
else curr = curr->next;
return false;
}
};
队列
是一种基于先进先出策略的集合类型;
在链表的表示方式下,队列的一端进一端出,所以需要两个指针域。
尾插法进队,first结点出队,否则遍历不好启动。
class queue{ | |
queue() | 建立一个空队列 |
bool Empty() | 队列是否为空 |
int size() | 队列长度 |
void enqueue(string item) | 进队 |
string dequeue() | 出队 |
void trave() | 遍历 |
void clear() | 清空队列 |
bool find() | 查找队列 |
}; |
class queue
{
struct Node
{
string data;
Node* next;
};
private:
Node* first=NULL;
Node* last = NULL;
int N = 0;
public:
bool Empty(){ return N == 0; }
int size(){ return N; }
void enqueue(string item)
{
Node* oldlast = last;
last = new Node();
last->data = item;
if (Empty())first = last;
else oldlast->next = last;
N++;
}
string dequeue()
{
if (Empty())exit(0);
Node* curr = first;
string item = first->data;
first = first->next;
delete curr;
N--;
return item;
}
void trave()
{
Node* curr = first;
for (int i = 0; i < N; ++i)
{
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
void clear()
{
for (int i = 0; i < N; ++i)
{
Node* curr = first;
first = first->next;
delete curr;
}
N = 0;
}
bool find(string item)
{
Node* curr = first;
for (int i = 0; i < N; ++i)
if (curr->data == item)return true;
else curr = curr->next;
return false;
}
};
栈和队列本质上都是一种被限制的线性表,只是在限制条件上有所不同而已。
特殊的队列:
双向队列:
class Deque//双向队列
{
struct Node
{
string data;
Node* right;
Node* left;
};
private:
Node* first = NULL;
Node* last = NULL;
int N = 0;
public:
bool Empty(){ return N == 0; }
int size(){ return N; }
void pushleft(string item)//左进队
{
Node* oldfirst = first;
first = new Node();
first->data = item;
if (Empty())last = first;
else
{
first->right = oldfirst;
oldfirst->left = first;
}
N++;
}
void pushright(string item)//右进队
{
Node* oldlast = last;
last = new Node();
last->data = item;
if (Empty())first = last;
else
{
last->left = oldlast;
oldlast->right = last;
}
N++;
}
string popleft()//左出队
{
if (Empty())exit(0);
Node* curr = first;
string item = curr->data;
first = first->right;
delete curr;
N--;
return item;
}
string popright()//右出队
{
if (Empty())exit(0);
Node* curr = last;
string item = curr->data;
last = last->left;
delete curr;
N--;
return item;
}
void traveleft()//左遍历
{
Node* curr = first;
for (int i = 0; i < N; ++i)
{
cout << curr->data << " ";
curr = curr->right;
}
cout << endl;
}
void traveright()//右遍历
{
Node* curr = last;
for (int i = 0; i < N; ++i)
{
cout << curr->data << " ";
curr = curr->left;
}
cout << endl;
}
void clear()//清空
{
for (int i = 0; i < N; ++i)
{
Node* curr = first;
first = first->right;
delete curr;
}
N = 0;
}
};
class xeque//循环队列
{
struct Node
{
string data;
Node* next;
};
private:
Node* first = NULL;
Node* last = NULL;
int N = 0;
public:
bool Empty(){ return N == 0; }
int size(){ return N; }
void push(string item)
{
Node* oldlast = last;
last = new Node();
last->data = item;
if (Empty())first = last;
else
{
oldlast->next = last;
last->next = first;
}
N++;
}
string pop()
{
if (Empty())exit(0);
Node* curr = first;
string item = curr->data;
first = first->next;
delete curr;
if (--N)last->next = first;
return item;
}
void trave()
{
Node* curr = first;
for (int i = 0; i != N; ++i)
{
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
void clear()
{
for (int i = 0; i != N; ++i)
{
Node* curr = first;
first = first->next;
delete curr;
}
N = 0;
}
};
双向循环队列:
class xwque//双向循环队列
{
struct Node
{
string data;
Node* right;
Node* left;
};
private:
Node* first=NULL;
Node* last=NULL;
int N = 0;
public:
bool Empty(){ return N == 0; }
int size(){ return N; }
void pushleft(string item)
{
Node* oldfirst = first;
first = new Node();
first->data = item;
if (Empty())last = first;
else
{
oldfirst->left = first;
first->right = oldfirst;
last->right = first;
first->left=last;
}
N++;
}
void pushright(string item)
{
Node* oldlast = last;
last = new Node();
last->data = item;
if (Empty())first = last;
else
{
oldlast->right = last;
last->left = oldlast;
first->left = last;
last->right = first;
}
N++;
}
string popleft()
{
if (Empty())exit(0);
Node* curr = first;
string item = curr->data;
first = first->right;
if (--N)
{
last->right = first;
first->left = last;
}
delete curr;
return item;
}
string popright()
{
if (Empty())exit(0);
Node* curr = last;
string item = last->data;
last = last->left;
if (--N)
{
last->right = first;
first->left = last;
}
delete curr;
return item;
}
void traveleft()
{
Node* curr = first;
for (int i = 0; i != N; ++i)
{
cout << curr->data << " ";
curr = curr->right;
}
cout << endl;
}
void traveright()
{
Node* curr = last;
for (int i = 0; i != N; ++i)
{
cout << curr->data << " ";
curr = curr->left;
}
cout << endl;
}
void clear()
{
for (int i = 0; i != N; ++i)
{
Node* curr = first;
first = first->right;
delete curr;
}
N = 0;
}
};
双栈队列:
class stque//双栈队列,栈为本章中的栈
{
private:
int N=0;
stack k1;
stack k2;
public:
bool Empty(){ return N == 0; }
int size(){ return N; }
void enqueue(string item)
{
k1.push(item);
N++;
}
string dequeue()
{
for (int i = 0; i <N-1; ++i)
k2.push(k1.pop());
string item = k1.pop();
N--;
for (int i = 0; i < N; ++i)
k1.push(k2.pop());
return item;
}
void trave()
{
for (int i = 0; i != N; ++i)
k2.push(k1.pop());
k2.trave();
for (int i = 0; i != N; ++i)
k1.push(k2.pop());
}
void clear()
{
k1.clear();
N = 0;
}
bool find(string item)
{
return k1.find(item);
}
};
int main()//双栈队列,测试用例
{
stque p;
string item;
int m;
cout << "**********************************" << endl;
cout << "0.查看菜单. 1.进队." << endl;
cout << "2.出队. 3.遍历队." << endl;
cout << "4.队是否为空. 5.队列长." << endl;
cout << "6.清空队列. 7.查找." << endl;
cout << "**********************************" << endl;
while (cin >> m)
{
switch (m)
{
case 0:
cout << "**********************************" << endl;
cout << "0.查看菜单. 1.进队." << endl;
cout << "2.出队. 3.遍历队." << endl;
cout << "4.队是否为空. 5.队列长." << endl;
cout << "6.清空队列. 7.查找." << endl;
cout << "**********************************" << endl;
break;
case 1:
cout << "输入进队元素:" << endl;
cin >> item;
p.enqueue(item);
break;
case 2:
cout << "出队元素为:" << p.dequeue() << endl;
break;
case 3:
cout << "遍历队:" << endl;
p.trave();
break;
case 4:
if (p.Empty())cout << "队为空" << endl;
else if (!p.Empty()) cout << "队不为空" << endl;
break;
case 5:
cout << "队长为:" << p.size() << endl;
break;
case 6:
cout << "清空队列." << endl;
p.clear();
break;
case 7:
cout << "输出想要查找的元素:" << endl;
cin >> item;
if (p.find(item))cout << "元素存在" << endl;
else if (!p.find(item))cout << "元素不存在" << endl;
break;
}
}
system("pause");
return 0;
}
背包
是一种只进不出的元素集合。
API:
class Bag{ | |
Bag{ | 创建背包 |
void add(Item item) | 添加元素 |
bool Empty() | 背包是否为空 |
int size() | 背包元素数量 |
void trave() | 遍历背包 |
}; |
代码:
template<typename Item>
class Bag
{
class Node
{
friend class Bag;
Item data;
Node* next;
Node(Item item,Node* x):data(item), next(x){}
};
public:
bool Empty(){ return N == 0; }
int size(){ return N; }
void add(Item item)
{
Node* oldfirst = first;
first = new Node(item,oldfirst);
N++;
}
void trave()
{
for (Node* x = first; x != NULL; x = x->next)
cout << x->data << " ";
cout << endl;
}
private:
Node* first = NULL;//链首
int N = 0;//元素数量
};
测试用例:
int main()
{
Bag<string> p;
int m;
string item;
cout << "**********************************" << endl;
cout << "0.查看菜单. 1.进包." << endl;
cout << "2.遍历背包. 3.包是否为空." << endl;
cout << "4.包长" << endl;
cout << "**********************************" << endl;
while (cin >> m)
{
switch (m)
{
case 0:
{
cout << "**********************************" << endl;
cout << "0.查看菜单. 1.进包." << endl;
cout << "2.遍历背包. 3.包是否为空." << endl;
cout << "4.包长" << endl;
cout << "**********************************" << endl;
break;
}
case 1:
{
cout << "输入进包元素:" << endl;
cin >> item;
p.add(item);
break;
}
case 2:cout << "遍历包:" << endl;
p.trave();
break;
case 3:if (p.Empty())cout << "包为空" << endl;
else if (!p.Empty()) cout << "包不为空" << endl;
break;
case 4:cout << "包长为:" << p.size() << endl;
break;
}
}
system("pause");
return 0;
}