954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Linked List (reverse and alphebetical order)

Hi, I have two seperate programs below writing a short program for each. My programs run, but sometimes when I rearrange things in my program to try to get it to work, I will receive an error. I am tried to figure it out since last night, but I am unable to. Any help is appreciate. Thank you


I am currently having problems and need help with:

1) Printing the words in reverse order in my printList function

2) Printing the words in alphabetical order in my printList function

Example of reverse order:


Input File:

hi
hello
hey
bye
adios

Example Output

adios
bye
hey
hello
hi

Example of alphabetical order

Input File:

hi
hello
hey
bye
adios

Example Output

adios
bye
hello
hey
hi

Reverse order

#include <fstream>


using namespace std;
struct nodeType
{
string info;
nodeType *link;

};

void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first, nodeType*& last);


int main()
{

nodeType *first, *last;
string words;


ifstream inData;
ofstream outData;


createList(first,last);
printList(first,last);


system("PAUSE");
return 0;
}

void createList(nodeType*& first, nodeType*& last)
{

ifstream inData("input.txt");

string words;
int emptyList;
nodeType *newNode;

first = NULL;
last = NULL;

while(inData >> words)
{
newNode = new nodeType; // create new node
newNode->info = words;
newNode->link = NULL;

if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
} 


}

void printList(nodeType*& first,nodeType*& last) // print words in reverse order
{

ofstream outData("output.txt");

nodeType *current=first;
nodeType *reverse = NULL;

last=current->link;
current->link = reverse;
reverse = current;
current = last;

while (current != NULL)
{

outData << current->info <<endl;
current = current->link;
}
}


Alphabetical Order

#include <fstream>


using namespace std;
struct nodeType
{
    string info;
    nodeType *link;
    
};

void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first, nodeType*& last);


int main()
{
    
    nodeType *first, *last;
    string words;
    
    
    ifstream inData;
    ofstream outData;

    
    createList(first,last);
    printList(first,last);
    
    
  system("PAUSE");
   return 0;
}

void createList(nodeType*& first, nodeType*& last)
{
     
    ifstream inData("input.txt");
     
    string words;
    int emptyList;
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
    
    while(inData >> words)
    {
         newNode = new nodeType; // create new node
         newNode->info = words;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
    }   
    
    
}

void printList(nodeType*& first,nodeType*& last) // print words in alphabetical order
{
        
        ofstream outData("output.txt");
        
        nodeType *current=first;
        nodeType *alphab = NULL;
        
          first=current->link;
          current->link = alphab;
          last = current;
          alphab= last;
            
        while (current != NULL)
        {
          
          outData << current->info <<endl;
          current = current->link;
        }
}
NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

What is the error that is generated?

redburn
Light Poster
37 posts since May 2009
Reputation Points: 7
Solved Threads: 8
 

It is a windows error when i run the program that just tells me to close.

Does anyone see what is wrong with my code to figure out how to print out in reverse and alphabetical order?

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

just a thought, shouldn't the assignment in line 60 be:

last->link = &newNode;
sandeepss6s
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Does anyone know how to figure it out with me? I am trying to print my word list in reverse order and alphabetical order. My programs are in my first post.

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

I am still having problems figuring out how to print out the words in reverse order.

Input file:

hi
hey
hello
bye
adios


Update code for:

reverse words

#include <fstream>


using namespace std;
struct nodeType
{
    string info;
    nodeType *link;
    
};

void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first, nodeType*& last);


int main()
{
    
    nodeType *first, *last;
    string words;
    
    
    ifstream inData;
    ofstream outData;

    
    createList(first,last);
    printList(first,last);
    
    
  system("PAUSE");
   return 0;
}

void createList(nodeType*& first, nodeType*& last)
{
     
    ifstream inData("input.txt");
     
    string words;
    int emptyList;
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
    
    while(inData >> words)
    {
         newNode = new nodeType; // create new node
         newNode->info = words;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
    }   
    
    
}

void printList(nodeType*& first,nodeType*& last)
{
        
        ofstream outData("output.txt");
        
        nodeType *current = first;
        nodeType *reverse = NULL;
        
          first = current->link;
          current->link = reverse;
          reverse = current;
          current = first;
          first = reverse;
            
        while (current != NULL)
        {

          outData << current->info <<endl;
          current = current->link;
        }
}
NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

did the assignment change help, did you try that?
since last->link is of type pointer of newNode shouldn't the &newNode be assigned?

sandeepss6s
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

No, I received an error when I added the "&" symbol.

Do you know what I am doing wrong in my reverse order code?

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

on debug mode does your code prior to printlist function call work as per your requirement (as in are the outputs coming fine)?

sandeepss6s
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

My program runs and the print out is fine. It just doesn't print in reverse order. it is printing everything except the first node which is suppose to be "hi"

hey
hello
bye
adios

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

