寻求一个C++程序,关于单链表的基本操作的。

2024-11-09 03:11:20
推荐回答(1个)
回答1:

#include typedef char Type;//栈类
class Stack{
public:
//构造和析构函数
Stack():m_pFirstNode(0){}
~Stack(){clear();}
public:
//操作函数
bool push(const Type& data);//压栈
bool pop();//出栈
Type& get();//得到栈顶的数据
int size()const;//得到大小
bool empty()const{return m_pFirstNode==0;}//是否为空
void clear(); //清空

private:
struct Node{
Type data;
Node* pNextNode;
};

Node* m_pFirstNode;//首节点的指针
};bool Stack::push(const Type& data){
Node* pNewNode = new Node;
if(pNewNode!=0){
pNewNode->data = data;
pNewNode->pNextNode = m_pFirstNode;
m_pFirstNode = pNewNode;
return true;
}else{
return false;
}
}bool Stack::pop(){
if(m_pFirstNode!=0){
Node* pNextNode = m_pFirstNode->pNextNode;
delete m_pFirstNode;
m_pFirstNode = pNextNode;
return true;
}else{
return false;
}
}Type& Stack::get(){
if(m_pFirstNode!=0){
return m_pFirstNode->data;
}else{
return *reinterpret_cast(0);
}
}int Stack::size() const{
int counter = 0;
Node* pNode = m_pFirstNode;
while(pNode!=0){
++counter;
pNode = pNode->pNextNode;
}
return counter;
}void Stack::clear(){
if(m_pFirstNode!=0){
Node* pNode = m_pFirstNode;
do{
Node* pNextNode =pNode->pNextNode;
delete pNode;
pNode = pNextNode;
}while(pNode!=0);
m_pFirstNode = 0;
}
}//队列类
class Queue{
public:
//构造和析构函数
Queue():m_pFirstNode(0){}
~Queue(){clear();}
public:
//操作函数
bool add(const Type& data);//入队列
bool remove();//出队列
Type& get();//得到队列的数据
int size()const;//得到大小
bool empty()const{return m_pFirstNode==0;}//是否为空
void clear(); //清空private:
struct Node{
Type data;
Node* pNextNode;
}; Node* m_pFirstNode;//首节点的指针};bool Queue::add(const Type& data){
Node* pNewNode = new Node;
if(pNewNode!=0){
pNewNode->data = data;
pNewNode->pNextNode = 0;
if(m_pFirstNode == 0){
m_pFirstNode = pNewNode;
}else{
Node* pLastNode = m_pFirstNode;
while(pLastNode->pNextNode != 0){
pLastNode = pLastNode->pNextNode;
}
pLastNode->pNextNode = pNewNode;
}
return true;
}else{
return false;
}
}bool Queue::remove(){
if(m_pFirstNode!=0){
Node* pNextNode = m_pFirstNode->pNextNode;
delete m_pFirstNode;
m_pFirstNode = pNextNode;
return true;
}else{
return false;
}
}Type& Queue::get(){
if(m_pFirstNode!=0){
return m_pFirstNode->data;
}else{
return *reinterpret_cast(0);
}
}int Queue::size() const{
int counter = 0;
Node* pNode = m_pFirstNode;
while(pNode!=0){
++counter;
pNode = pNode->pNextNode;
}
return counter;
}void Queue::clear(){
if(m_pFirstNode!=0){
Node* pNode = m_pFirstNode;
do{
Node* pNextNode =pNode->pNextNode;
delete pNode;
pNode = pNextNode;
}while(pNode!=0);
m_pFirstNode = 0;
}
}//二叉树的节点
struct BinaryTreeNode{
Type data;
BinaryTreeNode* pLeftNode;
BinaryTreeNode* pRightNode;
};//输出二叉树的节点
void outputBinaryTreeNode(BinaryTreeNode* pNode){
if(pNode!=0){
std::cout << pNode->data;//NLR前序遍历
outputBinaryTreeNode(pNode->pLeftNode);
outputBinaryTreeNode(pNode->pRightNode);
}
return;
}const int Id_None = 0;
const int Id_Island_A = 1;
const int Id_Island_B = 2;
const int Id_Island_C = 3;
const int Id_Island_D = 4;const int MaxCanReachSize = 4;struct MapNode{
int id;
int canReach[MaxCanReachSize];
};int main(){ {
Stack stack;
stack.push('a');
stack.push('b');
stack.push('c');
while(!stack.empty()){
std::cout << stack.get();
stack.pop();
}
std::cout << std::endl;

int a = 123456;
stack.push('0'+a%10);
a/=10;
while(a!=0){
stack.push('0'+a%10);
a/=10;
}
while(!stack.empty()){
std::cout << stack.get();
stack.pop();
}
}

{
Queue queue;
queue.add('a');
queue.add('b');
queue.add('c');
while(!queue.empty()){
std::cout << queue.get();
queue.remove();
}
std::cout << std::endl;
}

{
BinaryTreeNode treeNode[3];
treeNode[0].data = 'a';
treeNode[1].data = 'b';
treeNode[2].data = 'c';

treeNode[0].pLeftNode = &treeNode[1];
treeNode[0].pRightNode = &treeNode[2];
treeNode[1].pLeftNode = 0;
treeNode[1].pRightNode = 0;
treeNode[2].pLeftNode = 0;
treeNode[2].pRightNode = 0;

BinaryTreeNode* pTreeNode = &treeNode[0];

outputBinaryTreeNode(pTreeNode);
}

{
MapNode mapNode[4];
mapNode[0].id = Id_Island_A;
mapNode[1].id = Id_Island_B;
mapNode[2].id = Id_Island_C;
mapNode[3].id = Id_Island_D; mapNode[0].canReach[0] = Id_Island_B;
mapNode[0].canReach[1] = Id_Island_C;
mapNode[0].canReach[2] = Id_Island_D;
mapNode[0].canReach[3] = Id_None; mapNode[1].canReach[0] = Id_Island_C;
mapNode[1].canReach[1] = Id_None;
mapNode[1].canReach[2] = Id_None;
mapNode[1].canReach[3] = Id_None; mapNode[2].canReach[0] = Id_Island_A;
mapNode[2].canReach[1] = Id_Island_B;
mapNode[2].canReach[2] = Id_None;
mapNode[2].canReach[3] = Id_None;

mapNode[3].canReach[0] = Id_Island_A;
mapNode[3].canReach[1] = Id_None;
mapNode[3].canReach[2] = Id_None;
mapNode[3].canReach[3] = Id_None;
}

}三种数据结构,原来写的,自己参考一下