Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this the one of example linked list in full program, but i still cant solve it, hope any people there can help me

this quest wee need to answer on "//" sign only, hope please help me, this like one question it will come out in my exam, this question one of linked list in class classified.

C++
#ifndef H_LinkedListType
#define H_LinkedListType

#include <iostream>
#include <cassert>

using namespace std;

//Definition of the node

template <class type="">
struct nodeType {
    Type info;
    nodeType<type> *link;
};

template <class type="">
class linkedListIterator {
public:
    linkedListIterator();
    //Default constructor
    //Postcondition: current = NULL;

    linkedListIterator(nodeType<type> *ptr);
    //Constructor with a parameter.
    //Postcondition: current = ptr;

    Type operator*();
    //Function to overload the dereferencing operator *.
    //Postcondition: Returns the info contained in the node.

    linkedListIterator<type> operator++();
    //Overload the pre-increment operator.
    //Postcondition: The iterator is advanced to the next
    //               node.

    bool operator==(const linkedListIterator<type>& right) const;
    //Overload the equality operator.
    //Postcondition: Returns true if this iterator is equal to
    //               the iterator specified by right,
    //               otherwise it returns the value false.

    bool operator!=(const linkedListIterator<type>& right) const;
    //Overload the not equal to operator.
    //Postcondition: Returns true if this iterator is not
    //               equal to the iterator specified by
    //               right; otherwise it returns the value
    //               false.

private:
    nodeType<type> *current; //pointer to point to the current
    //node in the linked list
};

template <class type="">
linkedListIterator<type>::linkedListIterator() {
    current = NULL;
}

template <class type="">
linkedListIterator<type>::
linkedListIterator(nodeType<type> *ptr) {
    current = ptr;
}

template <class type="">
Type linkedListIterator<type>::operator*() {
    return current->info;
}

template <class type="">
linkedListIterator<type> linkedListIterator<type>::operator++() {
    current = current->link;

    return *this;
}

template <class type="">
bool linkedListIterator<type>::operator==
(const linkedListIterator<type>& right) const {
    return (current == right.current);
}

template <class type="">
bool linkedListIterator<type>::operator!=
(const linkedListIterator<type>& right) const {
    return (current != right.current);
}


//*****************  class linkedListType   ****************

template <class type="">
class linkedListType {
public:
    const linkedListType<type>& operator=
    (const linkedListType<type>&);
    //Overload the assignment operator.

    void initializeList();
    //Initialize the list to an empty state.
    //Postcondition: first = NULL, last = NULL, count = 0;

    bool isEmptyList() const;
    //Function to determine whether the list is empty.
    //Postcondition: Returns true if the list is empty,
    //               otherwise it returns false.

    void print() const;
    //Function to output the data contained in each node.
    //Postcondition: none

    int length() const;
    //Function to return the number of nodes in the list.
    //Postcondition: The value of count is returned.

    void destroyList();
    //Function to delete all the nodes from the list.
    //Postcondition: first = NULL, last = NULL, count = 0;

    Type front() const;
    //Function to return the first element of the list.
    //Precondition: The list must exist and must not be
    //              empty.
    //Postcondition: If the list is empty, the program
    //               terminates; otherwise, the first
    //               element of the list is returned.

    Type back() const;
    //Function to return the last element of the list.
    //Precondition: The list must exist and must not be
    //              empty.
    //Postcondition: If the list is empty, the program
    //               terminates; otherwise, the last
    //               element of the list is returned.

    virtual bool search(const Type& searchItem) const = 0;
    //Function to determine whether searchItem is in the list.
    //Postcondition: Returns true if searchItem is in the
    //               list, otherwise the value false is
    //               returned.

    virtual void insertFirst(const Type& newItem) = 0;
    //Function to insert newItem at the beginning of the list.
    //Postcondition: first points to the new list, newItem is
    //               inserted at the beginning of the list,
    //               last points to the last node in the list,
    //               and count is incremented by 1.

    virtual void insertLast(const Type& newItem) = 0;
    //Function to insert newItem at the end of the list.
    //Postcondition: first points to the new list, newItem
    //               is inserted at the end of the list,
    //               last points to the last node in the list,
    //               and count is incremented by 1.

    virtual void deleteNode(const Type& deleteItem) = 0;
    //Function to delete deleteItem from the list.
    //Postcondition: If found, the node containing
    //               deleteItem is deleted from the list.
    //               first points to the first node, last
    //               points to the last node of the updated
    //               list, and count is decremented by 1.

    linkedListIterator<type> begin();
    //Function to return an iterator at the begining of the
    //linked list.
    //Postcondition: Returns an iterator such that current is
    //               set to first.

    linkedListIterator<type> end();
    //Function to return an iterator one element past the
    //last element of the linked list.
    //Postcondition: Returns an iterator such that current is
    //               set to NULL.

    linkedListType();
    //default constructor
    //Initializes the list to an empty state.
    //Postcondition: first = NULL, last = NULL, count = 0;

    linkedListType(const linkedListType<type>& otherList);
    //copy constructor

    ~linkedListType();
    //destructor
    //Deletes all the nodes from the list.
    //Postcondition: The list object is destroyed.

    void reversePrint() const;

protected:
    int count;   //variable to store the number of
    //elements in the list
    nodeType<type> *first; //pointer to the first node of the list
    nodeType<type> *last;  //pointer to the last node of the list

private:
    void copyList(const linkedListType<type>& otherList);
    //Function to make a copy of otherList.
    //Postcondition: A copy of otherList is created and
    //               assigned to this list.
    void recursiveReversePrint(nodeType<type> *current) const;
};


template <class type="">
bool linkedListType<type>::isEmptyList() const {
    return(first == NULL);
}

