c++/c++ 자료구조
이중 링크드 리스트
삼전동해커
2021. 2. 23. 21:33
#include<iostream>
#include<string>
using namespace std;
class StringNode{
string elem;
StringNode * next;
friend class SLinkedList; //뒤에서 구현할 SLinkedList : 생성자,소멸자 삽입삭제 기능을 제공하는 클래스
};
class SLinkedList{
StringNode * head;
public:
SLinkedList();
~SLinkedList();
bool empty() const;
const string& front() const;
void addFront(const string& e);
void removeFront();
};
//head를 NULL로 초기화
SLinkedList::SLinkedList():head(NULL){} //초기화 리스트는 const에 대해서만 사용하는거 아닌가?
//head가 NULL인지 확인
bool SLinkedList::empty() const{
if(head == NULL)
return 0;
else return 1;
}
//앞에서부터 삭제
SLinkedList::~SLinkedList(){
while(!empty())
removeFront();
}
//값 리턴
const string& SLinkedList::front() const{
return head->elem;
}
void SLinkedList::addFront(const string& e){
StringNode *NewNode = new StringNode;
NewNode->elem = e; //원래는 N
NewNode->next = head;
head = NewNode;
}
void SLinkedList::removeFront(){
StringNode *ret = head;
head = head->next;
delete ret;
}
int main(){
return 0;
}