2011-08-29 82 views
1

我寫了一個小程序C++語法,打印幫助

#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<<"Queue\n"; 
    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<<"Queue\n"; 
    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(); 
} 

現在,我怎麼能得到的程序來顯示我的信息 如下圖所示...(OUTPUT) 喜歡我怎麼能顯示一個空的列表?

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. 
+0

這是功課?你到目前爲止嘗試了什麼? – Dawson

+0

它看起來像你已經瞭解了列表,並從輸出中判斷你將添加* iterators *。你的課程材料應該足夠詳細地涵蓋它們,至少可以開始。 – molbdnilo

回答

0

您在這裏處理一個雙向鏈表,從我可以從你的問題了解你想,當你瀏覽或創建節點,以顯示相應的消息。

要做到這一點,你需要插入您從主調用各項功能的相應cout<<語句()

+0

你能證明我是如何添加一個count語句的例子嗎?請 – bbbb

+0

我可以編寫程序...它並不困難,但我敦促你自己做。它會幫助你很多。請參考本教程獲得進一步的幫助.... http://www.cplusplus.com/doc/tutorial/basic_io/ – jayanth

+0

你能寫出來嗎?我一直試圖做它現在一整天,沒有運氣:( – bbbb