Devsecops

Linked_List(연결 리스트) 본문

Programming/자료구조

Linked_List(연결 리스트)

Futurism 2022. 12. 14. 05:56
728x90

- 연결리스트를 이용한 스택 구현

- 노드 중간 삽입, 삭제 구현

- 기본적으로 head에서 삽입, 삭제가 이루어지는 스택 형식

- 중간 삽입일 경우 2개의 data를 받고 쭉 내려가면서 첫번째 data와 일치하는 가장 가까운 노드의 뒤에 2번째 data를 삽입 하는 형식으로 작성함 

- 삭제 같은 경우 1개의 data를 받고 쭉 내려가면서 가장 먼저 일치한 값을 삭제하는 형식으로 진행 함

 

public class _Node {
	int data; // 데이터 저장 변수
	public _Node link; // 다른 노드를 참조할 링크 노드

	public _Node() {
		this.data = 0;
		this.link = null;
	}

	public _Node(int data) {// 데이터를 입력할때
		this.data = data;
		this.link = null;
	}
}
public class Act {

	_Node head = new _Node();
	_Node pre;

	Act() {
		this.head.data = 0;
		this.head.link = null;
	}

	_Node find(int data) {// 데이터 값을 찾는 곳
		_Node tmp = head;
		this.pre = head;

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

			tmp = tmp.link;
		}
		return tmp.link;
	}

	void insert(int data) {
		_Node no = new _Node(data);
		no.link = head.link;
		this.head.link = no;
	}

	void insert(int predata, int data) {
		_Node no = new _Node(data);

		_Node tmp;
		tmp = find(predata);
		if (tmp != null) {
			no.link = tmp.link;			
			tmp.link = no;
		}
		else {
			System.out.println("삽입 불가");
		}

	}

	void delete() {
		if (head.link == null) {
			System.out.println("데이터 없음");
			return;
		}
		head.link = head.link.link;
	}

	void delete(int data) {
		_Node tmp;
		tmp = find(data);
		if (tmp != null) {
			pre.link = tmp.link;
		} else {
			System.out.println("삭제할 데이터 없음");
			this.pre = null;
		}
	}

	void _print() {
		_Node pr = this.head;
		while (pr.link != null) {
			System.out.println(pr.link.data);
			pr = pr.link;
		}
	}
}
 public class Linked_list {
	public static void main(String[] args) {
		
		Act act = new Act();
		
		act.insert(10);
		act.insert(20);
		
		act.insert(30);
		act.delete(30);

		act.insert(20);
		act.insert(30);
		act.insert(20);
		act.insert(30);
		act.insert(20);
		
		act.insert(30);
		act.delete(30);
		
		act.delete(30);

		act.insert(30, 50);
		
		act._print();
		
	}
}

 

 

ChatGPT가 말하는 개선점

  This code defines two classes, _Node and Act. The _Node class represents a node in a linked list, with two instance variables: data and link. The data variable is used to store the data stored in the node, and the link variable is a reference to the next node in the linked list. The Act class is a linked list implementation that uses _Node objects to store its data. It provides methods for inserting and removing nodes, as well as a method for printing the data stored in the linked list.

One potential issue with this code is that the _Node class uses an underscore in its name. In Java, a class name starting with an underscore is reserved for use by the implementation, and it is generally considered bad practice to use such names for user-defined classes. It is recommended to rename the class to something like Node to avoid potential conflicts.

  Another potential issue is that the Act class has a public head instance variable, which represents the head of the linked list. In general, instance variables should be private to prevent external code from accessing and modifying them directly. It is recommended to make the head variable private and provide getter and setter methods for accessing and modifying it.

  Additionally, the _Node class does not override the toString() method, which means that if an instance of the class is printed using the System.out.println() method, it will print the name of the class and its memory address instead of the data stored in the node. It is recommended to override the toString() method in the _Node class to return the data stored in the node as a string.

Overall, this code provides a basic implementation of a linked list, but it could be improved in a few ways.

 

  1. Rename the _Node class to something like Node to avoid using a reserved name.
  2. Make the head instance variable private and provide getter and setter methods for accessing and modifying it.
  3. Override the toString() method in the _Node class to return the data stored in the node as a string.

These changes would make the code more readable and maintainable.

728x90

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

Hash_Map(해쉬 맵)  (0) 2022.12.19
Queue(큐)  (0) 2022.12.10