Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
how i do print element linked list in c++

I want set function in .h and cout in .cpp

please edit my code

.cpp

C++
#include <cstdlib>
#include <iostream>
#include <string>
#include "SLinkedList.h"
#include "book.h"
using namespace std;

int main(int argc, char *argv[])
{  
        int n;
        n = atoi(argv[1]);
        SLinkedList<int> a[n]; 
        int sizenum;
        int num;
        for(int i=0;i<n;i++)
        {
                cout<<"Enter size :";
                cin>>sizenum;
           for(int j=0;j<sizenum;j++)
             {
                   cout<<"Enter number:";
                   cin>>num;
                   a[i].addFront(num);
             }      
        }
        

        for(int i=0;i<n;i++)
        {
           for(int j=0;j<sizenum;j++)
             {
                   cout<<a[i].print()<<endl;
             }      
        }


        system("PAUSE");
        return EXIT_SUCCESS;
}


.h

C++
template <typename E>   class SLinkedList;  // SLinkedList  will be declared  later.
template <typename E>
  class SNode {					// singly linked list node
  private:
    E elem;
   			// linked list element value
    SNode<E>* next;	
    friend class SLinkedList <E>;	// provide SLinkedList access		
  };

template <typename E>
  class SLinkedList {				// a singly linked list
  public:
    SLinkedList();				// empty list constructor
    ~SLinkedList();				// destructor
    bool empty() const;	
    int size() const;			// is list empty?
    const E& front() const;			// return front element
    void addFront(const E& e);			// add to front of list
    void removeFront();				// remove front item list
    int print();	
  private:
    int n;
    SNode<E>* head;
   			// head of the list
  };


template <typename E>
  SLinkedList<E>::SLinkedList()				// constructor
    : head(NULL),n(0) { }

  template <typename E>
  bool SLinkedList<E>::empty() const			// is list empty?
    { return head == NULL; }

template <typename E>
  int SLinkedList<E>::size() const			// is list empty?
    { return n; }
    
  template <typename E>
  const E& SLinkedList<E>::front() const		// return front element
    { return head->elem; }

  template <typename E>
  SLinkedList<E>::~SLinkedList()			// destructor
    { while (!empty()) removeFront(); }

template <typename E>
  void SLinkedList<E>::addFront(const E& e) {		// add to front of list
    SNode<E>* v = new SNode<E>;				// create new node
    v->elem = e;					// store data
    v->next = head;					// head now follows v
    head = v;
    n++;						// v is now the head
  }

  
  template <typename E>
  int SLinkedList<E>::print() {		
    SNode<E>* ptr = head;			
    
    return ptr->elem ;

				
  }


thank you
Posted
Updated 27-Nov-14 5:34am
v2

This example code from Microsoft should give you a hint for your homework. ;-)
 
Share this answer
 
Something like this, I suppose:
C++
template <typename E>
 void SLinkedList<E>::print() {
   SNode<E>* ptr = head;
   cout << "{";
   while (ptr)
   {
     cout << ptr->elem;
     cout << (ptr->next ? ", " : "}");
     ptr = ptr->next;
   }
   cout << endl;
 }
 
Share this answer
 
v2
First, if you want a printing function for a class, the best way is to pass an output stream object to that function, like this:
C++
template <typename E>
class SLinkedList {
   ...
   void print ( std::ostream& os ) const;
};

This has the added advantage that you can choose to print to any kind of output stream, including file streams. Note the const qualifier: it indicates that this function does not modify the list object. I'll leave the implementation to you, you really only need to traverse the list and push each value on the stream (separated by blanks or commas, whatever you like).

Second, your function only returns the head - obviously you were stuck at the question on how to return the values of an entire list in a single return question; the short answer is: you can't (not easily anyway)! However, the suggestion above solves that problem, because there is no need to return anything - instead you just push the entire list on the stream object.

Third, once you have imlemented that function, you can add an override for the stream operator like this:
C++
template <typename E>
std::ostream& operator<<( std::ostream& os, const SLinkedList<E> mylist) {
   mylist.print(os);
   return os;
}

With this operator, you can now print your objects by simply passing the objects themselves to the output stream:
C++
SLinkedList<int> mylist;
...
std::cout << "My list contains: {" << mylist << "}" << std::endl;</int>
 
Share this answer
 
I did not arrive to a solution yet
 
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