2016-04-16 11 views
1

こんにちはすべて私はここまで、そして比較的プログラミングにも興味があります。私はリンクされたリスト内のノードの範囲を削除しようとしていますが、その方法を理解することはできません。私はforループを使ってみましたが、delete関数を文字列に渡しているので、ノードをインクリメントして最終的なカウントまで削除することはできません。どんな助けでも大歓迎です!代わりに、ノードの名前(INAME)で削除するのでC++のノードの範囲を削除する

#include <iostream> 
    #define nullptr 0 
    #include <cstdlib> 
    #include <algorithm> 
    #include <string> 
    #include <conio.h> 



    using namespace std; 
    int menu(); 

    class ItemList { 
     private: 
      struct ListNode{ 
       string IName; 
       string QQuantity; 
       string PPrice; 
       double value; 
       struct ListNode * next; 
        }; 
    ListNode *head; 
     public: 
      ItemList() 
       { 
        head = new ListNode; 
        head->next=nullptr; 
       } 
       ~ItemList(); 

       void insertNode(string Item, string Quantity, string Price) 
       { 
        ListNode *newNode; 
        ListNode *nodePtr; 
        ListNode *previousNode=nullptr; 

        newNode=new ListNode; 
        newNode->IName=Item; 
        newNode->QQuantity=Quantity; 
        newNode->PPrice=Price; 

        if(!head) 
        { 
         head=newNode; 
         newNode->next=nullptr; 
        } 
        else 
        { 
         nodePtr=head; 
         previousNode=nullptr; 

         while(nodePtr != nullptr && nodePtr->IName < Item) 
         { 
          previousNode=nodePtr; 
          nodePtr=nodePtr->next; 
         } 
         if(previousNode==nullptr) 
         { 
          head=newNode; 
          newNode->next=nodePtr; 
         } 
         else 
         { 
          previousNode->next=newNode; 
          newNode->next=nodePtr; 
         } 
        } 
       } 
       void displayNode() 
       { 
        ListNode *nodePtr; 
        nodePtr=head->next; 

        while(nodePtr) 
        { 
         cout << nodePtr->IName << ", "; 
         cout << nodePtr->QQuantity << " "; 
         cout << "$" << nodePtr->PPrice << "\n" << endl; 
         nodePtr=nodePtr->next; 
        } 
       } 
       void modifyNode(string Item) 
       { 
       ListNode *nodePtr; 
       ListNode *nodePrev; 
       string newName, newQuantity, newPrice; 
       int modify; 
       if (!head) 
       { 
        return; 
        cout << "Store is empty." << endl; 
       } 
       else 
       { 
        nodePtr = head; 
        if (head->IName==Item) 
         nodePtr = head->next; 
        else 
        { 
         while (nodePtr != nullptr && nodePtr->IName != Item) 
         { 
          nodePrev = nodePtr; 
          nodePtr = nodePtr->next; 
         } 
        } 
        if (nodePtr) 
        { 
         cout << nodePtr->IName << "\t" << nodePtr->QQuantity << "\t" << nodePtr->PPrice << endl; 
         cout << "What would you like to change?\n"; 
         cout << "1. Item" << endl; 
         cout << "2. Quantity" << endl; 
         cout << "3. Price" << endl; 
         cout << "4. Whole Entry" << endl; 
         cin >> modify; 

         transform(newName.begin(), newName.end(), newName.begin(), ::toupper); 
         switch (modify) 
         { 
          case 1: 
           cout << "Change to what?\n"; 
           cin >> newName; 
           nodePtr->IName = newName; 
           break; 
          case 2: 
           cout << "Change to what?\n"; 
           cin >> newQuantity; 
           nodePtr->QQuantity = newQuantity; 
           break; 
          case 3: 
           cout << "Change to what?\n"; 
           cin >> newPrice; 
           nodePtr->PPrice = newPrice; 
           break; 
          case 4: 
           cout << "Change to what?\n"; 
           cin >> newName; 
           nodePtr->IName = newName; 
           cout << "Change to what?\n"; 
           cin >> newQuantity; 
           nodePtr->QQuantity = newQuantity; 
           cout << "Change to what?\n"; 
           cin >> newPrice; 
           nodePtr->PPrice = newPrice; 
         } 
        } 
        else 
         cout << "Person not found\n"; 
       } 
      } 

       void deleteNode(string Item) 
       { 
        ListNode *nodePtr; 
        ListNode *previousNode; 

        if(!head) 
         return; 
        if(head->IName==Item) 
        { 
         nodePtr=head->next; 
         delete head; 
         head=nodePtr; 
        } 
        else 
        { 
         nodePtr=head; 
         while(nodePtr!=nullptr && nodePtr->IName!=Item) 
         { 
          previousNode=nodePtr; 
          nodePtr=nodePtr->next; 
         } 
         if(nodePtr) 
         { 
          previousNode->next=nodePtr->next; 
          delete nodePtr; 
         } 
         else 
         { 
          cout << "Nothing to delete." << endl; 
         } 
        } 
       } 
      }; 


    ItemList::~ItemList() 
    { 
     ListNode *nodePtr; 
     ListNode *nextNode; 

     nodePtr=head; 
     while(nodePtr!=nullptr) 
     { 
      nextNode=nodePtr->next; 
      delete nodePtr; 
      nodePtr=nextNode; 
     } 
    } 

    int main() 
    { 
     ItemList pro; 
     int method; 
     while(method!=0) 
     { 
     int method=menu(); 
     system("cls"); 
     string It, Q, P; 
     switch(method) 
     { 
     case 1: 
      int count; 
      cout << "How many products would you like to put in?" << endl; 
      cin >> count; 
      system("cls"); 
      for(int i=0; i<count; i++) 
      { 
       cout << "Product #" << i + 1 << endl; 
       cout << "Enter the item name: "; 
       cin.sync(); 
       getline(cin,It); 
       transform(It.begin(), It.end(), It.begin(), ::toupper); 
       cout << "Enter the Quantity: "; 
       cin >> Q; 
       transform(Q.begin(), Q.end(), Q.begin(), ::toupper); 
       cout << "Enter the Price: "; 
       cin >> P; 
       pro.insertNode(It, Q, P); 
       cout << "\n"; 
      } 
      break; 

     case 2: 
      pro.displayNode(); 
      break; 

     case 3: 
      pro.displayNode(); 
      cout << "What product do you wish to modify? (by item name)" << endl; 
      cin.sync(); 
      getline(cin, It); 
      transform(It.begin(), It.end(), It.begin(), ::toupper); 
      system("cls"); 
      pro.modifyNode(It); 
      break; 

     case 4: 
      int del; 
      cout << "Do you wish to delete one product or more?" << endl; 
      cout << "1. One" << endl; 
      cout << "2. Range of Products" << endl; 
       cin >> del; 
       system("cls"); 
       switch(del) 
       { 
        case 1: 
         cout << "What product do you wish to delete? (by item name)" << endl; 
         pro.displayNode(); 
         cout << "\n"; 
         cin.sync(); 
         getline(cin,It); 
         transform(It.begin(), It.end(), It.begin(), ::toupper); 
         pro.deleteNode(It); 
         cout << "\n"; 
         break; 
        case 2: 
         int count; 
         cout << "How many?"; 
         cin >> count; 
         pro.displayNode(); 
         cout << "Starting where?" << endl; 
         cin >> It; 
         transform(It.begin(), It.end(), It.begin(), ::toupper); 
         for(int i=0; i<count; i++) 
         { 
          pro.deleteNode(It); 
          It(i); 
         } 
         break; 
       } 
      break; 

     case 5: 
      pro.~ItemList(); 
      cout << "All items deleted." << endl; 
      break; 

     case 0: 
      cout << "Exiting the program." << endl; 
      return 0; 
     } 
     system("pause"); 
     system("cls"); 
     } 
     return 0; 
    } 

    int menu() 
    { 
     string space1= "     "; 
     string space2= "         "; 
     int method; 
     cout << space1 << "What would you like to do to the Phone Book?" << endl; 
     cout << space2 << "1. Insert" << endl; 
     cout << space2 << "2. Display" << endl; 
     cout << space2 << "3. Modify" << endl; 
     cout << space2 << "4. Delete" << endl; 
     cout << space2 << "5. Empty" << endl; 
     cout << space2 << "0. Exit\n" << endl; 
     cout << space2; 
     cin >> method; 
     return(method); 
    } 
