|자료구조| 1 | Circular LinkedList_2 | 원형 연결 리스트

2022. 3. 13. 14:47·PS/자료구조
no title

원형 연결 리스트 구현

Visual Studio 솔루션 구성


구현부

노드 - 헤더 : Node.h

#pragma once
#include "CircuitLinkedList.h"
class Node
{
    friend class CircuitLinkedList;
protected :
	int data;
	Node* next;
};

원형 연결 리스트 - 헤더 : CircuitLinkedList.h

#pragma once
#include "Node.h"
class CircuitLinkedList
{
	friend class Node;
private :
	//멤버변수
	Node* tail;
	int length;
public :
	//매서드
	CircuitLinkedList();
	void LInsert(int data);
	int LDel();
	int LStatus();
};

//이곳은 그냥 클래스의 구성요소만 적기를 바란다

원형 연결 리스트 - 메소드 : CircuitLinkedList.cpp

#include "CircuitLinkedList.h"
#include "Node.h"
#include <iostream>
#include <algorithm>
using namespace std;

CircuitLinkedList::CircuitLinkedList() {
	this->tail = NULL;
	this->length = 0;
}

void CircuitLinkedList::LInsert(int _data) {
	Node* newNode = new Node;
	newNode->data = _data;

	//리스트가 하나도 없었을때
	if (this->tail == NULL) {
		this->tail = newNode;
		//원형리스트라서 자기자긴을 붙여야함
		newNode->next = newNode;
	}
	else {
		//마지막을 찾아갈때 while 쓸필요가 없는게 어차핀 tail이 마지막을 보장한다
		//while (this->tail != NULL) {}

		//this->tail은 마지막 노드 그 자체
			//그래서 마지막노드 넥스트를 사용할 수 있다.
				//그렇다면 아까부터 head가 없었는데 head는 어떻게 지목하지?
				//tail->next는 바로 head를 가르키는것이다.
		newNode->next = this->tail->next;
		this->tail->next = newNode;
		this->tail = newNode;
	}
	(this->length)++;
}

int CircuitLinkedList::LDel() {
	//tail 삭제
	if (this->length == 0)
		throw out_of_range("CircuitLinkedList 노드가 없습니다");
	//rpos는 기존 tail이 삭제되고
		//새로운 tail이 생기고 나서의 다음 head(tail->next)
	Node* rpos = this->tail->next;
	int result = this->tail->data;
	//arr의 다음은 tail 
		//tail이 사라지면 다음 tail의 후보가 된다
	Node* arr = this->tail->next;
	while (1) {
		if (arr->next == this->tail)
			break;
		else
			arr = arr->next;
	}
	delete this->tail;
	this->tail = arr;
	this->tail->next = rpos;
	this->length--;
	return result;
}

int CircuitLinkedList::LStatus() {
	cout << "현재 개수 : " << this->length << endl;

	if (this->length == 0) return 0;

	cout << "처음 ::  " << this->tail->next->data << endl;
	cout << "끝 :: " << this->tail->data << endl;

	Node* arr = this->tail->next;
	for (; this->tail != arr;)
	{
		cout << arr->data << " ";
		arr = arr->next;
	}

	// for문에서 출력 되지 못하는 마지막 값 출력
	cout << arr->data << endl;
	return 1;
}

//헤더파일을 include 하자
//여기는 클래스 매서드 구현부를 적는다

적용부

#include "CircuitLinkedList.h"
#include <iostream>
#include <algorithm>
using namespace std;

void _Interface_(CircuitLinkedList *_plist) {
	while (1) {
		cout << "1.삽입 " << "2.삭제 " << "3.출력 " << "4.종료 " << '\n';
		int num; cin >> num;
		if (num == 1) {
			int inputNum; cin >> inputNum;
			_plist->LInsert(inputNum);
		}
		else if (num == 2) {
			cout << _plist->LDel() << '\n';
		}
		else if (num == 3) {
			_plist->LStatus();
		}
		else if (num == 4) {
			break;
		}
	}
}

int main() {
	CircuitLinkedList Clist;
	_Interface_(&Clist);
}

//클래스 헤더파일을 사용하자
//클래스를 사용할 메인 부분이다
저작자표시

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

|자료구조| 3 | BinSearchTree | 이진 탐색 트리  (0) 2022.03.13
| 니앙팽이 - 자료구조| 2 | 스택,큐 노트  (0) 2022.03.13
| 니앙팽이 - 자료구조| 1 | 연결 리스트 노트  (0) 2022.03.13
|자료구조| 1 | Double LinkedList_3 | 이중 연결 리스트  (0) 2022.03.13
|자료구조| 1 | LinkedList_1 | 연결리스트(더미노드 없음)  (0) 2022.03.13
'PS/자료구조' 카테고리의 다른 글
  • | 니앙팽이 - 자료구조| 2 | 스택,큐 노트
  • | 니앙팽이 - 자료구조| 1 | 연결 리스트 노트
  • |자료구조| 1 | Double LinkedList_3 | 이중 연결 리스트
  • |자료구조| 1 | LinkedList_1 | 연결리스트(더미노드 없음)
니앙팽이
니앙팽이
  • 니앙팽이
    니앙팽이 블로그
    니앙팽이
  • 전체
    오늘
    어제
    • 분류 전체보기 (126)
      • 그림그리기 (7)
      • 음악 (4)
        • FL Studio & MIDI (2)
        • 자작곡 (2)
      • 게임 (7)
        • 모바일 (0)
        • 스팀 (0)
        • 닌텐도 (0)
        • 개발 (7)
      • CS (44)
        • SW 공학 (27)
        • DB (7)
        • OS (9)
        • 네트워크 (1)
      • 팁 (9)
      • Language (21)
        • C# (8)
        • C&C++ (3)
        • 파이썬 메모 (3)
        • Javascript (7)
      • PS (0)
        • 알고리즘 (24)
        • 자료구조 (8)
        • 수학 (1)
        • 선형대수 (0)
        • 오토마타 (1)
        • 이산수학 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    프로세스
    객체지향개발
    가비지 콜렉터
    알고리즘
    클립 스튜디오
    c#
    파이썬
    따라그리기
    연결리스트
    노마드 코더
    Stack
    디자인패턴
    프로그래머스
    KAKAO
    clip studio paint
    자료구조
    unity
    Javascript
    유니티
    그림 연습
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
니앙팽이
|자료구조| 1 | Circular LinkedList_2 | 원형 연결 리스트
상단으로

티스토리툴바