728x90
반응형
// 선행처리 지시문
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
//헤더 추가 -> 클래스
#include "Date.h"
#include "Person.h"
#include "Wlink.h"
// -< Linked List >-------------------------------------------------------------------------
void initHeadTail( Wlink* LkList );
void showLinkMenu();
void selectMenu( int choice, Wlink* LkList );
void showDataLinkList( Wlink* LkList ,int n );
void addLinkDataInTail( Wlink* LkList );
void addLinkDataBettwinData( Wlink* LkList );
Wlink* searchLinkData( Wlink* LkList );
void deleteLinkData( Wlink* LkList );
// -------------------------------------------------------------------------------------------
int main() {
Wlink* LkList = new Wlink;
initHeadTail( LkList );
int choice = -2;
/*cout << "char " << sizeof( char ) << endl;
cout << "int " << sizeof( int ) << endl;
cout << "bool " << sizeof( bool ) << endl;
cout << "Wlink " << sizeof( new Wlink ) << endl;
cout << "LkList " << sizeof( LkList ) << endl;*/
while ( true )
{
showLinkMenu();
cin >> choice;
if ( choice == -1 ) {
break;
}
else {
selectMenu( choice, LkList );
}
}
return 0;
}
// -< Linked List >-------------------------------------------------------------------------
void initHeadTail( Wlink* LkList ) {
// Wlink *head = new Wlink;
LkList->setHead( true );
//Wlink* data = new Wlink;
//data->setTail( false );
//data->setData( 22 );
//LkList->setLink( data );
Wlink* tail = new Wlink;
tail->setTail( true );
tail->setData( 33 );
tail->setLink( nullptr );
LkList->setLink( tail );
// data->setLink( tail );
/*
cout << "Head => "<< LkList->getData() << endl;
cout << "Head => " << LkList->getLink() << endl;
cout << "tail => " << &tail << endl;
cout << "tail => " << tail->getData() << endl;
cout << "tail => " << tail->getLink() << endl;
cout << "test => " << LkList->getLink()->getData() << endl;*/
}
void showLinkMenu() {
cout << "=====<<메뉴 선택>>=====" << endl;
cout << "1.리스트 보기 " << endl;
cout << "2.데이터 추가" << endl;
cout << "3.데이터 중간 추가" << endl;
cout << "4.데이터 탐색 " << endl;
cout << "5.데이터 삭제" << endl;
cout << "-1. 프로그램 종료" << endl;
cout << "======================= " << endl << endl;
}
void selectMenu( int choice, Wlink* LkList ) {
switch ( choice )
{
case 1: {
// 보기
showDataLinkList( LkList ,0 );
break;
}
case 2: {
// 추가
addLinkDataInTail( LkList );
break;
}
case 3: {
// 중간 추가
addLinkDataBettwinData( LkList );
break;
}
case 4: {
// 탐색
searchLinkData( LkList );
break;
}
case 5: {
// 삭제
deleteLinkData( LkList );
break;
}
default:
break;
}
}
void showDataLinkList( Wlink* arr ,int n ) {
if ( arr->getHead() != true && arr->getTail() != true) {
n = n + 1;
cout << n << "번째 데이터 : " << arr->getData() << endl;
}
if ( arr->getTail() != true ) {
showDataLinkList( arr->getLink() ,n );
}
}
// 끝에 데이터 삽입 ( 헤더 <데이터> 테일 )
void addLinkDataInTail( Wlink* LkList ) {
int inputData;
cout << "추가 데이터 입력 : ";;
cin >> inputData;
cout << "====================="<<endl<<endl;
Wlink* data = new Wlink; //추가할 데이터
data->setData( inputData );
Wlink *preData = LkList; //마지막 Tail 바로앞 데이터
while ( true )
{
if ( preData->getLink()->getTail() != true) {
preData = preData->getLink();
}
else {
// 다음것이 종료라면..
Wlink* tmp = new Wlink;
tmp->setLink( preData->getLink() );
preData->setLink( data );
data->setLink( tmp->getLink() );
delete tmp;
break;
}
}
}
// 중간에 데이터 삽입.
void addLinkDataBettwinData( Wlink* LkList ) {
cout << "==< 목록 > ===================" << endl;
showDataLinkList( LkList, 0 );
int inputData;
cout << "추가 데이터 입력 : ";;
cin >> inputData;
cout << "=====================" << endl << endl;
Wlink* data = new Wlink; //추가할 데이터
data->setData( inputData );
int position;
cout << "삽입할 위치 번호를 입력하세요( 번호 뒤에 삽입 ) => : ";
cin >> position;
cout << "===========================" << endl << endl;
Wlink* moving = LkList;
Wlink* preData = nullptr;
for ( int i = 0; i <= position; i++ ) {
if ( i == position ) {
preData = moving;
}
else {
moving = moving->getLink();
}
}
Wlink* tmp = new Wlink;
tmp->setLink( preData->getLink() );
preData->setLink( data );
data->setLink( tmp->getLink() );
}
Wlink* searchLinkData( Wlink* LkList ) {
int count = 1;
int inputData;
cout << "찾을 데이터 입력 : " ;
cin >> inputData;
cout << "=====================" << endl << endl;
Wlink* tmp = LkList;
while ( true )
{
if ( tmp->getTail() ) {
cout << "===== 데이터 없음 ======" << endl;
tmp = nullptr;
break;
}
else {
if ( tmp->getData() == inputData ) {
cout << "===== 데이터가 " << count-1 <<"번째 있습니다.======" << endl;
break;
}
else {
tmp = tmp->getLink();
}
}
count++;
}
return tmp;
}
void deleteLinkData( Wlink* LkList )
{
// 데이터를 끊어주기전에 사이를 이어줘야한다.
cout << "==< 목록 > ===================" << endl ;
showDataLinkList( LkList, 0 );
int inputData;
cout << "삭제할 데이터 번호를 입력하세요 => : ";
cin >> inputData;
cout << "===========================" << endl << endl;
Wlink* moving = LkList;
Wlink *preData = nullptr;
Wlink *targetData = nullptr;
for ( int i = 0; i <= inputData; i++ ) {
if ( i == (inputData - 1) ) {
// 이전 노드(?)
preData = moving;
}
if ( i == inputData ) {
// 타겟 노드(?)
if ( moving->getHead() == true || moving-> getTail() == true) {
targetData = nullptr;
}
else {
targetData = moving;
}
}
moving = moving->getLink();
}
if ( targetData == nullptr ) {
cout << "=< 선택오류 > ====================" << endl;
}
else {
cout << "=< 타겟 > =======================" << endl;
cout << inputData << " 번째 데이터 " << endl;
cout << targetData->getData() << " 를 삭제 합니다. " << endl;
Wlink* tmp = new Wlink;
tmp->setLink( targetData ->getLink() );
preData->setLink( tmp->getLink() );
delete targetData;
delete tmp;
}
}
Wlink.cpp
#include "Wlink.h"
Wlink::Wlink()
{
}
void Wlink::setHead( bool ish )
{
this->isHEAD = ish;
}
bool Wlink::getHead()
{
return this->isHEAD;
}
void Wlink::setTail( bool ist )
{
this->isTail = ist;
}
bool Wlink::getTail()
{
return this->isTail;
}
void Wlink::setData( int value )
{
this->data = value;
}
int Wlink::getData()
{
if ( this->isTail == true ) {
return 99;
}
else if ( this->isHEAD == true ) {
return 11;
}
else {
return this->data;
}
}
void Wlink::setLink( Wlink* link )
{
this->link = link;
}
Wlink* Wlink::getLink()
{
return this->link;
}
Wlink.h
#ifndef Wlink_H
#define Wlink_H
class Wlink
{
private :
bool isHEAD = false; // 1
bool isTail = false; // 1
int data = 55; // 4
Wlink* link; // 다음을 가르키는 주소 // 8
public:
Wlink();
void setHead( bool ish );
bool getHead();
void setTail( bool ist );
bool getTail();
void setData( int value );
int getData();
void setLink( Wlink* link );
Wlink* getLink();
};
#endif // DEBUG
https://github.com/Karlina-Bytes/LinkedList_Tutorial
728x90
반응형
'C++' 카테고리의 다른 글
C++ 원형 LinkedList (0) | 2022.11.02 |
---|---|
C++ 배열 (0) | 2022.11.01 |
C++ 클래스 생성 / 헤더파일. (0) | 2022.10.28 |
C++ 함수 포인터 (0) | 2022.10.27 |
C++ 공용체 ( union ) (0) | 2022.10.27 |