카테고리 없음

c++로 단일 연결 리스트 구현하기

coding-potato 2025. 2. 13. 18:16

클래스, 템플릿을 이용해서 단일 연결 리스트를 구현해보았습니다.

주석이 많지 않아 친절하지 않습니다.

참고로 SLL은 Single Linked List의 줄임말입니다.

++ 처음 리스트를 만들때와 리스트 뒤에 숫자를 추가할때 하나 입력->엔터->하나 입력-> 엔터... 처럼 입력해야합니다. 한꺼번에 입력 안됩니다... 

#include <iostream>
using namespace std;

template <typename T>
class Node{
public:
    T value;
    Node<T>* next = nullptr;

    Node(T val){
        value = val;
    }

    Node(){}
};

template <typename T>
class SLL{
private:
    Node<T>* begin_node = nullptr;
public:
    SLL(T val){
        begin_node = new Node<T>(val);
    }

    SLL(){}

    void push_back(T val, Node<T>* node = nullptr){
        // 리스트가 완전히 없을 때
        if(begin_node == nullptr){
            begin_node = new Node<T>(val);
            return;
        }
        // 초기 값이 있을때 -> 초기값.next로 재귀
        else if(begin_node != nullptr && node == nullptr){
            push_back(val, begin_node);
            return;
        }
        
        if(node->next != nullptr){
            push_back(val, node->next);
        }
        else{
            node->next = new Node<T>(val);
        }
    }

    int size(){
        if(begin_node == nullptr)
            return 0;
        int cnt = 1;

        Node<T>* iterator = begin_node;
        while(iterator->next != nullptr){
            cnt++;
            iterator = iterator->next;
        }
        
        return cnt;
    }

    void pop_back(){
        if(begin_node == nullptr)
            return;

        if(begin_node->next == nullptr){
            delete begin_node;
            begin_node = nullptr;
            return;
        }

        Node<T>* iterator = begin_node;

        while(iterator->next->next != nullptr){
            iterator = iterator->next;
        }

        delete iterator->next;
        iterator->next = nullptr;
    }

    void print_SLL(){
        if(this->size() == 0){
            cout << "List is empty!" << endl;
            return;
        }

        Node<T>* curr_node = begin_node;
        int listsize = this->size();
        for(int i = 0; i < listsize; i++){
            cout << curr_node->value << " ";
            curr_node = curr_node->next;
        }
        cout << endl;
    }

    // 특정 값을 찾아 index를 반환
    int find(T val){
        if(this->size() == 0){
            return -1;
        }

        Node<T>* curr_node = begin_node;
        int listsize = this->size();
        for(int i = 0; i < listsize; i++){
            if(curr_node->value == val)
                return i;
            curr_node = curr_node->next;
        }
        return -1;
    }

    // 특정 값이 들어있는 노드를 찾아 주소를 반환
    Node<T>* find_ptr(T val){
        if(this->size() == 0){
            return nullptr;
        }

        Node<T>* curr_node = begin_node;
        int listsize = this->size();
        for(int i = 0; i < listsize; i++){
            if(curr_node->value == val)
                return curr_node;
            curr_node = curr_node->next;
        }
        return nullptr;
    }
};

int main() {
    SLL<int> list;

    int size_of_list;
    cout << "enter the size of list : ";
    cin >> size_of_list;

    if(size_of_list <= 0)
    {
        cout << "the size of list must be bigger than 0" << endl;
        return 0;
    }

    int tmp;
    for(int i = 0; i < size_of_list; i++){
        cout << "enter numbers : ";
        cin >> tmp;
        list.push_back(tmp);
    }

    int selected_num = -1;
    while(1){
        cout << "========================================" << endl;
        cout << "select number you want" << endl;
        cout << "0. end this program" << endl;
        cout << "1. add the back of current list" << endl;
        cout << "2. reduce the back of current list" << endl;
        cout << "3. print the infromation of current list" << endl;
        cout << "4. print index and pointer of val" << endl;
        cout << "========================================" << endl;
        cin >> selected_num;
        if(!(selected_num >= 0 && selected_num <= 4)){
            cout << "you must enter from 0 to 4!" << endl;
            continue;
        }
        if(selected_num == 0){
            cout << "end this program..." << endl;
            break;
        }
        else if(selected_num == 1){
            int add_size;
            cout << "enter the size you want to add" << endl;
            cin >> add_size;
            for(int i = 0; i < add_size; i++){
                cout << "enter numbers : ";
                int num;
                cin >> num;
                list.push_back(num);
            }
            cout << "complete!" << endl;
        }
        else if(selected_num == 2){
            int reduce_size;
            cout << "enter the size you want to reduce" << endl;
            cin >> reduce_size;
            for(int i = 0; i < reduce_size; i++){
                list.pop_back();
            }
            cout << "complete!" << endl;
        }
        else if(selected_num == 3){
            cout << "current list size : " << list.size() << endl;
            cout << "print elements : ";
            list.print_SLL();
        }
        else if(selected_num == 4){
            int val;
            cout << "enter the value that you want to find : ";
            cin >> val;
            cout << "index of value : " << list.find(val) << endl;
            cout << "pointer of value : " << list.find_ptr(val) << endl;
        }
    }
    return 0;
}