본문 바로가기
::public/자료구조

LinkedList 만들어 보기.

by 해맑은욱 2019. 6. 28.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once
#include <iostream>
using namespace std;
class Node
{
public:
    int nData;
    Node* pNext;
public:
    Node()
    {
        pNext = NULL;
    }
};
class LinkedList
{
public:
    int nSize;
    Node* pHead;
public:
    LinkedList()
    {
        nSize = 0;
        pHead = new Node;
    }
    ~LinkedList() 
    {
        Node* pTemp = pHead;
        for (int i = 0; i < nSize; i++)
        {
            pTemp = pTemp->pNext;
            delete pHead;
            pHead = pTemp;
        }
    }
    void push_front(int nData)
    {
    }
    void pop_front()
    {
    }
    void push_back(int nData)
    {
        cout << "push_back = " << nData << endl;
        if (nSize == 0)                // 데이터가 없으면
        {
            pHead->nData = nData;    // 헤드에 데이터 넣고
            pHead->pNext = NULL;    
        }
        else
        {
            Node* pNewNode = new Node;    // 새로운 노드를 만들어 세팅
            pNewNode->nData = nData;
            pNewNode->pNext = NULL;
            Node* pTemp = pHead;        // 연결하기 위한 시작점
            while (pTemp->pNext != NULL)// 마지막 노드 전까지만 
            {
                pTemp = pTemp->pNext;    // 연결 될 노드를 찾고 
            }
            pTemp->pNext = pNewNode;    // 마지막 노드에 새노드 연결
        }
        nSize++;    // 리스트 사이즈 추가됨
    }
    void pop_back()
    {
        if (nSize == 0)
        {
            cout << "empty list" << endl;
        }
        else
        {
            if (nSize == 1)
            {
                cout << "pop_back = " << pHead->nData << endl;
                delete pHead;
                pHead = NULL;
            }
            else
            {
                Node* pTemp = pHead;
                Node* pPrevNode = NULL;
                while (pTemp->pNext != NULL)
                {
                    pPrevNode = pTemp;
                    pTemp = pTemp->pNext;
                }
                cout << "pop_back = " << pTemp->nData << endl;
                delete pTemp;
                pTemp = NULL;
                pPrevNode->pNext = NULL;
            }
            nSize--;
        }        
    }
    int size()
    {
        return nSize;
    }
    void printNode()
    {
        if (nSize == 0)
        {
            cout << "===== NodeData is empty" << endl;
        }
        else
        {
            Node* pTemp = pHead;
            cout << "===== NodeData = ";
            for (int i = 0; i < nSize; i++)
            {
                cout << pTemp->nData << " ";
                pTemp = pTemp->pNext;
            }
            cout << endl;
        }        
    }
};
 
cs

'::public > 자료구조' 카테고리의 다른 글

우선순위 큐(힙)  (0) 2019.09.03
이진 탐색 트리  (0) 2019.09.03
스레드 이진트리  (0) 2019.08.29
순환  (0) 2019.08.22
LinkedList 구현.  (0) 2019.08.21