Thread: Splitting in a linked list class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    "found" is a node right?
    A pointer to a node, actually.

    My function "SearchAssign" doesn't return the node found from the list but it returns a message with the found node in it. From there it calls "DeleteAssign" to delete the line from the node.
    That's fine.

    I am curious as to why it deletes my entire line of assignment instead of just current->date since that is what is being called in "SearchAssign".
    I thought that's what you wanted.

    Also how could a insert a new assignment at the location where I've deleted the searched date?
    Actually... how can I put this? If you are simply erasing the dates of given assignments and then changing them, it might be better performance wise if you simply overwrite assignments. For example, you will have an additional field in an assignment that marks when it is "done." From there on, the assignment doesn't matter. When you have a new assignment to put in its place, you can first look for done assignments to overwrite with new data, and mark them not done.

    If the list gets too long, you can write a purge function to actually delete the nodes from memory.

    I'm recommending this approach mainly because if you do it this way, you can invoke new less often. You would only need to invoke it when there are no old assignments around to reuse. And since this way leaves the list in tact, no special effort is required to find out where an old assignment was. In fact, after you delete an object normally there is really no guaranteed way of going back to that.

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    24
    Quote Originally Posted by whiteflags View Post

    I thought that's what you wanted.
    It is, I was just having a hard time understanding how its delete it. Originally,I wasn't sure whether or not my "DeleteAssign" function would work correctly. I thought that since "current->date" was going into DeleteAssign, that only that specific
    string in the list that had been found would be deleted and when I had called "PrintAssign", it would print the following:
    Code:
    "Statistics,43"
    //... 
    "English 101,100"
    instead of removing the whole line itself.
    Quote Originally Posted by whiteflags View Post
    Also how could a insert a new assignment at the location where I've deleted the searched date? Actually... how can I put this? If you are simply erasing the dates of given assignments and then changing them, it might be better performance wise if you simply overwrite assignments. For example, you will have an additional field in an assignment that marks when it is "done." From there on, the assignment doesn't matter. When you have a new assignment to put in its place, you can first look for done assignments to overwrite with new data, and mark them not done.

    If the list gets too long, you can write a purge function to actually delete the nodes from memory.

    I'm recommending this approach mainly because if you do it this way, you can invoke new less often. You would only need to invoke it when there are no old assignments around to reuse. And since this way leaves the list in tact, no special effort is required to find out where an old assignment was. In fact, after you delete an object normally there is really no guaranteed way of going back to that.
    I apologize, I was reading my hw assignment wrong. In addition to searching for a due date and removing the assignment, my program is also suppose to search for a due date and insert a new assignment after that due date instead of removing it and inserting a new one at that location.
    Last edited by FloatingButter; 03-12-2013 at 07:31 AM.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    24
    I've managed to figure out how to insert an assignment after I've found a certain due date. I've ended up making two search function: "search_and_remove" and "search_and_insert" that removes and inserts a new assignment in a list. I've tested it a couple of times and it seems to work ok, except when the date to be searched is the last one in the list.


    I'm excluding my h file since all I've added was my function
    definitions for each search function. Any other critics on how I could improve my code would be appreciated as well.
    list.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include "List.h"
    
    
    
    using namespace std;
    
    List::List(){
        head = NULL;
        current = NULL;
        temp = NULL;
        
    }
    
    /*
      adds a list of elements to 
     * assignment list. 
     */
    void List::AddAssign(string addDate,string addName,string addGrade){
        Node n = new node;
        n->next = NULL;
        n->date = addDate;
        n->name = addName;
        n->grade = addGrade;
      
        
        if(head != NULL){
            current = head;
            while(current->next != NULL){
                current = current->next;
            }
            current->next = n;
        }
        else{
            head = n;
        }
    }
    /*
     Deletes an Assignment 
     */
    void List::DeleteAssign(string delData){
        Node delPtr = NULL;
        temp = head;
        current = head;
        while(current != NULL && current->date  != delData){
         temp = current;
         current = current->next;  
        }
        if(current == NULL){
        cout << delData << " was not in the list\n";
        delete delPtr;
        
    }else{
            delPtr = current;
            current = current->next;
            temp->next = current;
            if(delPtr == head){
                head = head->next;
                temp = NULL;
                
            }
            delete delPtr;
            cout << "The Value " << delData << " was deleted\n";
    } 
    }
    
    /*
     Prints the list.
     */
     void List::PrintList(){
            current = head;
            while(current!= NULL){
               
                
       cout << current->date<< ","<<current->name <<","
               <<current->grade <<endl;
        
                current = current->next;
            }
                    
        }
     
     /*
      Searches the current date in the list and 
      * deletes the assignment from the list 
      * if found.  
      */
     
     void List::search_and_remove(string findDate){
         current = head;
    
         while(current!= NULL){
             if(current->date == findDate){     
                 cout << current->date << " found\n";
                DeleteAssign(current->date);
             } 
        current = current->next;
         }
             
         
     }
     
    /*
     Searches and inserts a new assignment if a date is found.
     */
     void List::search_and_insert(string findDate,string date,string name,string grade){
           current = head;
                 Node insertData = new node;
                 insertData->date = date;
                 insertData->name = name;
                 insertData->grade = grade;
                
         while(current!= NULL){
             if(current->date == findDate){     
                 cout << current->date << " found\n";
              //inserts the new assignment
                insertData->next = current->next;
                 current->next = insertData;
             }                            
        current = current->next;
         }
         
     }
    listmain.cpp
    Code:
    #include <cstdlib>
    #include "List.h"
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    
    int main(int argc, char** argv) {
     
    //Tests a linked list.
    
        List assign;   
        
        
      assign.AddAssign("3/14/13","Statistics","43");
       assign.AddAssign("3/12/13","Geometry","43");
      assign.AddAssign("3/4/13","Calculus","45");
         assign.AddAssign("3/12/13","Java","43");
         assign.AddAssign("4/11/13","Biology","55");    
      assign.AddAssign("5/13/13","Economics","77");
       assign.AddAssign("3/11/13","English 101","100");
      assign.AddAssign("3/9/13","French","86");
         assign.AddAssign("5/23/13","Spanish","55");
         assign.AddAssign("3/3/13","Biology","55");
       
      cout << "Printing list...\n";
       assign.PrintList();
      
       
       cout << "Deleting assignments with 3/12/13\n";
       assign.search_and_remove("3/12/13");
       
       cout<< "New list without dates 3/12/13\n";
       assign.PrintList();
    
    
       cout << "search assignments with dates 3/9/13\n";
       assign.search_and_insert("3/9/13","3/24/13","PhysicsII","89");
    
       cout << "updated list with inserted assignment"
               " 3/24/13 PhysicsII 89 \n";
       assign.PrintList();
         cout << "End of list. \n"; 
    
    return 0;
    }
    Last edited by FloatingButter; 03-12-2013 at 07:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list of a class object?....
    By chadsxe in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2005, 03:15 PM
  2. splitting linked list recursive vs. iterative
    By Micko in forum C Programming
    Replies: 7
    Last Post: 03-17-2005, 05:51 PM
  3. Basic Linked List class
    By ExCoder01 in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2003, 02:15 AM
  4. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM
  5. STL Linked List of class within class
    By NixPhoeni in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2001, 10:17 AM

Tags for this Thread