0
새 연결 목록에 0부터 시작하는 10 개의 연속 값을 채울 수있는 생성자를 만들어야합니다. 그런 다음 목록을 인쇄해야합니다! 그래서 내가 그 기능에 대한 썼다 확인하는 경우 확인하고 싶습니다. 감사!!생성자가있는 단일 링크 목록의 값을 초기화하는 방법
#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;
}
사전에 도움이나 제안을 보내 주셔서 감사합니다. 내가 확신하지 못하는 것은이 두 가지 기능뿐입니다.
나는 그것이 '표준 : initializer_list'을 고려 코드 검토 SE –
로 이동해야하기 때문에 오프 주제로이 질문을 닫으 투표 해요 형식적인 인수 유형으로? –
@VittorioRomeo C++에 대한 특정 사이트를 찾으려고했으나 확인하지 못했습니다. –