如何初始化与构造函数的单链接列表的值
How to initialize values of Singly Linked List with constructor
我必须创建一个构建器,该构造函数允许一个连续的值填充一个新的链接列表,从0开始。然后,我需要打印列表!因此,我想检查我为这些功能写的功能是否还可以。谢谢!!
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
template <typename E> class SLinkedList; // forward declaration to be used when declaring SNode
template <typename E>
class SNode {
private:
E elem;
SNode<E> *next;
friend class SLinkedList<E>;
};
template <typename E>
class SLinkedList {
public:
SLinkedList();
SLinkedList(SNode<E>* v); //What I need help with
~SLinkedList();
bool empty() const;
E& front();
void printList(SLinkedList<E> &list); //what i need help with
void addFront(const E& e);
void removeFront();
int size() const;
private:
SNode<E>* head;
int n; // number of items
};
template <typename E>
SLinkedList<E>::SLinkedList() // constructor
: head(NULL), n(0) { }
template <typename E>
SLinkedList<E>::SLinkedList(SNode<E>* v){ //WHat I Need Help With
SNode<E>* v = new SNode<E>;
for (int i = 0; i < 10; i++)
v->elem = i;
}
template <typename E>
bool SLinkedList<E>::empty() const
{
return head == NULL; // can also use return (n == 0);
}
template <typename E>
E& SLinkedList<E>::front()
{
if (empty()) throw length_error("empty list");
return head->elem;
}
template <typename E>
SLinkedList<E>::~SLinkedList()
{
while (!empty()) removeFront();
}
template<typename E>
void SLinkedList<E>::printList(SLinkedList<E> &list) //What I need help with
{
for (int i = 0; i < list.size(); i++)
{
cout << list << " ";
}
cout << endl;
}
template <typename E>
void SLinkedList<E>::addFront(const E& e) {
SNode<E>* v = new SNode<E>; // create new node
v->elem = e; // store data
v->next = head; // head now follows v
head = v; // v is now the head
n++;
}
template <typename E>
void SLinkedList<E>::removeFront() {
if (empty()) throw length_error("empty list");
SNode<E>* old = head;
head = old->next;
delete old;
n--;
}
template <typename E>
int SLinkedList<E>::size() const {
return n;
}
事先感谢您的任何帮助或建议!我不确定只是这两个功能。
尝试更多类似的东西:
#include <iostream>
#include <string>
#include <stdexcept>
#include <initializer_list> // C++11 and later only
template <typename E>
class SLinkedList {
public:
SLinkedList();
SLinkedList(const E *vals, int num_vals);
SLinkedList(std::initializer_list<E> vals); // C++11 and later only
~SLinkedList();
bool empty() const;
int size() const;
E& front();
void addFront(const E &e);
void removeFront();
void printList() const;
private:
class SNode
{
public:
E elem;
SNode *next;
SNode(const E &e, SNode *n = NULL);
};
SNode* head;
int n; // number of items
};
template <typename E>
SLinkedList<E>::SNode::SNode(const E &e, SLinkedList<E>::SNode *n)
: elem(e), next(n) { }
template <typename E>
SLinkedList<E>::SLinkedList()
: head(NULL), n(0) { }
template <typename E>
SLinkedList<E>::SLinkedList(const E *vals, int num_vals)
: head(NULL), n(0)
{
for (int i = num_vals-1; i >= 0; --i)
addFront(vals[i]);
/* alternatively:
SNode **ptr = &head;
for (int i = 0; i < num_vals; ++i)
{
*ptr = new SNode(vals[i]);
++n;
ptr = &((*ptr)->next);
}
*/
}
template <typename E>
SLinkedList<E>::SLinkedList(std::initializer_list<E> vals)
: head(NULL), n(0)
{
const E *begin = vals.begin(), *iter = vals.end();
while (iter != begin)
addFront(*(--iter));
/* alternatively:
const E *iter = vals.begin(), *end = vals.end();
SNode **ptr = &head;
while (iter != end)
{
*ptr = new SNode(*iter);
++n;
ptr = &((*ptr)->next);
}
*/
}
template <typename E>
SLinkedList<E>::~SLinkedList()
{
while (head)
removeFront();
}
template <typename E>
bool SLinkedList<E>::empty() const
{
return (!head);
}
template <typename E>
int SLinkedList<E>::size() const
{
return n;
}
template <typename E>
E& SLinkedList<E>::front()
{
if (!head) throw std::length_error("empty list");
return head->elem;
}
template<typename E>
void SLinkedList<E>::printList() const
{
SNode *p = head;
while (p)
{
std::cout << p->elem << " ";
p = p->next;
}
std::cout << std::endl;
}
template <typename E>
void SLinkedList<E>::addFront(const E& e)
{
head = new SNode(e, head);
++n;
}
template <typename E>
void SLinkedList<E>::removeFront()
{
SNode* old = head;
if (!old) throw std::length_error("empty list");
head = old->next;
--n;
delete old;
}
实时演示
相关文章:
- 从链接列表c++中删除一个项目
- 读取文件的最后一行并输入到链接列表时出错
- 下面是我为检测链接列表中的循环而制作的代码
- 有人能帮我处理这个链接列表吗?C++
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 链接列表运算符重载没有打印出我想要的内容
- 链接列表在 cpp 中包含不同的对象类
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 如何在构建链接列表时调整头、尾指针
- Shared_ptr双链接列表内存泄漏
- 为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?
- 在链接列表中查找元素 - C++
- 删除链接列表中剩余的最后一个节点
- 只有我的"链接列表"中的第一个节点正在打印
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 如何在C / C++中正确实现链接列表而不会使程序崩溃
- 将元素插入链接列表