티스토리 뷰

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;
}

'c++ > c++ 자료구조' 카테고리의 다른 글

환형 링크드 리스트  (0) 2021.02.23
자료구조 배열 사용  (0) 2021.02.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함