Is there some more code to this because I am unable to compile this?

sandeepss6s
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This is the code and I have an input file called input.txt that contains the contents below.

Input File:

hi
hey
hello
bye
adios

#include <fstream>


using namespace std;
struct nodeType
{
    string info;
    nodeType *link;
    
};

void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first, nodeType*& last);


int main()
{
    
    nodeType *first, *last;
    string words;
    
    
    ifstream inData;
    ofstream outData;

    
    createList(first,last);
    printList(first,last);
    
    
  system("PAUSE");
   return 0;
}

void createList(nodeType*& first, nodeType*& last)
{
     
    ifstream inData("input.txt");
     
    string words;
    int emptyList;
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
    
    while(inData >> words)
    {
         newNode = new nodeType; // create new node
         newNode->info = words;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
    }   
    
    
}

void printList(nodeType*& first,nodeType*& last)
{
        
        ofstream outData("output.txt");
        
        nodeType *current = first;
        nodeType *reverse = NULL;
        nodeType *link;
        
          first = current;       
          last = current->link;
          current->link = reverse;
          reverse = current;
          current = last;
      
        
            
        while (current != NULL)
        {

          outData << current->info <<endl;
          current = current->link;
        }
}
NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

Are you able to reverse words and sort an alphabetical order using Linked List? I'm starting to think it doesn't exist =/

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

sandeep, were you able to find something out?

Anyone know how to reverse words in a Linked List? =/

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

I am currently having problems and need help with:

1) Printing the words in reverse order in my printList function

2) Printing the words in alphabetical order in my printList function

You need a sort function to put something in alphabetical order. printList should do that: print the list. It should have nothing to do with sorting/ordering the list. Do you have a sort function? Any sort that can be implemented on an array can be implemented on a linked list. The O values (i.e. O(n squared)) will often be different than they are for an array based sort due to lack of random access. But you still have to sort if you want to alphabetize. I see no sort function in your code.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

What about reverse? Do I need a seperate function for that too?

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

nevermind, I got reverse to work finally work. I am now working on the alphabet function. Are there any advice to set it up or any principles to follow to sort words in alphabetical order?

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 
nevermind, I got reverse to work finally work. I am now working on the alphabet function. Are there any advice to set it up or any principles to follow to sort words in alphabetical order?

As mentioned, you need a sort function. Bubble Sort works well with a linked list since you're only comparing neighbors. It's just like doing it with an array. Compare neighbors. Swap if they're in the wrong order. When you've gone through the whole list with no swaps, you're done.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

I gave a swing at it, but I did some research first because I am unfamiliar with sorting using Linked Lists. Under my alphasort function, I have created a block of code that tries to use "Bubble sort" to sort the words in my input file alphabetically. However, I am receiving an error "struct nodeType has no member named 'words'".

Example of Input file:

Jack
Apple
Corn
Tea
Jacob

Example of Output

Apple
Corn
Jack
Jacob
Tea

#include <fstream>


using namespace std;
struct nodeType
{
    string info;
    nodeType *link;
};

void createList(nodeType*& first, nodeType*& last);
void alphasort (nodeType*& first, nodeType*& last);
void printList(nodeType*& first);


int main()
{
    
    nodeType *first, *last;
    string words;
    
    
    ifstream inData;
    ofstream outData;

    
    createList(first,last);
    alphasort (first,last);
    printList(first);
    
    
  system("PAUSE");
   return 0;
}

void createList(nodeType*& first, nodeType*& last)
{
     
    ifstream inData("input.txt");
     
    string words;
    int emptyList;
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
    
    while(inData >> words)
    {
         newNode = new nodeType; // create new node
         newNode->info = words;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
    }   
}


void alphasort (nodeType*& first, nodeType*& last)
{
     
     
      int i;
      int j;
      int n;
      int hold; 
      string words;
      nodeType *q, *p, *t;  
    
        for  (nodeType *q = first ; q ; q=q->link)  
     
        ++n;   
        
        for   (i=1 , t = first  ; i<=n-1 ;  t = t->link , ++i)
           
        for   ( j=0 , p = first  ; j<n-i ;  p = p->link , ++j)      
          
          if  (p->words > (p->link)->words)       
          
            {	
            hold = p->words;         
            p->words = (p->link)->words;         
            (p->link)->words = hold;       
            }
          

}




void printList(nodeType*& first)
{
        
         ofstream outData("output.txt");
        
        nodeType *current;
        current = new nodeType;
        current = first;    
        while (current != NULL)
        {
          outData << current->info <<endl;
          current = current->link;
        }
}
NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

Anyone available to help me figure out what I am doing wrong in my code? I am trying to print my words in alphabetical order. The alphasort function is where I tried to do bubblesort.

NinjaLink
Posting Pro in Training
419 posts since Mar 2008
Reputation Points: 8
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You