c++语法,打印帮助
C++ Syntax, Print Help
我写了一个小程序
#include <iostream.h>
class node
{
public:
int value; //value stored in the node
node *next; //pointer to next node
node *prev; //pointer to previous node
};
class dlist
{
public:
node *front; //pointer to front of list
node *back; //pointer to back of list
dlist()
{
front=NULL;
back=NULL;
}
void insertFront(int value);
void insertBack(int value);
void removeFront();
void removeBack();
void insertBefore(int value,node *nodeB);
void insertAfter(int value,node *nodeA);
void removeBefore(node *nodeB);
void removeAfter(node *nodeA);
void removeNode(node *newNode);
void printDListFront();
void printDListBack();
};
//insert a node before nodeB
void dlist::insertBefore(int value,node *nodeB)
{
node *newNode;
newNode=new node();
newNode->prev=nodeB->prev;
newNode->next =nodeB;
newNode->value =value;
if(nodeB->prev==NULL)
{
this->front=newNode;
}
nodeB->prev=newNode;
}
//insert a node before the front node
void dlist::insertFront (int value)
{
node *newNode;
if(this->front==NULL)
{
newNode=new node();
this->front=newNode;
this->back =newNode;
newNode->prev=NULL;
newNode->next=NULL;
newNode->value=value;
}
else
{
insertBefore(value,this->front );
}
}
//insert a node after nodeB
void dlist::insertAfter(int value,node *nodeB)
{
node *newNode;
newNode=new node();
newNode->next= nodeB->next ;
newNode->prev =nodeB;
newNode->value =value;
if(nodeB->next==NULL)
{
cout<<"n "<< endl;
this->back =newNode;
}
nodeB->next=newNode;
cout<<"2"<<endl;
}
//insert a node after the last node
void dlist::insertBack (int value)
{
if(this->back==NULL)
{
cout<<"insert at back";
insertFront(value);
}
else
{
cout<<"insert at back";
insertAfter(value,this->back );
}
}
//remove the front node
void dlist::removeFront ()
{
removeNode(this->front);
}
//remove a back node
void dlist::removeBack ()
{
removeNode(this->back);
}
//remove before a node
void dlist::removeBefore(node *nodeB)
{
if(nodeB->prev==this->front)
{
this->front=nodeB;
this->front->prev=NULL;
}
else
{
removeNode(nodeB->prev);
}
}
//remove after a node
void dlist::removeAfter(node *nodeA)
{
if(nodeA->next==this->back)
{
this->back=nodeA;
this->back->next=NULL;
}
else
{
removeNode(nodeA->next);
}
}
//remove a perticular node
void dlist::removeNode(node *nodeToRemove)
{
if(nodeToRemove==this->front)
{
this->front=this->front->next;
this->front->prev=NULL;
}
else if (nodeToRemove==this->back)
{
this->back=this->back->prev;
this->back->next=NULL ;
}
else
{
nodeToRemove->prev->next=nodeToRemove->next;
nodeToRemove->next->prev=nodeToRemove->prev;
}
}
//Print the list from front
void dlist::printDListFront()
{
node* curr2;
curr2= this->front;
cout<<"n-----n";
cout<<"Queuen";
cout<<"-----n";
//cout<<"size:"<<getQueueSize()<<endl;
while(curr2!=NULL)
{
cout<<" |"<<curr2->value<<"|";
curr2=curr2->next;
}
cout<<endl;
}// print the Double Linked List from front
// print the Double Linked List from backwards
void dlist::printDListBack()
{
node* curr2;
curr2= this->back;
cout<<"n-----n";
cout<<"Queuen";
cout<<"-----n";
//cout<<"size:"<<getQueueSize()<<endl;
while(curr2!=NULL)
{
cout<<" |"<<curr2->value<<"|";
curr2=curr2->prev;
}
cout<<endl;
}// print the Double Linked List from back
int main()
{
dlist *st ;
st= new dlist();
st->insertBack(8);
st->printDListFront ();
st->insertBack(5);
st->printDListFront ();
st->insertBack(6);
st->printDListFront ();
st->insertFront(1) ;
st->printDListFront ();
st->insertFront(3) ;
st->printDListFront ();
st->insertBack(7);
st->printDListFront ();
st->removeFront();
st->printDListFront ();
st->removeBack();
st->printDListFront ();
}
现在,我如何让程序显示我的信息如下图所示……(输出)比如如何显示一个空列表?
List created using an empty value of 0.
Empty list:
Going forwards, I see channel 0
Going forwards, I see channel 0
Going forwards, I see channel 0
Current channel is 0
Going backwards, I see channel 0
Going backwards, I see channel 0
Going backwards, I see channel 0
Single node:
Going forwards, I see channel 2
Going forwards, I see channel 2
Going forwards, I see channel 2
Current channel is 2
Going backwards, I see channel 2
Going backwards, I see channel 2
Going backwards, I see channel 2
Multiple nodes:
Going forwards, I see channel 2
Going forwards, I see channel 3
Going forwards, I see channel 7
Going forwards, I see channel 9
Going forwards, I see channel 10
Going forwards, I see channel 44
Going forwards, I see channel 2
Going forwards, I see channel 3
Going forwards, I see channel 7
Going forwards, I see channel 9
Going forwards, I see channel 10
Going forwards, I see channel 44
Current channel is 44
Going backwards, I see channel 10
Going backwards, I see channel 9
Going backwards, I see channel 7
Going backwards, I see channel 3
Going backwards, I see channel 2
Going backwards, I see channel 44
Going backwards, I see channel 10
Going backwards, I see channel 9
Going backwards, I see channel 7
Going backwards, I see channel 3
Going backwards, I see channel 2
Going backwards, I see channel 44
List de-allocated.
您在这里处理的是一个双重链表,从您的问题中我可以理解,您希望在导航或创建节点时显示适当的消息。
要做到这一点,您需要在Main()cout<<
语句。
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 需要帮助设置在C++中使用的Potrace
- 如何在c++中打印目录
- 在指针的帮助下,文本文件中单词的频率
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 从函数打印 CLI 帮助消息后,我应该如何干净地退出 C++ 程序?
- 如何使用类中的函数从 main 打印变量,帮助理解 OOP c++
- infix/postfix/prefix程序.想要使用我的printresult()函数打印结果.需要帮助获得输出
- 需要帮助打印出字符串
- 需要帮助在同一行打印不带空格的序列
- c++帮助循环打印星号
- 初学者帮助:用sum打印Table
- c++语法,打印帮助
- 链表需要帮助无法打印我的数据.想要添加功能.在 C - C++ 中
- 需要帮助解引用这个uchar*来打印它的内容
- 使用boost::program_options打印普通参数和位置参数的帮助