Thread: Linked Lists in C++

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Angry Linked Lists in C++

    Help!

    I can not figure out this linked list using a date. I got it to work using a number, but the teacher wants us to modify the program and make it work with a date. Can you look at my code and tell me where to I make this change at? Everything I try comes out with . Any helpful code or advise is appreciated!

    Thanks Bro.

    Same output should be like:
    What value do you wish to append?
    Enter the month: 10
    Enter the day: 21
    Enter the year: 2001
    Here is the list
    10/21/2001

    This is my working code for a number

    #include <stdlib.h>
    #include <iostream.h>
    template <class T>
    class LinkedList
    {
    private:
    struct ListNode
    {

    T value;
    //char date[20];
    //int day, month, year, value;
    struct ListNode *next;
    };
    ListNode *head;
    public:
    LinkedList(void)
    { head = NULL; }
    ~LinkedList(void);
    void appendNode(T);
    void insertNode(T);
    void deleteNode(T);
    void displayList(void);
    };
    //**************************************************
    // appendNode appends a node containing the *
    // value pased into num, to the end of the list. *
    //**************************************************
    template <class T>
    void LinkedList<T>::appendNode(T num)
    {
    ListNode *newNode, *nodePtr;
    // Allocate a new node & store num
    newNode = new ListNode;
    newNode->value = num;
    newNode->next = NULL;
    // If there are no nodes in the list
    // make newNode the first node
    if (!head)
    head = newNode;
    else // Otherwise, insert newNode at end
    {
    // Initialize nodePtr to head of list
    nodePtr = head;
    // Find the last node in the list
    while (nodePtr->next)
    nodePtr = nodePtr->next;
    // Insert newNode as the last node
    nodePtr->next = newNode;
    }
    }
    //**************************************************
    // displayList shows the value *
    // stored in each node of the linked list *
    // pointed to by head. *
    //**************************************************
    template <class T>
    void LinkedList<T>::displayList()
    {
    ListNode *nodePtr;
    nodePtr = head;
    while (nodePtr)
    {
    cout << nodePtr->value << endl;
    nodePtr = nodePtr->next;
    }
    }
    //**************************************************
    // The insertNode function inserts a node with *
    // num copied to its value member. *
    //**************************************************
    template <class T>
    void LinkedList<T>::insertNode(T num)
    {
    ListNode *newNode, *nodePtr, *previousNode;
    // Allocate a new node & store num
    newNode = new ListNode;
    newNode->value = num;

    // If there are no nodes in the list
    // make newNode the first node
    if (!head)
    {
    head = newNode;
    newNode->next = NULL;
    }
    else // Otherwise, insert newNode
    {
    // Initialize nodePtr to head of list
    nodePtr = head;
    // Skip all nodes whose value member is less
    // than num.
    while (nodePtr != NULL && nodePtr->value < num)
    {
    previousNode = nodePtr;
    nodePtr = nodePtr->next;
    }
    // If the new node is to be the 1st in the list,
    // insert it before all other nodes.
    if (previousNode == NULL)
    {
    head = newNode;
    newNode->next = nodePtr;
    }
    else // Otherwise, insert it after the prev. node.
    {
    previousNode->next = newNode;
    newNode->next = nodePtr;
    }
    }
    }
    //**************************************************
    // The deleteNode function searches for a node *
    // with num as its value. The node, if found, is *
    // deleted from the list and from memory. *
    //**************************************************
    template <class T>
    void LinkedList<T>::deleteNode(T num)
    {
    ListNode *nodePtr, *previousNode;
    // If the list is empty, do nothing.
    if (!head)
    return;

    // Determine if the first node is the one.
    if (head->value == num)
    {
    nodePtr = head->next;
    delete head;
    head = nodePtr;
    }
    else
    {
    // Initialize nodePtr to head of list
    nodePtr = head;
    // Skip all nodes whose value member is
    // not equal to num.
    while (nodePtr != NULL && nodePtr->value != num)
    {
    previousNode = nodePtr;
    nodePtr = nodePtr->next;
    }
    // Link the previous node to the node after
    // nodePtr, then delete nodePtr.
    previousNode->next = nodePtr->next;
    delete nodePtr;
    }
    }
    //**************************************************
    // Destructor *
    // This function deletes every node in the list. *
    //**************************************************

    template <class T>
    LinkedList<T>::~LinkedList()
    {
    ListNode *nodePtr, *nextNode;
    nodePtr = head;
    while (nodePtr != NULL)
    {
    nextNode = nodePtr->next;
    delete nodePtr;
    nodePtr = nextNode;
    }
    }

    void main(void)
    {
    // Date value;//You can change this to work with integers or dates
    LinkedList<int> List;
    int Response = -1;
    int Value;
    cout<<"What do you want to do"<<endl;
    while(Response != 1)
    {
    cout<<"(1) Exit"<<endl;
    cout<<"(2) Append a node"<<endl;
    cout<<"(3) Insert a node"<<endl;
    cout<<"(4) Delete a node"<<endl;
    cout<<"(5) Delete the list"<<endl;
    cout<<"(6) Display the list"<<endl;
    cin>>Response;
    switch(Response)
    {
    case 1:
    cout<<"Thanks for visiting. Bye."<<endl;
    break;
    case 2:
    cout<<"What value do you wish to append?"<<endl;
    cin>>Value;
    List.appendNode(Value);
    cout<<"Here is the list"<<endl;
    List.displayList();
    break;
    case 3:
    cout<<"What value do you wish to insert?"<<endl;
    cin>>Value;
    List.insertNode(Value);
    cout<<"Here is the list"<<endl;
    List.displayList();
    break;
    case 4:
    cout<<"What value do you wish to delete?"<<endl;
    cin>>Value;
    List.deleteNode(Value);
    cout<<"Here is the list"<<endl;
    List.displayList();
    break;
    case 5:
    cout<<"Are you sure you want to delete the list(Y for yes)?"<<endl;
    char Answer;
    cin>>Answer;
    if(Answer=='Y'||Answer=='y')
    {
    // List.deleteNode(Value);
    List.~LinkedList();
    }
    else
    {
    cout<<"Here is the list"<<endl;
    List.displayList();
    // system("PAUSE");
    }
    break;

    case 6:
    cout<<"Here is the list"<<endl;
    List.displayList();
    break;
    default:
    cout<<"Please enter a value 1 - 6"<<endl;
    }
    }
    system("PAUSE");
    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    A) Use code tags.

    B) Instead of using a template, just drop it and make 3 ints for month, day, and year.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This is the easiest way, assuming that your compiler isn't an old cheap one that schools seem to love and never conforms to the current standard. But before I post the code, two things:

    1) Don't use void main, it's wrong.
    2) You really don't want to explicitly call the destructor of your class. Bad things can happen. I didn't write a destroyList method for you, that's your job. Just remember to remove the destructor call.
    3) Please for the love of god use the code tags when your source is more than 10 lines. I could have fixed this in less than a minute, but it took me considerably longer because I had to reformat everything.
    4) I didn't change the interface at all, just the processing. Once again that is your job.

    Okay, four things, but you know what they say about mathematicians. There are three kinds, those who can count and those who can't. Good thing I'm a programmer.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
     
    template <class T> 
    class LinkedList 
    { 
    private: 
      struct ListNode 
      {  
        string value; 
        struct ListNode *next; 
      }; 
      ListNode *head; 
    public: 
      LinkedList(void) 
      { head = NULL; } 
      ~LinkedList(void); 
      void appendNode(T); 
      void insertNode(T); 
      void deleteNode(T);
      void displayList(void); 
    }; 
    
    template <class T> 
    void LinkedList<T>::appendNode(T num) 
    { 
      ListNode *newNode, *nodePtr; 
      newNode = new ListNode; 
      newNode->value = num; 
      newNode->next = NULL; 
      if (!head) 
        head = newNode; 
      else
      { 
        nodePtr = head; 
        while (nodePtr->next) 
          nodePtr = nodePtr->next; 
        nodePtr->next = newNode; 
      } 
    } 
    
    template <class T> 
    void LinkedList<T>::displayList() 
    { 
      ListNode *nodePtr; 
      nodePtr = head; 
      while (nodePtr) 
      { 
        cout << nodePtr->value << endl; 
        nodePtr = nodePtr->next; 
      } 
    } 
    
    template <class T> 
    void LinkedList<T>::insertNode(T num) 
    { 
      ListNode *newNode, *nodePtr, *previousNode; 
      newNode = new ListNode; 
      newNode->value = num; 
      if (!head) { 
        head = newNode; 
        newNode->next = NULL; 
      } 
      else{ 
        nodePtr = head; 
        while (nodePtr != NULL && nodePtr->value < num) { 
          previousNode = nodePtr; 
          nodePtr = nodePtr->next; 
        } 
        if (previousNode == NULL) { 
          head = newNode; 
          newNode->next = nodePtr; 
        } 
        else { 
          previousNode->next = newNode; 
          newNode->next = nodePtr; 
        } 
      } 
    } 
    
    template <class T> 
    void LinkedList<T>::deleteNode(T num) 
    { 
      ListNode *nodePtr, *previousNode; 
      if (!head) 
        return; 
      if (head->value == num) { 
        nodePtr = head->next; 
        delete head; 
        head = nodePtr; 
      } 
      else { 
        nodePtr = head; 
        while (nodePtr != NULL && nodePtr->value != num) { 
          previousNode = nodePtr; 
          nodePtr = nodePtr->next; 
        } 
        previousNode->next = nodePtr->next; 
        delete nodePtr; 
      } 
    } 
    
    template <class T> 
    LinkedList<T>::~LinkedList() 
    { 
      ListNode *nodePtr, *nextNode; 
      nodePtr = head; 
      while (nodePtr != NULL) { 
        nextNode = nodePtr->next; 
        delete nodePtr; 
        nodePtr = nextNode; 
      } 
    } 
    
    int main(void) 
    { 
      LinkedList<string> List; 
      int Response = -1; 
      string Value; 
      cout<<"What do you want to do"<<endl; 
      while(Response != 1) { 
        cout<<"(1) Exit"<<endl; 
        cout<<"(2) Append a node"<<endl; 
        cout<<"(3) Insert a node"<<endl; 
        cout<<"(4) Delete a node"<<endl; 
        cout<<"(5) Delete the list"<<endl; 
        cout<<"(6) Display the list"<<endl; 
        cin>>Response; 
        switch(Response) 
        { 
        case 1: 
          cout<<"Thanks for visiting. Bye."<<endl; 
          break; 
        case 2: 
          cout<<"What value do you wish to append?"<<endl; 
          cin>>Value; 
          List.appendNode(Value); 
          cout<<"Here is the list"<<endl; 
          List.displayList(); 
          break; 
        case 3: 
          cout<<"What value do you wish to insert?"<<endl; 
          cin>>Value; 
          List.insertNode(Value); 
          cout<<"Here is the list"<<endl; 
          List.displayList(); 
          break; 
        case 4: 
          cout<<"What value do you wish to delete?"<<endl; 
          cin>>Value; 
          List.deleteNode(Value); 
          cout<<"Here is the list"<<endl; 
          List.displayList(); 
          break; 
        case 5: 
          cout<<"Are you sure you want to delete the list(Y for yes)?"<<endl; 
          char Answer; 
          cin>>Answer; 
          if(Answer=='Y'||Answer=='y') { 
            List.~LinkedList(); 
          } 
          else { 
            cout<<"Here is the list"<<endl; 
            List.displayList(); 
          } 
          break; 
        case 6: 
          cout<<"Here is the list"<<endl; 
          List.displayList(); 
          break; 
        default: 
          cout<<"Please enter a value 1 - 6"<<endl; 
        } 
      } 
      cin.get();
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM