자료구조: 연결리스트 Search 연산을 알아보아요

Jhoon·2022년 11월 19일
0

본 내용은 공부를 위한 내용으로 틀린내용이 있을수 있습니다.

저번 내용에서 아래의 Class 를 구현해 보았다.

interface Node {
	data: string
  	link: Node
}

type NodeType = Node | null

class LinkedList {
	constructor(head: NodeType = null) {}
  	public insertLastNode(data: string) {
    	const newNode: Node = { data, link: null };	
      
      	if(this.head == null) {
        	this.head = newNode 
        } else {
          	let tmp: NodeType = head
        	while(tmp != null) tmp = tmp.link
          	tmp.link = newNode
        }
    }
  	public deleteLastNode() {
    	if (this.head == null) return
      	if (this.head.link == null) this.head = null
      	let pre: Node = this.head
        let tmp: Node = this.head.link
        
        while(tmp.link != null) {
        	pre = tmp
          	tmp = tmp.link
        }
      	pre = null
    }
}

다음의 연산로직은 위의 Class 에 Method 를 추가하여 구현한다.

연결리스트 Search 연산을 알아보아요.

배열을 보면 쉽게 원하는 원소를 가져올 수 있다.
Typescript 에서의 배열을 한번 살펴보도록 하자.

const week: string[] = ['월', '수', '금']
console.log(arr[0]) // 월

위의 코드를 살펴보면 week 배열에서 '월' 을 가져와 콘솔로 찍어낸다.

연결리스트에서 Node 를 Search 하기 위해서는 Node.data 의 값과 찾고자 하는 Data 가 동일하면, 해당 Node 를 반환한다.

만약 그렇지 않으면 마지막 Node 를 반환한다.

연결리스트에서 원하는 Node 의 data 를 가져와 사용할 수 있도록 하기위해 Search 연산을 구현해보자.


class searchNode(data: string) {
	if (this.head == null) return
  	let tmp: Node = this.head
    
    while(tmp.link != null) {
    	if (tmp.data == data) return tmp
      	tmp = tmp.link
    }
  	return tmp
}

위 code 를 살펴보도록 하자.

if (this.head == null) return

위 연산은 this.head 에 아무런 값이 없는 null 값이라면 null 을 return 한다.

하지만 그렇지 않다면 다음의 code 가 실행된다.

let tmp: Node = this.head

tmp 에 현재의 첫번째 Node 를 할당하기 위해 this.head 를 할당한다.

while(tmp.link != null) {
	if (tmp.data == data) return tmp
  	tmp = tmp.link
}

위의 code 는 while 문을 사용하여 tmp.link 가 null 이 아니라면 true 가 되어 지속 반복한다.

여기서 tmp.data 가 data prarmeter 의 값과 같다면, 해당 tmp 를 return 하며 그렇지 않다면 tmp 에 다음 Node 를 할당한다.

return tmp

while 문을 통해 tmp 는 마지막 노드를 할당했다는점 알아두자.

위의 코드는 while 문을 통해 반복하였지만, 맞는 data 를 가진 Node 가 없다면 마지막 노드를 반환하도록 하는 코드이다.

이렇게 searchNode 를 구현해 보았다.

그럼 맨위에 첫번째 배열을 통해 '월' 을 콘솔로 찍었듯이, 위 LinkedList class 로직을 사용하여, '월' 을 search 하여 콘솔로 찍어본다.

// linkedList.ts
export interface Node {
	data: string
  	link: NodeType
}

export type NodeType = Node | null

export class LinkedList {
	costructor(private head: NodeType = null) {}
  
  	public insertLastNode(data: string) {
    	let newNode: Node = { data, link: null }
        if (this.head == null) {
          	this.head = newNode 
    	} else {
        	let tmp: Node = this.head
            while(tmp.link != null) {
            	tmp = tmp.link
            }
          	tmp.link = newNode
        } 
    }
  
  	public deleteLastNode() {
    	if (this.head == null) return
      	if (this.head.link == null) this.head = null
      	let pre: Node = this.head
        let tmp: Node = this.head.link
        
        while(tmp.link != null) {
        	pre = tmp
          	tmp = tmp.link
        }
      	pre = null
    }
  
	public searchNode(data: string) {
    	if (this.head == null) return
      	let tmp: NodeType = this.head
        
        while(tmp != null) {
        	if (tmp.data == data) return tmp
          	tmp = tmp.link
        }
      	return tmp
    }
}

위는 지금까지 구현한 linkedList class 라고 보면된다.
위의 class 를 index.ts 에서 console 로 찍어보도록 하자.

// index.ts

import {Node, NodeType, LinkedList} from "./LinkedList"

const L: LinkedList = new LinkedList()
L.insertLastNode('월')
L.insertLastNode('수')
L.insertLastNode('금')
const mon: NodeType = L.searchNode('월')
if (mon != null) console.log(mon.data) // 월

LinkedList 를 구현하여 해당 원하는 data 를 가져올수 있는것을 확인해 보았다.

TO BE CONTINUED

이상으로 search 연산을 살펴보았다.
다음은 LinkedList 의 Print 연산과 LinkedList 의 중간에 insert 하기 위한 insertMiddleNode method 를 구현해 보자.

profile
익숙해지면 다 할수 있어!!

0개의 댓글