GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2007, 03:21
fahad fahad is offline
New Member
 
Join Date: Mar 2007
Posts: 9
fahad has a little shameless behaviour in the past
Question

Please help me to solve C++ problem


Question Details:
Q. in this code we will add two function which is shown after this code. the code is

CPP / C++ / C Code:
/*Node.h file*/

#include <iostream.h>
#include <stdlib.h>

class Node
{
  public:
        int get();
        void set(int);
        Node*getNext();
        void setNext(Node*);
        Node*getPrev();
        void setPrev(Node*);
  
  private:
      int object;
      Node*nextNode;
      Node*prevNode;
};

/* Node.cpp file*/


//#include "Node.h"

/*The Node class implemenatation*/

int Node::get()
 {
 return object;
 }
 
 void Node::set(int object)
 {
 this->object = object;
 }
 
 Node*Node::getNext()
 {
 return nextNode;
 }
 
 void Node::setNext(Node*NextNode)
 {
 this->nextNode = nextNode;
 }
 
 Node*Node::getPrev()
 {
 return prevNode;
 }
 
 void Node::setPrev(Node *prevNode)
 {
 this->prevNode = prevNode;
 }
 
 /*  DoublyLinkList.h */

//#include "Node.h"

/* The LinList class implementation */

class DoublyLinkList
{
    public:
      DoublyLinkList();
      void add( int addObject);
      int get();
      bool next();
      bool prev();
      friend void traverseForward(DoublyLinkList list);
      friend void traverseReverse(DoublyLinkList list);
      friend DoublyLinkList addNodes();
    
    private:
      int size;
      Node * headNode;
      Node* currentNode;
      Node* lastCurrentNode;

   

};

/* DoublyLinkList.cpp file */

//#include "DoublyLinkList.h"

/* The LinkList class implementation */

// constructor

DoublyLinkList::DoublyLinkList()
{
  headNode = new Node();
     headNode->setNext(NULL);
     headNode->setPrev(NULL);
     currentNode = NULL;
     lastCurrentNode = NULL;
     size = 0;

};

//  add() class method

void DoublyLinkList::add(int addObject)
{
  Node*newNode = new Node();
  newNode->set(addObject);
  
  if(currentNode != NULL)  // if list is not emppty
  {
     newNode->setNext(currentNode->getNext());
     if(currentNode->getNext()!=NULL)
          currentNode->getNext()->setPrev(newNode);
          newNode->setPrev(currentNode);
          currentNode->setNext(newNode);
          
          lastCurrentNode = currentNode;
          currentNode = newNode;
     }
     
     else  // if list is empty
     {
          newNode->setNext(NULL);
          newNode->setPrev(headNode);
          headNode->setNext(newNode);
          lastCurrentNode = headNode;
          currentNode = newNode;
     }
     
     size++;
}

/* get() class method */

int DoublyLinkList::get()
{
        if(currentNode != NULL)
        return currentNode->get();
}

// next() class method

bool DoublyLinkList::next()
{
  if(currentNode == NULL) return false;  // no node  in list
  lastCurrentNode = currentNode;
  if(currentNode == NULL || size == 0) // we have reached at the tail no more nodes
  
return false;
  else
      return true;
}

// prev() class method

bool DoublyLinkList::prev()
{
    if(currentNode == NULL) return false;  // no node in list
    currentNode = lastCurrentNode;
    lastCurrentNode = lastCurrentNode->getPrev();
    if(lastCurrentNode == NULL || size == 0)  // we have reached at the head no more nodes
    
  return false;
    else
        return true;
}

/* Friend function to traverse linked list */

void traverseForward(DoublyLinkList list)
{
  Node* savedCurrentNode = list.currentNode;
  list.currentNode = list.headNode;
  
  cout<<"\n Traversing in Forward Direction";
  for(int i=1; list.next(); i++)
  {
    cout<<"\n Element "<<i<<" of the list is "<<list.get()<<endl;
    
  }
  list.currentNode = savedCurrentNode;
}

void traverseReverse(DoublyLinkList list)
{
  Node* savedCurrentNode = list.currentNode;
  cout<<"\nTraversing in Reverse Direction";
  
  do
  {
  cout<<"\n Element "<<list.size<<" of the list is "<<list.get()<<endl;
  list.size = list.size-1;
  }
  while(list.prev());
  list.currentNode = savedCurrentNode;
  }
  
// friend funciton to add nodes

DoublyLinkList addNodes()
{

    DoublyLinkList list;
    int temp;
    for(int i=0; i<9; i++)
    {
      cout<<"\nPlease Enter the Element No # "<<i+1<<":";
      cin>>temp;
      list.add(temp);
    }
    
    cout<<"\n\nList size is = "<<list.size<<"\n";
    
    return list;
}

/* Main file code */

//#include "DoublyLinkList.h"


int main()
{
DoublyLinkList list = addNodes();
traverseForward(list);
traverseReverse(list);


system("pause");
return 0;
} 


WHAT TO DO:
Add a integer part of a rollno in the doubly link list addnodes method for example if someone has a rollno bc020000001,he will add integers
0
2
0
0
0
0
0
0
1

in the same order as in this list

you will add two more friend functions in doubly link list class as described below;

1. first function will get the first four integers and will show the student batch for example in case of roll no. bc020400001 the batch to be displayed will be,
0204

2. second function will add all the entered roll number integers and will check whether the result is even or odd and will show the result.

for example output for this function my be,
The sum is Even
Last edited by admin : 01-Nov-2007 at 11:15. Reason: Please insert your C/C++ example codes between [CPP] and [/CPP] tags
  #2  
Old 01-Nov-2007, 04:43
davis
 
Posts: n/a

Re: Please help me to solve C++ problem


