Please correct my c++ Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • king imran
    New Member
    • Jun 2007
    • 6

    #1

    Please correct my c++ Code

    [CODE=cpp]////////////////////////////// LinkList.cpp


    #include "LinkList.h "


    /* The LinkList class implementation*/

    /* Constructor */
    LinkList::LinkL ist()
    {
    headNode = new Node();
    headNode->setNext(NULL );
    currentNode = NULL;
    lastCurrentNode = NULL;
    size = 0;
    }

    /* add() class method */
    void LinkList::add (int addObject)
    {
    Node * newNode = new Node();
    newNode->set(addObject) ;
    if( currentNode != NULL )
    {
    newNode->setNext(curren tNode->getNext());
    currentNode->setNext( newNode );
    lastCurrentNode = currentNode;
    currentNode = newNode;
    }
    else
    {
    newNode->setNext(NULL );
    headNode->setNext(newNod e);
    lastCurrentNode = headNode;
    currentNode = newNode;
    }
    size ++;
    }

    /* get() class method */
    int LinkList::get()
    {
    if (currentNode != NULL)
    return currentNode->get();
    }

    /* next() class method */
    bool LinkList::next( )
    {
    if (currentNode == NULL) return false;

    lastCurrentNode = currentNode;
    currentNode = currentNode->getNext();
    if (currentNode == NULL || size == 0)
    return false;
    else
    return true;
    }

    /* Friend function to traverse linked list */
    void traverse(LinkLi st list)
    {
    Node* savedCurrentNod e = list.currentNod e;
    list.currentNod e = list.headNode;

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

    list.currentNod e = savedCurrentNod e;
    }

    /* Friend function to add Nodes into the list */
    LinkList addNodes(){

    LinkList list;
    int count = 0;
    cout << "\n Enter the length of the LinkList you want to create: ";
    cin>>count;
    int temp;
    for(int i = 0; i < count ; i++)
    {
    cout <<"\n Enter the Element No # "<<i+1<<": ";

    cin>>temp;

    list.add(temp);

    }
    cout << "\n List size is = " << list.size <<'\n';

    return list;
    return list.size;

    //////////********** MEAN() ***********/////////
    }
    void LinkList ::mean()
    {
    if (headNode == Null)
    cout<<"\n Undefined Value";
    else
    {
    Node *temp = headNode;
    int mean = temp->get();
    int total;
    //show content of node
    while(temp !=Null)
    {
    if (mx<temp->get() )
    total = total + temp->get;
    //move into next node
    temp = temp->getNext();
    }
    mean=total/list.size;
    cout<<"\n Mean of List is "<<mean;
    }
    }

    //////////********** MAX () ***********/////////

    void LinkList ::max()
    {
    if(headNode == Null )
    cout<< "\n Undefined Value";
    else
    {
    //declaring the initialize temp
    Node *temp = headNode;
    int mx = temp->get();
    //show content of node
    while(temp !=Null)
    {
    if (mx<temp->get() )
    mx = temp -> get () ;
    //move into next node
    temp = temp->getNext();
    }
    cout<<"/n The Maximum Element in List is"<< mx;
    }
    cout<<"/n"
    }


    }


    ///////////////////// LinkList.h



    #include "Node.h"

    /* The LinkList class declaration*/
    class LinkList
    {
    public:
    LinkList();
    void add (int addObject);
    int get();
    bool next();

    friend void traverse(LinkLi st list);
    friend LinkList addNodes();
    friend void mean(LinkList list);
    friend void max(LinkList list);

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

    };



    /////////////////////// main.cpp




    #include "LinkList.h "

    int main()
    {
    LinkList list = addNodes();
    traverse(list);
    mean(LinkList list)
    max(LinkList list)


    system("pause") ;
    return 0;
    }



    ////////////////////// Node.cpp

    #include "Node.h"

    /* The Node class implementation*/

    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.h

    #include <iostream>
    #include <stdlib.h>
    using namespace std;

    /* The Node class declaration */

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

    private:
    int object;
    Node * nextNode;
    };[/CODE]
    Last edited by Ganon11; Oct 21 '07, 01:39 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Nice code. If you would be so kind as to read our Posting Guidelines, especially the section entitled How to Ask a Question, and come back here, we could help you out.

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      king imran-

      Dude, you have posted four or five times, and nobody has just done your homework for you. This is an official warning. You post again without trying (and don't answer people who ask you what you have done), you will be banned.

      Comment

      Working...