一:C++版链式栈和队列
1.1栈
#include<iostream>
#include<string>
using namespace std;
//抽象类
class stack
{
//纯虚函数
public:
virtual~stack()
{
}
virtual void push(int data) = 0;
virtual void pop() = 0;
virtual int& getTop() = 0;
virtual int sizeStack() const = 0;
virtual bool isEmpty() const = 0;
};
struct Node
{
public:
Node(int data, Node* next)
{
this->data = data;
this->next = next;
}
int data;
struct Node* next;
};
class pStackA :public stack{
public:
pStackA()
{
size = 0;
stackTop = nullptr;
}
~pStackA();
void push(int data);
void pop();
int& getTop();
int sizeStack() const ;
bool isEmpty() const ;
protected:
int size;
Node *stackTop;
};
void pStackA::push(int data)
{
stackTop = new Node(data, stackTop);
size++;
}
void pStackA::pop()
{
if (!stackTop)
{
cout << "栈中无数据";
return;
}
Node* headNode = stackTop->next;
delete stackTop;
stackTop = headNode;
size--;
}
int& pStackA::getTop()
{
return stackTop->data;
}
int pStackA::sizeStack() const
{
return size;
}
bool pStackA::isEmpty() const
{
return size == 0;
}
pStackA::~pStackA()
{
while (stackTop)
{
Node* headNode = stackTop->next;
delete stackTop;
stackTop = headNode;
}
}
int main()
{
stack* p1 = new pStackA;
p1->push(1);
p1->push(2);
while (!p1->isEmpty())
{
cout << p1->getTop() << " ";
p1->pop();
}
return 0;
}
1.2队列
#include<iostream>
#include<string>
using namespace std;
class queue
{
public:
virtual ~queue(){}
virtual int queueSize() const = 0 ;
virtual bool isEmpty() const = 0;
virtual int& getFront() = 0;
virtual int& getBack() = 0;
virtual void push(const int element) = 0;
virtual void pop() = 0;
};
struct Node
{
int data;
Node* next;
Node() {}
Node(int data) :data(data)
{
next = nullptr;
}
Node(int data, Node* next) :data(data), next(next) {}
};
class queueList:public queue
{
public:
queueList()
{
size = 0;
frontNode = backNode = nullptr;
}
~queueList()
{
while (frontNode != backNode)
{
Node* frontNextNode = frontNode->next;
delete frontNode;
frontNode = frontNextNode;
}
}
int queueSize() const;
bool isEmpty() const;
int& getFront();
int& getBack();
void push(const int element);
void pop();
protected:
int size;
Node* frontNode;
Node* backNode;
};
int queueList::queueSize() const
{
return size;
}
bool queueList::isEmpty() const
{
return size == 0;
}
int& queueList::getFront()
{
if (isEmpty())
{
cout << "队中无数据,不能出队" << endl;
exit(0);
}
return frontNode->data;
}
int& queueList::getBack()
{
if (isEmpty())
{
exit(0);
}
return frontNode->data;
}
void queueList::push(const int element)
{
Node* newNode = new Node(element, nullptr);
if (isEmpty())
{
frontNode = newNode;
}
else
backNode->next = newNode;
backNode = newNode;
size++;
}
void queueList::pop()
{
if (isEmpty())
{
exit(0);
}
Node* frontnextNode = frontNode->next;
delete frontNode;
frontNode = frontnextNode;
size--;
}
int main()
{
queue* Quene = new queueList;
Quene->push(0);
Quene->push(1);
Quene->push(2);
while (!Quene->isEmpty())
{
cout<<Quene->getFront();
Quene->pop();
}
return 0;
}
二:++的运算符重载
前置++: A& operator++()
后置++: A operator++(int)
#include<iostream>
#include<string>
using namespace std;
class A {
public:
A() {}
A(int data):data(data){}
A& operator++() //前置++运算符重载
{
this->data++;
return *this;
}
A operator++(int) //后置++需要加个int,后置++时不可以返回引用,因为temp是临时变量。
{
A temp;
temp.data = this->data++;
return temp;
}
void print()
{
cout << data << endl;
}
protected:
int data;
};
int main()
{
A a(10);
++a;
a.print();
A m = a++;
m.print();
return 0;
}
三:const的使用
此处错误,因为struct A中包含const 的成员变量属性,带有const的都必须以参数列表的形式来初始化,如下图所示:
①
②
③
①中两个没有区别
②中p,p1无区别,p2与之有区别
③中有区别
总结:当*在const 前面即 *const的时候如p4,该指针不可以再改变指向,永远只能指向p4, 当const * 时,指针依然可以改变指向。
四:void(*)
1,2都相当于调用print函数