我如何打印元素链接列表C ++ [英] How I Do Print Element Linked List C++

查看:96
本文介绍了我如何打印元素链接列表C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我怎么用c ++打印元素链表



我想在.h和cout中设置功能in .cpp



请编辑我的代码



.cpp



  #include   <   cstdlib  >  
#include < iostream >
#include < string >
#include < span class =code -string> SLinkedList.h
#include book.h
使用 命名空间标准;

int main( int argc, char * argv [])
{
int n;
n = atoi(argv [ 1 ]);
SLinkedList< int>一个];
int sizenum;
int num;
for int i = 0 ; i< n; i ++)
{
cout<< 输入大小: ;
cin>> sizenum;
for int j = 0 ; j< sizenum; j ++)
{
cout<< 输入数字: ;
cin>> num;
a [i] .addFront(num);
}
}


for int i = 0 ; i< n; i ++)
{
for int j = 0 ; j< sizenum; j ++)
{
COUT<< A [1] .PRINT()<< ENDL;
}
}


system( PAUSE );
return EXIT_SUCCESS;
}





.h



 模板< typename E>  class  SLinkedList;  //  稍后将声明SLinkedList。 
模板< typename E>
class SNode { // 单链表节点
私人
E elem;
// 链表元素值
SNode< E> * next;
朋友 SLinkedList< E> ;; // 提供SLinkedList访问
};

模板< typename E>
class SLinkedList { // 单链接list
public
SLinkedList(); // 空列表构造函数
~SLinkedList(); // 析构函数
bool empty() const ;
int size() const ; // 列表为空?
const E& front() const ; // 返回前端元素
void addFront( const E& e); // 添加到列表前面
void removeFront(); // 删除前项目列表
int print();
private
int n;
SNode< E> * head;
// 列表的头部
};


模板< typename E>
SLinkedList< E> :: SLinkedList() // 构造函数
:head (NULL),n( 0 ){}

template < typename e基
bool SLinkedList< E> :: empty() const // 列表为空?
{返回 head ==空值; }

模板< typename E>
int SLinkedList< E> :: size() const // 列表为空?
{ return n; }

模板< typename E>
const E& SLinkedList< E> :: front() const // 返回前面元素
{返回 head-> elem; }

模板< typename E>
SLinkedList< E> ::〜SLinkedList() // 析构函数
{ while (!empty())removeFront(); }

模板< typename E>
void SLinkedList< E> :: addFront( const E& e){ // 添加到列表前面
SNode< E> * v = SNode< E> ;; // 创建新节点
v-> elem = e; // 商店数据
v-> next = head; // 现在跟随v
head = v;
n ++; // v现在是头
}


模板< typename E>
int SLinkedList< E> :: print(){
SNode< E> * ptr = head;

return ptr-> elem;


}





谢谢

解决方案

来自Microsoft的示例代码应该为您提供作业提示。 ; - )


我想这样的事情:

 模板< typename E> 
void SLinkedList< E> :: print(){
SNode< E> * ptr = head;
cout<< {;
while (ptr)
{
cout<< ptr-> ELEM;
cout<< (ptr-> next? });
ptr = ptr-> next;
}
cout<< ENDL;
}


首先,如果你想要一个类的打印功能,最好的方法是将输出流对象传递给该函数,如这个:

 模板< typename E> 
class SLinkedList {
...
void print(std :: ostream& os) const ;
};



这有一个额外的好处,你可以选择打印到任何类型的输出流,包括文件流。请注意const限定符:它表示此函数不会修改列表对象。我将把实现留给你,你真的只需要遍历列表并推送流上的每个值(用空格或逗号分隔,无论你喜欢什么)。



其次,你的函数只返回头部 - 显然你被困在关于如何在一个返回问题中返回整个列表的值的问题上;简短的回答是:你不能(反正不容易)!但是,上面的建议解决了这个问题,因为没有必要返回任何内容 - 而只需将整个列表推送到流对象上。



三,一旦你已经实现了这个功能,你可以像这样为流操作符添加一个覆盖:

  template < typename E> 
std :: ostream& operator<<(std :: ostream& os, const SLinkedList< E> mylist){
mylist.print(os);
return os;
}



使用此运算符,您现在可以通过将对象本身传递到输出流来打印对象:

< pre lang =c ++> SLinkedList< int>我的列表;
...
std :: cout<< 我的列表包含:{<< mylist<< }<<的std :: ENDL;< / INT>


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

#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

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

解决方案

This example code from Microsoft should give you a hint for your homework. ;-)


Something like this, I suppose:

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;
 }


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:

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:

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:

SLinkedList<int> mylist;
...
std::cout << "My list contains: {" << mylist << "}" << std::endl;</int>


这篇关于我如何打印元素链接列表C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