Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




Linked List Appending Time

 
Reply to this topicStart new topic

Linked List Appending Time

ifoam
post 30 Oct, 2006 - 08:37 AM
Post #1


D.I.C Head

**
Joined: 26 Oct, 2006
Posts: 54



Thanked 1 times
My Contributions


What I'm trying to do it create a program that generates 500,000 random first names, last names, and id numbers. Then put them in a linked list, and sort them. I've decided to use the merge sort because of the large amount of data.

when just appending nodes, the code take 1.5 hours just to append. how can i change my appendNode function to increase the speed of this program?


CODE

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>

using namespace std;

class LinkedList
{
private:
        class ListNode
        {
              friend class LinkedList;
              string fname;
              string lname;
              int id;
              ListNode *next;
              ListNode(string fname1, string lname1, int id1, ListNode *next1 = NULL)
              {
              fname = fname1;
              lname = lname1;
              id = id1;
              next = next1;
              }
        };
        ListNode *head;
public:
       LinkedList()
       {head=NULL;}
       ~LinkedList();
       void appendNode(string, string, int);
};

void LinkedList::appendNode(string fname, string lname, int id)
{
     if(head==NULL)
        head=new ListNode(fname, lname, id);
     else
     {
         ListNode *nodePtr;
         nodePtr = head;
         while (nodePtr->next != NULL)
               nodePtr = nodePtr->next;
         nodePtr->next = new ListNode(fname, lname, id);
     }
}

LinkedList::~LinkedList()
{
     ListNode *nodePtr, *nextNodePtr;
    
     nodePtr = head;
     while(nodePtr != NULL)
     {
          nextNodePtr = nodePtr->next;
          delete nodePtr;
          nodePtr = nextNodePtr;
     }
}
#endif


CODE

#include "linkedlist.h"
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <cstdlib>

using namespace std;

void rword (char *word)
{
int len = rand () % 6 + 1;
word [len] = 0;
while (len) word [--len] = 'a' + rand () % 26;
}

int main ()
{
    char word[7];
    char word2[7];
    int x=0;
    srand(time(0));
    LinkedList list;
    
    while (x<=500000)
    {
      rword(word);
      rword(word2);
      list.appendNode(word, word2, x);
      x++;
      cout << "Node " << x << " inserted." << endl;
    }
    cout << "Program ready to sort." << endl;
}
User is offlineProfile CardPM

Go to the top of the page

UMTopSpinC7
post 30 Oct, 2006 - 09:44 AM
Post #2


New D.I.C Head

*
Joined: 20 Oct, 2006
Posts: 47


My Contributions


QUOTE(ifoam @ 30 Oct, 2006 - 09:37 AM) *

when just appending nodes, the code take 1.5 hours just to append. how can i change my appendNode function to increase the speed of this program?


Use a growable array (vector) instead of a linked list if you want it to be faster. If you are set on using a linked list then you might want to keep (as part of the linked list class) a pointer to the last element in a list. That way you dont have to run through the whole list every time you want to add something to it. Or you could just add to the front of the list instead of the end. The reason yours is taking so long is because it has to go through the whole list every time.

This post has been edited by UMTopSpinC7: 30 Oct, 2006 - 09:48 AM
User is offlineProfile CardPM

Go to the top of the page

ifoam
post 30 Oct, 2006 - 09:49 AM
Post #3


D.I.C Head

**
Joined: 26 Oct, 2006
Posts: 54



Thanked 1 times
My Contributions


care to explain how to keep a pointer at the end?

i have to keep a linked list becaues it is a class project :\
User is offlineProfile CardPM

Go to the top of the page

UMTopSpinC7
post 30 Oct, 2006 - 10:02 AM
Post #4


New D.I.C Head

*
Joined: 20 Oct, 2006
Posts: 47


My Contributions


QUOTE(ifoam @ 30 Oct, 2006 - 10:49 AM) *

care to explain how to keep a pointer at the end?

i have to keep a linked list becaues it is a class project :\


It might be easier to just add to the front of the list.

CODE
void LinkedList::appendNode(string fname, string lname, int id)
{
     if(head==NULL)
        head=new ListNode(fname, lname, id);
     else
     {
         ListNode *temp = head; //Copy what the head used to point to
         head = new ListNode(fname, lname, id); //Creates the new node
         nodePtr->next = temp; //new node's next point is now what head used to point to
     }
}


Is there any reason you dont want to add to the front? It seems like the best solution to me. It has been a while since i have done anything with linked lists though so you might want to make sure this works. Let me know if it doesn't.
User is offlineProfile CardPM

Go to the top of the page

ifoam
post 30 Oct, 2006 - 02:13 PM
Post #5


D.I.C Head

**
Joined: 26 Oct, 2006
Posts: 54



Thanked 1 times
My Contributions


i get an error with nodePtr->next = temp

nodePtr being undeclared

btw, appending to the front is fine because i'm going to sort it afterwards anyways

This post has been edited by ifoam: 30 Oct, 2006 - 02:18 PM
User is offlineProfile CardPM

Go to the top of the page

ifoam
post 30 Oct, 2006 - 04:14 PM
Post #6


D.I.C Head

**
Joined: 26 Oct, 2006
Posts: 54



Thanked 1 times
My Contributions


got it. before it would take 1.5 hours to add and generate 500k nodes

now its 26 secs smile.gif what a drastic improvement!

CODE

void LinkedList::appendNode2(string fname, string lname, int id)
{
     if(head==NULL)
        head=new ListNode(fname, lname, id);
     else
     {
         ListNode *nodePtr;
         nodePtr = head;
         head = new ListNode(fname, lname, id, nodePtr);
        
     }
}
User is offlineProfile CardPM

Go to the top of the page

UMTopSpinC7
post 31 Oct, 2006 - 08:30 AM
Post #7


New D.I.C Head

*
Joined: 20 Oct, 2006
Posts: 47


My Contributions


Hahaha... good to hear that it's working smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 05:58AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month