Quote:
Originally Posted by fahad
Question Details:
Q. in this code we will add two function which is shown after this code.

Whew...talk about ugly! ...and not a real question in the entire BUNCH!

Here it is cleaned up enough that it compiles...now you just have to make it actually "work." Oh yeah, good luck...you'll need it! (You may want to consider adding copy constructors, assignment operators, etc, to your classes, particularly if you're going to be passing by value...)

CPP / C++ / C Code:
/* DoublyLinkedList.cpp file */

#include <iostream>

using namespace std;

#include <DoublyLinkedList.h>

/* The LinkList class implementation */

// constructor

DoublyLinkedList::DoublyLinkedList()
{
    headNode = new Node();
    headNode->setNext(NULL);
    headNode->setPrev(NULL);
    currentNode = NULL;
    lastCurrentNode = NULL;
    size = 0;

}

//  add() class method

void DoublyLinkedList::add(int addObject)
{
    Node*newNode = new Node();
    newNode->set(addObject);

    if( currentNode != NULL )  // if list is not emppty
    {
        newNode->setNext(currentNode->getNext());
        if( currentNode->getNext()!=NULL )
            currentNode->getNext()->setPrev(newNode);
        newNode->setPrev(currentNode);
        currentNode->setNext(newNode);

        lastCurrentNode = currentNode;
        currentNode = newNode;
    }

    else  // if list is empty
    {
        newNode->setNext(NULL);
        newNode->setPrev(headNode);
        headNode->setNext(newNode);
        lastCurrentNode = headNode;
        currentNode = newNode;
    }

    size++;
}

/* get() class method */

int DoublyLinkedList::get()
{
    if( currentNode != NULL )
        return currentNode->get();
    else
        return 0;
}

// next() class method

bool DoublyLinkedList::next()
{
    if( currentNode == NULL ) return false;  // no node  in list
    lastCurrentNode = currentNode;
    if( currentNode == NULL || size == 0 ) // we have reached at the tail no more nodes

        return false;
    else
        return true;
}

// prev() class method

bool DoublyLinkedList::prev()
{
    if( currentNode == NULL ) return false;  // no node in list
    currentNode = lastCurrentNode;
    lastCurrentNode = lastCurrentNode->getPrev();
    if( lastCurrentNode == NULL || size == 0 )  // we have reached at the head no more nodes

        return false;
    else
        return true;
}

/* Friend function to traverse linked list */

void traverseForward(DoublyLinkedList list)
{
    Node* savedCurrentNode = list.currentNode;
    list.currentNode = list.headNode;

    cout << "\n Traversing in Forward Direction";
    for( int i = 1; list.next(); i++ )
    {
        cout << "\n Element " << i << " of the list is " << list.get() << endl;

    }
    list.currentNode = savedCurrentNode;
}

void traverseReverse(DoublyLinkedList list)
{
    Node* savedCurrentNode = list.currentNode;
    cout << "\nTraversing in Reverse Direction";

    do
    {
        cout << "\n Element " << list.size << " of the list is " << list.get() << endl;
        list.size = list.size-1;
    }
    while( list.prev() );
    list.currentNode = savedCurrentNode;
}

// friend funciton to add nodes

DoublyLinkedList addNodes()
{

    DoublyLinkedList list;
    int temp;
    for( int i = 0; i < 9; i++ )
    {
        cout << "\nPlease Enter the Element No # " << i + 1 << ":";
        cin >> temp;
        list.add(temp);
    }

    cout << "\n\nList size is = " << list.size << "\n";

    return list;
}

/* Main file code */

#include <iostream>
#include <string>

using namespace std;

#include <DoublyLinkedList.h>

extern DoublyLinkedList addNodes();

int main()
{
    DoublyLinkedList dlist = addNodes();
    traverseForward(dlist);
    traverseReverse(dlist);


    cout << "Enter a key to continue: ";
    char c;
    cin >> c;

    return 0;
}

/* Node.cpp file*/


#include <Node.h>

/*The Node class implemenatation*/

int Node::get()
{
    return object;
}

void Node::set(int object)
{
    this->object = object;
}

Node* Node::getNext()
{
    return nextNode;
}

void Node::setNext(Node* nextNode)
{
    this->nextNode = nextNode;
}

Node* Node::getPrev()
{
    return prevNode;
}

void Node::setPrev(Node* prevNode)
{
    this->prevNode = prevNode;
}


/*  DoublyLinkedList.h */

#ifndef _DoublyLinkedList_h_
#define _DoublyLinkedList_h_

#include <Node.h>

/* The DoublyLinkedList class implementation */

class DoublyLinkedList
{
public:
    DoublyLinkedList();
    void add( int addObject);
    int get();
    bool next();
    bool prev();
    friend void traverseForward(DoublyLinkedList list);
    friend void traverseReverse(DoublyLinkedList list);
    friend DoublyLinkedList addNodes();

private:
    int size;
    Node* headNode;
    Node* currentNode;
    Node* lastCurrentNode;
};

#endif // ! _DoublyLinkedList_h_


/*Node.h file*/

#ifndef _Node_h_
#define _Node_h_


class Node
{
public:
    int get();
    void set(int);
    Node* getNext();
    void setNext(Node*);
    Node* getPrev();
    void setPrev(Node*);

private:
    int object;
    Node* nextNode;
    Node* prevNode;
};


#endif // ! _Node_h_



:davis:
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Description: Graph’s Degree.....can help me to solve this out? thefinalyy C++ Forum 0 24-Aug-2007 05:42
Hitting a logic problem I can't solve Elsydeon C++ Forum 3 09-Oct-2005 21:29
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
Please help to solve me a problem with multiline texrbox and checkbox mithila MS Visual C++ / MFC Forum 0 08-Sep-2004 22:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:00.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.