😂
대학교 시절 데이터베이스 수업시간에 들었을때는 참 힘들었는데;;
시간이 지나서 다시 한번 복기 해보고자 코드를 작성해본다.
using System;
namespace LinkedList
{
class Program
{
static void Main(string[] args)
{
LinkedList list = new LinkedList();
for(int i = 0; i<5;i++)
{
Console.WriteLine("입력하세요");
var a = Console.ReadLine();
list.AddNode(a);
}
while(true)
{
Console.WriteLine("숫자를 입력하세요");
Console.WriteLine("1: 전체 출력, 2:값 검색, 0:종료");
var key = Console.ReadLine();
var convertResult = int.TryParse(key, out int keyValue);
if(convertResult)
{
switch(keyValue)
{
case 1:
list.print();
break;
case 2:
Console.WriteLine("값을 입력하세요");
var a = Console.ReadLine();
list.SearchValue(a);
break;
case 0:
return;
}
}
else
{
break;
}
}
//p.print();
}
}
class Node
{
public string Value{get;set;}
public Node NextNode{get; set;}
}
class LinkedList
{
Node head;
public Node Head
{
get { return this.head; }
set { this.head = value; }
}
Node current;
public Node Current
{
get { return this.current; }
set{this.current = value;}
}
Node tail;
public Node Tail
{
get { return this.tail; }
set {this.tail = value;}
}
public void AddNode(string a)
{
Node newNode = new Node
{
Value = a
};
if(head == null)
{
head = newNode;
tail = head;
}
else
{
tail.NextNode = newNode;
tail = newNode;
}
current = newNode;
}
public void SearchValue(string value)
{
current = head;
int count = 1;
int searchCount = 0;
while(current != null)
{
if(current.Value == value)
{
Console.WriteLine($"{count}번째 노드의 값:{value}\n");
searchCount++;
}
count++;
current = current.NextNode;
}
if(searchCount == 0)
{
Console.WriteLine("찾으시는 값이 없습니다!");
}
}
public void print()
{
current = head;
while(current != null)
{
Console.WriteLine("---------------------------------------");
Console.WriteLine(current.Value);
current = current.NextNode;
}
}
}
}