Cramster.com - Homework Solutions, Lecture Notes, Exams, and Free Online Homework Help
Sign Up Now! Login Customer Support Cramster Blog
McAfee Secure sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams
Problem Solved.
    Home    
    Homework Help    
   Answer Board   
    Resources (Beta)    
   
Member's Topic Headline:

link list c++...declaring reverse function

Know the answer? Have a better solution? Share it.
Get Help Now.
View homework problems
explained for free!
Member Testimonials

Question:

Advertisement:

Answer | Ask New Question | Customize Profile | Leaderboards | 
FAQ

Member's Avatar

Guru
Karma Points: 2,026
Respect (97%):
Date Posted: 7/23/2008 1:41:33 AM  Status: Closed
link list c++...declaring reverse function
Course Textbook Chapter Problem
N/A N/A N/A N/A
Question Details:
I have my code just need to add the reverse function to it.

Declare the reverse function as follow:
void reverse();

The function reverses the order of the nodes held by the linked list. This function can be implemented in several ways. The easiest way is to create a new linked list with the values from the original linked list (in reverse order). Then you could substitute the new list for the old list with a statement like
head = newList;
It is more challenging to reverse the list "in place" without creating any new nodes.
Change main.cpp to reverse the list and the display the reversed list.


Main.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "part1.h"

using namespace std;

int main()
{
    char name;
    ifstream infile;
    infile.open("demoTest.txt");
    part1 list;
    
if(!infile)
    cout << "Can not open file" << endl;
else
{
    infile >> name;

    while(infile)
    {
        list.insertNode(name);
        infile >> name;
    }

    cout << "The list now contains:" << endl;
    list.displayList();
    list.~part1();
}
    return 0;
}

Part1.cpp#include "part1.h"
#include <iostream>
#include <fstream>
using namespace std;

void part1::appendList(char num)
{
    listNode *newNode;
    listNode *nodePtr;

    newNode = new listNode;
    newNode->letter = num;
    newNode->next = NULL;

    if(!head)
        head = newNode;
    else
    {
        nodePtr = head;

        while(nodePtr->next)
            nodePtr = nodePtr->next;

        nodePtr->next = newNode;
    }
}


void part1::displayList() const
{
    listNode *nodePtr; // move through the list

    nodePtr = head; //position nodePtr at head of the list

    while(nodePtr)
    {
        cout << nodePtr->letter << endl;//display value

        nodePtr = nodePtr->next;//move to next node
    }
}

void part1::insertNode(char num)
{
    // get a new node to insert
    listNode *newNode;
    listNode *nodePtr; // to traverse the list
    listNode *previousNode = NULL;  // the previous node
    // allocate a new node
    newNode= new listNode;
    newNode->letter=num;

    if(!head)
    {
        head = newNode;
        newNode->next=NULL;
    }
    else
    {
        nodePtr = head;
        previousNode= NULL;
   
      while ( nodePtr!=NULL && toupper(nodePtr->letter) <= toupper (num))
      {
         if (nodePtr->letter >num&&toupper (nodePtr->letter) == toupper (num))   //current is the last in list
         {
               break;
         }
              previousNode  = nodePtr;
              nodePtr=nodePtr->next;
            
      }
        if(previousNode ==NULL)
        {
            head=newNode;
            newNode->next=nodePtr;
        }
        else
        {
            previousNode->next=newNode;
            newNode->next=nodePtr;
        }
         
      }

   }


part1::~part1()
{
    cout << "\nDeleting the list\n" << endl;
    do
    {
        listNode *nodePtr=head;
        cout << "Delete element " << nodePtr->letter << endl;
        head=head->next; //move to next
        delete nodePtr; //delete the node
    }while(head!=NULL);

    cout << "\nDeletion complete" << endl;
} // end

Part1.h
#ifndef PART1_H
#define PART1_H
#include <iostream>
using namespace std;

class part1
{
public:
    part1()
    {
        head = NULL;
    }
    void appendList(char);
    void insertNode(char);
    void displayList() const;
    ~part1();

private:
    struct listNode
    {
        char letter;
        struct listNode *next;
    };
    listNode *head;

};
#endif
Bonus Point Alert! Earn +4 additional karma points for helping this annual member.

I take the time to answer your question. Please take the time to rate it.

Answers:

Cramster Expert

Member's Avatar

(Cramster SME)
Moderator
Cramster In-House Subject Matter Expert
Date Posted: 7/23/2008 4:07:17 AM  Status: Live
Asker's Rating: Lifesaver   
Response:
 
Dear User
 
You need the add the following line of code to the definition of the listNode class in the header file.
       void reverse();
Then add the following code in the part1.cpp at your convenient place.
 
 
 
 
 
buzz1's Comment:
thanks



By reading or posting messages on these forums, you are agreeing to the Answer Board's Terms of Service and Conduct (TSC).


About Cramster | Terms of Use | Privacy Policy | Contact Us | Press Room | Site Map | Support | Anti-Cheating Policy

Cramster.com is not affiliated with any publisher. Book covers, title and author names appear for reference only.
Copyright © 2008 Cramster, Inc.