template <class type="">
linkedListType<type>::linkedListType() { //default constructor
    first = NULL;
    last = NULL;
    count = 0;
}

template <class type="">
void linkedListType<type>::destroyList() {
    nodeType<type> *temp;   //pointer to deallocate the memory
    //occupied by the node
    while (first != NULL) { //while there are nodes in the list
        temp = first;        //set temp to the current node
        first = first->link; //advance first to the next node
        delete temp;   //deallocate the memory occupied by temp
    }
    last = NULL; //initialize last to NULL; first has already
    //been set to NULL by the while loop
    count = 0;
}

template <class type="">
void linkedListType<type>::initializeList() {
    destroyList(); //if the list has any nodes, delete them
}

template <class type="">
void linkedListType<type>::print() const {
    nodeType<type> *current; //pointer to traverse the list

    current = first;    //set current so that it points to
    //the first node
    while (current != NULL) { //while more data to print
        cout << current->info << " ";
        current = current->link;
    }
}//end print

template <class type="">
int linkedListType<type>::length() const {
    return count;
}  //end length

template <class type="">
Type linkedListType<type>::front() const {
    assert(first != NULL);

    return first->info; //return the info of the first node
}//end front

template <class type="">
Type linkedListType<type>::back() const {
    assert(last != NULL);

    return last->info; //return the info of the last node
}//end back

template <class type="">
linkedListIterator<type> linkedListType<type>::begin() {
    linkedListIterator<type> temp(first);

    return temp;
}

template <class type="">
linkedListIterator<type> linkedListType<type>::end() {
    linkedListIterator<type> temp(NULL);

    return temp;
}

template <class type="">
void linkedListType<type>::copyList
(const linkedListType<type>& otherList) {
    nodeType<type> *newNode; //pointer to create a node
    nodeType<type> *current; //pointer to traverse the list

    if (first != NULL) //if the list is nonempty, make it empty
        destroyList();

    if (otherList.first == NULL) { //otherList is empty
        first = NULL;
        last = NULL;
        count = 0;
    } else {
        current = otherList.first; //current points to the
        //list to be copied
        count = otherList.count;

        //copy the first node
        first = new nodeType<type>;  //create the node

        first->info = current->info; //copy the info
        first->link = NULL;        //set the link field of
        //the node to NULL
        last = first;              //make last point to the
        //first node
        current = current->link;     //make current point to
        //the next node

        //copy the remaining list
        while (current != NULL) {
            newNode = new nodeType<type>;  //create a node
            newNode->info = current->info; //copy the info
            newNode->link = NULL;       //set the link of
            //newNode to NULL
            last->link = newNode;  //attach newNode after last
            last = newNode;        //make last point to
            //the actual last node
            current = current->link;   //make current point
            //to the next node
        }//end while
    }//end else
}//end copyList

template <class type="">
linkedListType<type>::~linkedListType() { //destructor
    destroyList();
}//end destructor

template <class type="">
linkedListType<type>::linkedListType
(const linkedListType<type>& otherList) {
    first = NULL;
    copyList(otherList);
}//end copy constructor

//overload the assignment operator
template <class type="">
const linkedListType<type>& linkedListType<type>::operator=
(const linkedListType<type>& otherList) {
    if (this != &otherList) { //avoid self-copy
        copyList(otherList);
    }//end else

    return *this;
}

#endif
Posted
Updated 27-Feb-11 18:20pm
v5
Comments
Sergey Alexandrovich Kryukov 27-Feb-11 22:52pm    
Please go to source code, replace all < > with HTML character entities "&lt;" "&gt;", explain your problem, write in full punctuation, correct spelling without abbreviations.
--SA
Sergey Alexandrovich Kryukov 27-Feb-11 22:58pm    
Please never post non-answer as Answer, use "Improve question" or "Add comment". I moved your "Answer" the Question at the end. Edit Question appropriately.
--SA
Indivara 28-Feb-11 0:23am    
Fixed encoding for you.

Still not sure what you need. You want us to fill all in the parts given in the comments? Don't you think you should at least attempt to answer it yourself before asking for help? (show us what you have done)

Your question has two problems:

1. It is obviously homework. Why not try to solve it for yourself? Who knows, you might learn something.

2. It is unintelligible. I haven't a clue what you're asking; apparently something about "//"???
 
Share this answer
 
Comments
ikvenhonay 28-Feb-11 1:34am    
1. thanks for ur suggestion, n yap this my homework, but i have try hard to find it, for the simple link list i have try n can solve it, but for this question i cant solve it cos it use class spesification, n this question will be come out in my exam, that way i need help.
2. for this sign "//", that mean in this sample question have not full program, so in this case i need fill up n put the full code on "//" that mean on comment for C++ coding.
it ok thanks for ur view all, thanks for ur suggestion, maybe on this site no one can help me, yap this my home work, n i have try to solve it, but i still cant find the clue how to start it, in this home work, the lec give me this question, n i have fill up at "//"(comment), n this question about linked list in data structure, n i dont know from where i need to start it. cos i also dont have the print screen of the output, the way i stuck in this quest n dont know how to start it.
 
Share this answer
 
Comments
Emilio Garavaglia 28-Feb-11 14:55pm    
Please, write plain English, not your local dialect. This is not tweeter: you have the full capability of a complete keyboard.
for this code i have get the out put waht the question want it, this the instruction of that question, Printing a single linked list backward. Include the functions reversePrint and recursiveReverseprint,Also write a program function to print a (single) linked list backward.


This the out put
Enter numbers ending with -999
2 23 4 5 6 7 45 33 2 11 -999

List: 2 23 4 5 6 7 45 33 2 11
list in reverse order:
List: 11 2 33 45 7 6 5 4 23

or any people can help me to find the simple code for linked list in c++ based on that output.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900