#include<iostream>
using namespace std;
/*
간단한 연결리스트
데이터 관리
*/
class Node
{
public:
Node* nextNode;
Node* prevNode;
int nodeData;
int nodeIndex;
Node(){
nextNode = NULL;
prevNode = NULL;
nodeData = 0;
nodeIndex = 0;
}
Node(int _data){
nextNode = NULL;
prevNode = NULL;
nodeData = _data;
nodeIndex = 0;
}
};
class ListManager
{
private:
int Count;
Node* firstPoint;
public:
ListManager(){
Count=0;
firstPoint = new Node();
}
void Insert(int addedData){
Node* newNode = new Node(addedData);
if(firstPoint->nextNode == NULL)
{
firstPoint->nextNode = newNode;
newNode->prevNode = firstPoint;
newNode->nodeIndex = Count; //nodeIndex=0;
}//맨 처음이라면 새로생성한 노드와 firstPoint연결해주고 종료
else{
Node* nowNode = firstPoint;
while(nowNode->nextNode != NULL)
{
nowNode = nowNode->nextNode;
}
nowNode->nextNode = newNode;
newNode->prevNode = nowNode;
newNode->nodeIndex = Count;
}
Count++;
}
void showAll(){
Node* nowNode = firstPoint;
if(nowNode->nextNode == NULL){
cout<<"데이터가 하나도 없습니다. 먼저 Insert해주세요"<<endl;
return;
}
while(nowNode->nextNode !=NULL)
{
nowNode = nowNode->nextNode;
cout<<nowNode->nodeData<<"\t";
}
return;
}
//해당 Index의 노드 제거
void RemoveAt(int Index){
Node* nowNode = firstPoint;
if(nowNode->nextNode == NULL)
{
cout<<"데이터가 하나도 없습니다. 먼저 Insert해주세요"<<endl;
}
else
{
while(nowNode->nextNode != NULL)
{
nowNode = nowNode->nextNode;
if(nowNode->nodeIndex == Index)
{
nowNode = nowNode->prevNode;
if(nowNode->nextNode->nextNode == NULL)
{
delete nowNode->nextNode;
nowNode->nextNode = NULL;
}
else
{
nowNode->nextNode = nowNode->nextNode->nextNode;
delete nowNode->nextNode->prevNode;
nowNode->nextNode->prevNode = nowNode;
}
break;
}
}
while(nowNode->nextNode != NULL)
{
nowNode = nowNode->nextNode;
nowNode->nodeIndex -= 1;
}
Count--;
}
}
int getCount(){
return Count;
}
~ListManager(){
Node* nowNode = firstPoint;
if(nowNode->nextNode == NULL)
return;
nowNode = nowNode->nextNode;
while(nowNode->nextNode != NULL)
{
nowNode = nowNode->nextNode;
delete nowNode->prevNode;
}
delete nowNode;
}
};
int main()
{
ListManager lm;
int op=0;
int tempNum;
cout<<"------------------간단한 리스트입니다.-----------------"<<endl;
while(1)
{
cout<<"---------------------------------------\n"
<<"1. Insert\n"
<<"2. Remove\n"
<<"3. ShowAll\n"
<<"4. 현제 데이터갯수 출력\n"
<<"5. 종료\n"
<<"---------------------------------------"<<endl;
cin>>op;
switch(op)
{
case 1:
cout<<"\n추가할 정수 입력::"; cin>>tempNum;
lm.Insert(tempNum);
break;
case 2:
cout<<"\n제거할 노드의 인덱스 입력::"; cin>>tempNum;
if(tempNum < 0 || tempNum>= lm.getCount()){
cout<<"error!"<<endl;
break;
}
lm.RemoveAt(tempNum);
break;
case 3:
cout<<endl;
lm.showAll();
cout<<endl;
break;
case 4:
cout<<"현제 노드의 총 개수는 "<<lm.getCount()<<" 개 입니다."<<endl;
break;
case 5:
return 0;
default:
cout<<"삑-. 잘못된 입력!"<<endl;
break;
}
}
return 0;
}