"링크드리스트"에 해당되는 글 - 1건
Post
간단한 링크드 리스트를 만들어보았다.
서로 참조하는 링크드 리스트는 좀 어려운듯ㅠ_ㅠ
원형 큐도 공부해야 하고@_@ 공부할 것 많다 ㅋㅋ!
자료구조랑 DB랑 C, C++ 기초 확실하게 공부해야 한다.
혹시 오류 있으면 지적해주세요!
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 119 120 121 122 123 124 125 | #include <iostream> using namespace std; class Node { public: int nValue; Node* pNextNode; }; class NodeList { Node* m_pHead; public: NodeList() : m_pHead(NULL) { } ~NodeList() { Node* pTemp = m_pHead; while(pTemp) { Node* pTempNode = pTemp; pTemp = pTemp->pNextNode; delete pTempNode; pTempNode = NULL; } } void Add(int Value) { if(NULL == m_pHead) { Node* pNewNode = new Node(); pNewNode->nValue = Value; pNewNode->pNextNode = NULL; m_pHead = pNewNode; } else { Node* pTemp = m_pHead; while(true) { if(NULL == pTemp->pNextNode) break; pTemp = pTemp->pNextNode; } Node* pNewNode = new Node(); pNewNode->nValue = Value; pNewNode->pNextNode = NULL; pTemp->pNextNode = pNewNode; } } Node* Find(int Value) { Node* pTemp = m_pHead; while(pTemp) { if(pTemp->nValue == Value) return pTemp; pTemp = pTemp->pNextNode; } return NULL; } bool Delete(int Value) { Node* pTemp = m_pHead; Node* pTemp2 = NULL; bool bCheck = false; while(pTemp) { if(pTemp->nValue == Value) { bCheck = true; break; } pTemp2 = pTemp; pTemp = pTemp->pNextNode; } if(bCheck) { if(pTemp == m_pHead) { m_pHead = pTemp->pNextNode; delete pTemp; pTemp = NULL; } else { pTemp2->pNextNode = pTemp->pNextNode; delete pTemp; pTemp = NULL; } } return bCheck; } void Print() { Node* pTemp = m_pHead; while(NULL != pTemp) { printf("%d", pTemp->nValue); pTemp = pTemp->pNextNode; } printf("\n"); } }; | cs |
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 119 120 121 122 123 124 125 126 127 128 129 | class Node { public: int value; Node* m_pNextNode; }; class LinkedList { Node* m_pHead; public: LinkedList() { m_pHead = NULL; } ~LinkedList() { Node* pNode = m_pHead; while(pNode) { Node* pTempNode = pNode->m_pNextNode; pNode = pNode->m_pNextNode; delete pTempNode; pTempNode = NULL; } } void Add(int value) { Node* pNewNode = new Node(); pNewNode->value = value; pNewNode->m_pNextNode = NULL; if(NULL == m_pHead) { m_pHead = pNewNode; } else { Node* pTemp = m_pHead; while(pTemp) { if(NULL == pTemp->m_pNextNode) { pTemp->m_pNextNode = pNewNode; break; } pTemp = pTemp->m_pNextNode; } } } Node* Find(int value) { Node* pNode = m_pHead; bool bResult = false; while(!pNode) { if(pNode->value == value) return pNode; } return NULL; } void Delete(int value) { Node* pTemp = m_pHead; Node* pTemp2 = NULL; bool bCheck = false; while(NULL != pTemp) { if(value == pTemp->value) { bCheck = true; break; } pTemp2 = pTemp; pTemp = pTemp->m_pNextNode; } if(bCheck) { if(m_pHead == pTemp) { m_pHead = pTemp->m_pNextNode; delete pTemp; pTemp = NULL; } else { Node* pNode = pTemp->m_pNextNode; pTemp2->m_pNextNode = pNode; delete pTemp; pTemp = NULL; } } } void Print() { Node* pNode = m_pHead; while(pNode) { cout<< pNode->value <<endl; pNode = pNode->m_pNextNode; } cout<<endl; } }; void main() { LinkedList linkedList; linkedList.Add(1); linkedList.Add(2); linkedList.Add(3); linkedList.Print(); linkedList.Delete(1); linkedList.Delete(2); linkedList.Print(); linkedList.Add(4); linkedList.Print(); getchar(); } | cs |
'이전게시판 > C, C++' 카테고리의 다른 글
C++ 다중상속 문제점? (0) | 2016.04.04 |
---|---|
C++ 데드락(DeadLock) (0) | 2016.04.04 |
2의 보수 만드는 법 (0) | 2016.04.01 |
구조체 패딩이란? (0) | 2016.03.31 |
C++ 스택(Stack) 예제 (0) | 2016.03.20 |
C++ 간단한 원형 큐 (0) | 2016.03.20 |
C++ 문자열 뒤집기, 한글 뒤집기 (0) | 2016.03.18 |
[C/C++]C2512 사용할 수 있는 적절한 기본 생성자가 없습니다 에러 (0) | 2016.02.22 |