为了训练阅读代码的能力,本篇文章的所有代码都不作详细注释。
一、链栈
实验要求:构造一个链栈,要求至少能完成下列操作:将元素压入栈顶、将栈顶元素弹出、获得栈顶元素、获得栈的大小、清除栈。
参考代码及简易注释:
1、成员访问运算符.的优先级要比取值符*高,因此在本例中诸如 s.next.next 的写法不会通过编译,应该写成 (*s.next).next 来获取响应的值。
#include<cstdio>
#include<random>
#include<ctime>
typedef unsigned long long ull; typedef unsigned uint;
const ull lmax = 64;
template<typename t> struct LinkStack { t top = 0; LinkStack<t>* next = nullptr; };
template<typename t> inline void PushIntoLinkStack(LinkStack<t>& s, const t& value) {
LinkStack<t> p = s; s.next = (LinkStack<t>*)malloc(sizeof(LinkStack<t>)), (*s.next).next = p.next, (*s.next).top = p.top, s.top = value;
}
template<typename t> inline void PopFromLinkStack(LinkStack<t>& s) {
if (s.next == nullptr)return;
LinkStack<t>* p = s.next; s.top = (*s.next).top, s.next = (*s.next).next, free(p);
}
template<typename t> inline t TopOfLinkStack(const LinkStack<t>& s) {
return s.top;
}
template<typename t> inline ull SizeOfLinkStack(const LinkStack<t>& s) {
ull r = 0; LinkStack<t> p = s;
while (p.next != nullptr) { ++r, p = *p.next; }
return r;
}
template<typename t> inline void ClearLinkStack(LinkStack<t>& s) {
LinkStack<t>* p;
while (s.next != nullptr) {
p = s.next, s.top = (*s.next).top, s.next = (*s.next).next, free(p);
}
}
int main() {
std::uniform_int_distribution<uint> u(0, 99999), v(1, 128), w(1, 64); std::default_random_engine d; d.seed(clock()); uint n, e;
LinkStack<uint> s;
for (;;) {
n = v(d);
for (uint i = 0; i < n; ++i) {
e = u(d), PushIntoLinkStack(s, e);
printf("Pushed %u into link stack s.\nThe size of s = %llu, the top of s = %u\n", e, SizeOfLinkStack(s), TopOfLinkStack(s));
}
n = w(d);
for (uint i = 0; i < n; ++i) {
if (SizeOfLinkStack(s) != 0)printf("The top of s = %u\n", TopOfLinkStack(s));
PopFromLinkStack(s), puts("Executed a pop operation on link stack s.");
}
printf("The size of link stack s = %llu\n", SizeOfLinkStack(s));
ClearLinkStack(s), printf("The size of link stack s = %llu\n", SizeOfLinkStack(s));
puts("If you wanna continue please input any character. Or input EOF (Ctrl + Z).");
if (getchar() == EOF)return 0;
}
}
二、链队列
实验要求:构造一个链队列,要求至少能完成下列操作:将元素压入队尾、将队首元素弹出、获得队首元素、获得队尾元素、获得队列的大小、清除队列。
参考代码及简易注释:
1、成员访问运算符.的优先级要比取值符*高,因此在本例中诸如 s.next.next 的写法不会通过编译,应该写成 (*s.next).next 来获取响应的值。
2、如果需要访问指针指向的内容,应该在指针后采用->运算符或者直接先取指针的值并打括号然后再用点来访问值的成员。
#include<cstdio>
#include<random>
#include<ctime>
#pragma warning(disable:4996)
typedef unsigned long long ull; typedef unsigned uint;
const ull lmax = 64;
template<typename t> struct LinkQueueNode { t data = 0; LinkQueueNode<t>* next = nullptr; };
template<typename t> struct LinkQueue { LinkQueueNode<t>* front, * back; ull size = 0; };
template<typename t> inline void PushIntoLinkQueue(LinkQueue<t>& q, const t& value) {
if (q.size == 0) { q.front = (LinkQueueNode<t>*)malloc(sizeof(LinkQueueNode<t>)), q.back = q.front, (*q.back).data = value, ++q.size; return; }
(*q.back).next = (LinkQueueNode<t>*)malloc(sizeof(LinkQueueNode<t>)), q.back = q.back->next, (*q.back).data = value, ++q.size;
}
template<typename t> inline void PopFromLinkQueue(LinkQueue<t>& q) {
if (q.size == 0)return;
LinkQueueNode<t>* p = q.front; q.front = p->next, --q.size, free(p);
}
template<typename t> inline t FrontOfLinkQueue(const LinkQueue<t>& q) {
return (*q.front).data;
}
template<typename t> inline t BackOfLinkQueue(const LinkQueue<t>& q) {
return (*q.back).data;
}
template<typename t> inline void ClearLinkQueue(LinkQueue<t>& q) {
LinkQueueNode<t>* p;
while (q.size != 0) {
p = q.front, q.front = p->next, --q.size, free(p);
}
}
int main() {
std::uniform_int_distribution<uint> u(0, 99999), v(1, 128), w(1, 64); std::default_random_engine d; d.seed(clock()); uint n, e;
LinkQueue<uint> q;
for (;;) {
n = v(d);
for (uint i = 0; i < n; ++i) {
e = u(d), PushIntoLinkQueue(q, e);
printf("Pushed %u into link queue q. The front and back of q are: %u, %u\nThe size of q = %llu\n", e, FrontOfLinkQueue(q), BackOfLinkQueue(q), q.size);
}
n = w(d);
for (uint i = 0; i < n; ++i) {
if (q.size != 0)printf("The front and back of q are: %u, %u\n", FrontOfLinkQueue(q), BackOfLinkQueue(q));
PopFromLinkQueue(q); puts("Executed a pop operation on link queue q.");
}
printf("The size of link queue q = %llu\n", q.size);
ClearLinkQueue(q), printf("The size of link queue q = %llu\n", q.size);
puts("If you wanna continue please input any character. Or input EOF (Ctrl + Z).");
if (getchar() == EOF)return 0;
}
}