Singly Linked List
2022. 5. 7. 02:02ㆍC++/자료구조
Singly Linked List
함수
- int Get(int index) : 해당 인덱스의 값을 리턴한다.
- int IndexTail() : Tail의 인덱스 값을 리턴한다.
- void AddHead(int val) : Head에 값을 넣는다.
- void AddTail(int val) : Tail에 값을 넣는다.
- void AddIndex(int index, int val) : 해당 인덱스에 값을 넣는다.
- int DeleteIndex(int index) : 해당 인덱스의 값을 삭제한다.
- void ShowLinkedList() : List의 값을 Head부터 출력한다.
#SinglyLinkedList.h
struct Node
{
int Value;
Node* Next;
};
class SinglyLinkedList
{
public:
SinglyLinkedList();
~SinglyLinkedList();
int Get(int index);
int IndexTail();
void AddHead(int val);
void AddTail(int val);
void AddIndex(int index, int val);
int DeleteIndex(int index);
bool IsEmpty();
void ShowLinkedList();
private:
Node *head, *tail;
};
SinglyLinkedList.cpp
// 예외처리 공부해보기
#include "SinglyLinkedList.h"
#include <iostream>
SinglyLinkedList::SinglyLinkedList():head(nullptr),tail(nullptr)
{}
SinglyLinkedList::~SinglyLinkedList()
{
while (!IsEmpty())
DeleteIndex(IndexTail());
}
int SinglyLinkedList::Get(int index)
{
if(index>IndexTail())
{
return -1;
}
Node* Point = head;
while (index--)
{
Point = Point->Next;
}
return Point->Value;
}
int SinglyLinkedList::IndexTail()
{
if (IsEmpty())
{
return -1;
}
Node* Point = head;
int index = 0;
while (Point != tail)
{
index++;
Point = Point->Next;
}
return index;
}
void SinglyLinkedList::AddHead(int val)
{
if (IsEmpty())
{
head = new Node;
tail = head;
}
else
{
Node* NewHead = new Node;
NewHead->Next = head;
head = NewHead;
}
head->Value = val;
}
void SinglyLinkedList::AddTail(int val)
{
if (IsEmpty())
{
tail = new Node;
head = tail;
}
else
{
Node* NewTail = new Node;
tail->Next = NewTail;
tail = NewTail;
}
tail->Value = val;
}
void SinglyLinkedList::AddIndex(int index, int val)
{
if (index > IndexTail())
{
std::cout << "index out of bounds" << std::endl;
return;
}
Node* PreNode = head;
--index;
while (index--)
{
PreNode = PreNode->Next;
}
Node* NewNode = new Node;
NewNode->Value = val;
NewNode->Next = PreNode->Next;
PreNode->Next = NewNode;
}
int SinglyLinkedList::DeleteIndex(int index)
{
if (IsEmpty())
{
return -1;
}
if (index == 0)
{
Node* NextHead = head->Next;
head->Next = nullptr;
delete head;
head = NextHead;
}
else if (index == IndexTail())
{
Node* PreTail = head;
index--;
while (index--)
{
PreTail = PreTail->Next;
}
PreTail->Next = nullptr;
delete tail;
tail = PreTail;
}
else if(0<index<IndexTail())
{
Node* PreNode = head;
index--;
while (index--)
{
PreNode = PreNode->Next;
}
Node* Point = PreNode->Next;
PreNode->Next = Point->Next;
Point->Next = nullptr;
delete Point;
}
else
{
std::cout << "index out of bounds" << std::endl;
}
return 0;
}
bool SinglyLinkedList::IsEmpty()
{
return(head == nullptr && tail == nullptr);
}
void SinglyLinkedList::ShowLinkedList()
{
Node* Point = head;
while(Point != tail)
{
std::cout << Point->Value << " -> ";
Point = Point->Next;
}
std::cout << Point->Value << "\n";
}
'C++ > 자료구조' 카테고리의 다른 글
Circular Linked List (0) | 2022.05.07 |
---|---|
이중연결리스트 (0) | 2022.04.30 |
배열을 이용한 큐 구현 (0) | 2022.04.29 |
배열을 이용한 원형 큐의 확장 (0) | 2022.04.29 |
스택: 배열의 확장 (0) | 2022.04.27 |