Thread: Splitting in a linked list class

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    24

    Splitting in a linked list class

    I'm trying to develop a program that stores a list of hw assignments in the form of "Date,Assignment name,grade". I have functions that add,remove, and delete from a list, but I would also like to search and delete a particular date found in the list. From here, split - Splitting a string in C++ - Stack Overflow I've tested Evan Teran's method to split a string, but I'm unsure of how to implement it in my code.

    List.h
    Code:
    #ifndef LIST_H
    #define    LIST_H
    #include <string>
    using namespace std;
    class List{
    private: 
        
       typedef struct node{
           string hw;
           string grade;
           string date;
         
           
            node* next;
            
            
        }* Node;
        
        
        Node head;
        Node current;
        Node temp;
    
        
    public:    
        List();
        void AddAssign(string addData);
        void DeleteAssign(string delData);
        void PrintList();
          
        
    };
    
    
    #endif    /* LIST_H */
    List.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include "List.h"
    using namespace std;
    
    List::List(){
        head = NULL;
        current = NULL;
        temp = NULL;
        
    }
    
    /*
     @function - addAssign
     * @pre - 
     * @post
     * @return - adds a list of elements to 
     * assignment list. 
     */
    void List::AddAssign(string addData){
        Node n = new node;
        n->next = NULL;
        n->hw = addData;//initializes hw
        
        if(head != NULL){
            current = head;
            while(current->next != NULL){//next node is not at end of list
                current = current->next;//go to next node 
            }
            current->next = n;//next node is new
        }
        else{
            head = n;//newest node is head of list
        }
    }
    
    void List::DeleteAssign(string delData){
        Node delPtr = NULL;
        temp = head;
        current = head;
        while(current != NULL && current->hw != 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";
    }
       
        
        
    }
    
     void List::PrintList(){
            current = head;
            while(current!= NULL){//is not last node
                cout << current->hw << endl;
                current = current->next;
            }
                    
        }

    listmain.cpp
    Code:
    #include <cstdlib>
    #include "List.h"
    #include <string>
    #include <iostream>
    #include <list>
    using namespace std;
    
    
    int main(int argc, char** argv) {
        
        List assign;
        string date = "3/9/13";
        
    
        assign.AddAssign("3/4/13,Calculus,77");
        assign.AddAssign("3/7/13,Programming,88");
        assign.AddAssign("3/15/13,English,89");
        assign.AddAssign("3/23/13,Physics,78");
        
        cout<<"Starting list..." << endl; 
        assign.PrintList();
      
        
       
          
        return 0;
    }
    Last edited by FloatingButter; 03-09-2013 at 04:01 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