C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 307,221 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,695 people online right now. Registration is fast and FREE... Join Now!




Linked list

 

Linked list, Problem in my code, please help

CIS_Soldier87

30 Oct, 2007 - 05:51 AM
Post #1

New D.I.C Head
*

Joined: 30 Oct, 2007
Posts: 2


My Contributions
Good evening everyone.

I'm a CIS student and I wrote an implementation for a Linked List code while studying for my test tomorrow.

Everything seems to work perfectly except that there are four problems that need solutions:

CODE


# include <iostream>
using namespace std;

# include <cstdlib>
# include <ctime>

# include <assert.h>


template <class E>

class LinkedList

{

protected:

    struct node
    {

        E data;
        node *next;

    };

    node *myCurrent;
    node *myPrevious;
    node *myFirst;
     int mySize;

     node* getNode(const E &e)
{

     node* newNode = new node;
     assert (newNode !=0 );

     newNode -> data = e;
     newNode -> next = 0;

     return newNode;
}

     void removeNode(E val)
     {

         node* remv;

             remv = myCurrent;

              myPrevious -> next = myCurrent->next;
             myCurrent -> next = NULL;
             myCurrent = myPrevious;

             delete remv; /*next = NULL*/

            
             myCurrent = myCurrent -> next;
            
             //nextNode();

     }

public:
    
    LinkedList<E>()
        : myCurrent(0), myPrevious(0)
    {

        mySize = 0;
        myFirst = 0;

    }

E access()
{
     assert (myCurrent != 0);

     return myCurrent -> data;
    
}



void nextNode()
{
     assert (myCurrent != 0);

     myPrevious = myCurrent;
     myCurrent = myCurrent -> next;

}

void insert (const E &e1)
{

     node* newnode = getNode(e1);

     if (mySize != 0 || myCurrent == myFirst)

         myFirst = newnode;

     else

         myPrevious->next = newnode;

     newnode->next = myCurrent;
     myCurrent = newnode;
     mySize++;

    



}

void First()
{

     assert (mySize != 0);

     myCurrent = myFirst;
     myPrevious = NULL;

}


    LinkedList( LinkedList& lcopy)
        //:mySize(0),myCurrent(0),myPrevious(0),myFirst(0)
{

     lcopy.First();

     while (lcopy.myCurrent->next != NULL)
     {
         insert (lcopy.myCurrent->data);
         lcopy.nextNode();
         nextNode();

         lcopy.mySize++;
     }
     /*mySize = lcopy.mySize;*/
};


void printList()
{
     if (mySize == 0 || myFirst == NULL)
         cout << "List is empty. " << '\n';

     else

        First();

         for (int i = 0; i < mySize; i++)
         {
             cout << myCurrent -> data << ' ';
                          
             nextNode();
         }
            
}

void removeValue(E v)
{
     First();

     for (int i = 0; i < mySize; i++)
     {

         if (myCurrent -> data == v)
         {
             removeNode(v);
         }






             //myPrevious -> next = myCurrent->next;
             //delete myCurrent -> next; /*next = NULL*/
             //myCurrent = myPrevious -> next;
             //nextNode();
        
     }
        
     }

};





     void main()

     {

         LinkedList<int> l1;
         /*LinkedList<int> l2(l1);*/
        
         srand(time(0));

         int b = 1;

          for (int i = 14; i >= 1; i--)
          {
              l1.insert(b);
              b++;
              l1.nextNode();
          
          }

         l1.printList();

         l1.removeValue(4);

         cout << '\n';
         cout << '\n';

         l1.printList();

     }

        







First one: is in the copy constructor, it produces a runtime error.

Second one: in either the nextNode() function or the insert() function, a runtime error occurs when
attempting to call the nextNode() function from the main();

Third one: is with printList() function, it prints the elements of the list oppositely (i.e., 54321 instead of 12345).

Fourth one: Is in the removeNode() function, it doesn't produce any errors, but it still doesn't work.

Please help me.
Thanks.

This post has been edited by CIS_Soldier87: 30 Oct, 2007 - 06:54 AM

User is offlineProfile CardPM
+Quote Post


Posts in this topic
CIS_Soldier87   Linked list   30 Oct, 2007 - 05:51 AM

Reply to this topicStart new topic

Time is now: 11/21/09 10:33PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month