我是C++的新手.请帮我链接列表(要添加哪些功能)?

Iga*_*tor 2 c++ linked-list member-functions

亲爱的大家;

嗨,我只是C++的初学者; 请帮我理解:

Linked list类中应该有哪些函数?我认为应该重载运算符<<和>>; 请帮我改进代码(样式,错误等),谢谢你提前.有约色.

编辑:这只是第一阶段,下一个将(希望)与模板.

请查看整数列表的小代码(包含MyNODE.h和ListDriver1.cpp); MyNODE.h

    // This is my first attempt to write linked list. Igal Spector, June 2010.

#include <iostream.h>
#include <assert.h>

//Forward Declaration of the classes:
class ListNode;
class TheLinkedlist;

// Definition of the node (WITH IMPLEMENTATION !!!, without test drive):


class ListNode{
 friend class TheLinkedlist;
public:
 // constructor:
 ListNode(const int& value, ListNode *next= 0);
 // note: no destructor, as this handled by TheLinkedList class.

 // accessor: return data in the node.
// int Show() const {return theData;}

private:
 int theData;  //the Data
 ListNode* theNext; //points to the next node in the list.
};

//Implementations:
//constructor:
inline ListNode::ListNode(const int &value,ListNode *next)
:theData(value),theNext(next){}


//end of ListNode class, now for the LL class:

class TheLinkedlist
{
public:
 //constructors:
 TheLinkedlist();
 virtual ~TheLinkedlist();
 // Accessors:
 void InsertAtFront(const &);
 void AppendAtBack(const &);

// void InOrderInsert(const &);
 bool IsEmpty()const;//predicate function
 void Print() const;
private:
 ListNode * Head; //pointer to first node
 ListNode * Tail; //pointer to last node.
};

//Implementation:

//Default constructor
inline TheLinkedlist::TheLinkedlist():Head(0),Tail(0) {}

//Destructor
inline TheLinkedlist::~TheLinkedlist(){
 if(!IsEmpty()){  //list is not empty
 cout<<"\n\tDestroying Nodes"<<endl;
 ListNode *currentPointer=Head, *tempPtr;

  while(currentPointer != 0){ //Delete remaining Nodes.
   tempPtr=currentPointer;
  cout<<"The node: "<<tempPtr->theData <<" is Destroyed."<<endl<<endl;
  currentPointer=currentPointer->theNext;
  delete tempPtr;
  }
 Head=Tail = 0;  //don't forget this, as it may be checked one day.
 }
}

//Insert the Node to the beginning of the list:
void TheLinkedlist::InsertAtFront(const int& value){
 ListNode *newPtr = new ListNode(value,Head);
 assert(newPtr!=0);

 if(IsEmpty())  //list is empty
  Head = Tail = newPtr;
 else {    //list is NOT empty
  newPtr->theNext = Head;
  Head = newPtr;
 }
}

//Insert the Node to the beginning of the list:
void TheLinkedlist::AppendAtBack(const int& value){
 ListNode *newPtr = new ListNode(value, NULL);
 assert(newPtr!=0);

 if(IsEmpty())  //list is empty
  Head = Tail = newPtr;
 else {    //list is NOT empty
  Tail->theNext = newPtr;
  Tail = newPtr;
 }
}

//is the list empty?
inline bool TheLinkedlist::IsEmpty() const
  { return (Head == 0); }

// Display the contents of the list
void TheLinkedlist::Print()const{
 if ( IsEmpty() ){
  cout << "\n\t The list is empty!!"<<endl;
  return;
 }

 ListNode *tempPTR = Head;
 cout<<"\n\t The List is: ";

 while ( tempPTR != 0 ){
  cout<< tempPTR->theData <<"  ";
  tempPTR = tempPTR->theNext;
 }
 cout<<endl<<endl;
}
//////////////////////////////////////
Run Code Online (Sandbox Code Playgroud)

测试驱动程序:

//Driver test for integer Linked List.

#include <iostream.h>
#include "MyNODE.h"

// main Driver
int main(){

 cout<< "\n\t This is the test for integer LinkedList."<<endl;

 const int arraySize=11,
   ARRAY[arraySize]={44,77,88,99,11,2,22,204,50,58,12};

 cout << "\n\tThe array is: "; //print the numbers.
 for (int i=0;i<arraySize; i++)
  cout<<ARRAY[i]<<",  ";

 TheLinkedlist list;   //declare the list

 for(int index=0;index<arraySize;index++)
  list.AppendAtBack( ARRAY[index] );//create the list

 cout<<endl<<endl;
 list.Print();    //print the list

 return 0;     //end of the program.
}
Run Code Online (Sandbox Code Playgroud)

sta*_*ica 7

Linked list类中应该有哪些函数?

这取决于你需要做什么.至少,应该可以向其中添加元素,并查看列表中的元素.

(这是常识.因为如果你不能以任何方式修改或阅读你的清单,它可以用于什么?)


我认为应该重载运算符<<和>>;

为什么?他们会做什么?我想你的意思operator <<是插入,类似于如何将对象插入到C++ IO流中; 但究竟该怎么operator >>办?提取/删除某种元素?如果以这种方式实现插入和提取(?),可能没有人能够理解您的链表类.链表不是 IO流.(为简洁起见,选择那些具有IO流的运营商.)

如果操作的含义不明确,我建议你不要操作员重载.我建议你更明确地命名你的操作,例如通过提供方法addremove(我仍然猜测后一种操作的含义>>顺便说一句.).


请帮我改进代码(样式,错误等)

我不想把这个问题作为我的答案的主要观点,所以只是简单地说一下我的问题:

  • 你应该#include <iostream>代替#include <iostream.h>,然后添加using namespace std;或写(例如)std::cout而不是cout.

  • 试着摆脱friend.您应该能够以不需要的方式设计类.friend很容易被误用来绕过适当的封装.但封装是你应该在OOP中明确考虑的事情.

  • 虽然这不是给C++初学者的建议,但是如果你将链表类放到模板类中,它可以存储不同于ints的不同值.只需将其作为未来改进的暗示.


最后:

  • 只需使用 C++标准库中包含的STL("标准模板库")容器即可.我知道"滚动自己"有助于理解这些数据结构的工作原理,但要注意C++标准库已经包含了一组可靠而有效的数据容器.