C++ LinkedList C++;实施
我刚刚创建了LinkedList的一个实现(只是为了自学)。我让它运行,但输出结果有点奇怪。。。 代码如下:C++ LinkedList C++;实施,c++,templates,linked-list,C++,Templates,Linked List,我刚刚创建了LinkedList的一个实现(只是为了自学)。我让它运行,但输出结果有点奇怪。。。 代码如下: #include "stdafx.h" #include <iostream> #include <stdio.h> using namespace std; template <class T> class Node{ T datum; Node<T> *_next; public: Node(T datum) { this
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
using namespace std;
template <class T>
class Node{
T datum;
Node<T> *_next;
public:
Node(T datum)
{
this->datum = datum;
_next = NULL;
}
void setNext(Node* next)
{
_next = next;
}
Node* getNext()
{
return _next;
}
T getDatum()
{
return datum;
}
};
template <class T>
class LinkedList{
Node<T> *node;
Node<T> *currPtr;
Node<T> *next_pointer;
int size;
public:
LinkedList(T datum)
{
node = new Node<T>(datum);
currPtr = node; //assignment between two pointers.
next_pointer = node;
size = 1;
}
LinkedList* add(T datum) // return pointer type.
{
Node<T> *temp = new Node<T>(datum);
currPtr->setNext(temp);
currPtr = temp;
size++;
cout<<datum<<" is added.";
return this; //pointer type specification
}
T next()
{
T data = (*next_pointer).getDatum();
cout<<data<<" is visited.";
next_pointer = next_pointer->getNext();
return data;
}
int getSize()
{
return size;
}
};
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
模板
类节点{
T基准;
节点*\u下一步;
公众:
节点(T基准)
{
这个->基准=基准;
_next=NULL;
}
void setNext(节点*下一个)
{
_下一个=下一个;
}
节点*getNext()
{
返回下一步;
}
T getDatum()
{
返回数据;
}
};
模板
类链接列表{
节点*节点;
节点*currPtr;
节点*下一个_指针;
整数大小;
公众:
链接列表(T数据)
{
节点=新节点(基准);
currPtr=node;//两个指针之间的赋值。
下一个指针=节点;
尺寸=1;
}
LinkedList*add(T datum)//返回指针类型。
{
节点*温度=新节点(基准);
currPtr->setNext(温度);
currPtr=温度;
大小++;
cout参数的评估顺序未指定,因此:
printf("%d %d %d %d",list->next(),list->next(),list->next(),list->next());
可以首先计算最后一个列表->下一个()
或中间的一个
编辑:仅仅处理我假设的问题,因为我怀疑这是实际代码:是否有一个固定的C++评估顺序?或者可以自己指定它?谢谢!@ GANO(至少对于C++ 03)。在代码> PROTFF < />代码之前获取值并将其存储在变量中。@高:C++中的评价顺序未指定。
printf("%d %d %d %d",list->next(),list->next(),list->next(),list->next());