+0

ノードの範囲を削除することを定義すると、例がわかりやすくなります。 – Stephen

+0

deleteNode()関数にカウントを送信して、そこでforループを実行してみませんか?あなたは何を期待していますか?** "It(i);" **はあなたの削除ループで行いますか? –

+0

たとえば、ノード「ボール」をノード「人形」まで一度に削除したい場合 –

答えて

0

、あなたは与えられた名前のノードポインタを取得する関数を作成し、その後、ポインタの代わりに名前によって削除1にあなたの「deleteNode」関数を変更することができます。また、この関数は、次のノードポインタを返します(リスト内に他の要素がない場合はnull)。後続のノードを削除するために使用できます。

EDIT:非常に基本的なコード例:

char * get_pointer_by_name(string Name) 
{ 
    ListNode * curr_node = this->head; 

    while(curr_node != NULL) 
    { 
     if(curr_node->IName == Name) 
     { 
      return (char *) curr_node; 
     } 
     else 
     { 
      curr_node = curr_node->next; 
     } 
    } 

    // node not found 

} 

char * delete_node(char * pointer) 
{ 
    char * tmp = (char *) (ListNode *) pointer->next; // check this, it may be wrong 
    // this is a very simple and illustrative way to delete your node, not the right one 
    delete pointer; 
    // return the next node pointer 
    return tmp; 
} 

これが完了すると、あなたがしなければならないすべてはあなたが(get_pointer_by_nameで削除したいノードを取得)し、削除するために数回の反復であります残りのノードは次のようになります。

char * tmp = get_pointer_by_name("name"); 

while(tmp != NULL && count > 0) 
{ 
    tmp = deleteNode(tmp); 
    count--; 
} 
関連する問題