브런치 좋다고 소개했었다.
JAVA
package level1;
public class MyNode<E> {
E info;
MyNode<E> next;
public MyNode(E p) {
info = p;
}
}
package level1;
public class LinkedList<E> {
private MyNode<E> mHead;
public LinkedList() {
this.mHead = null;
}
public void add(MyNode<E> pNode) {
if(mHead==null) {
mHead = pNode;
} else {
MyNode<E> n = mHead;
while(n.next != null) n = n.next;
n.next = pNode;
}
}
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add(new MyNode<String>("현종우"));
list.add(new MyNode<String>("우종완"));
list.add(new MyNode<String>("정동민"));
list.add(new MyNode<String>("김유황"));
MyNode<String> n = list.mHead;
while(n != null) {
System.out.println(""+ n.info);
n = n.next;
}
}
}
C++
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
template <class E>
struct MyNode {
E info;
MyNode *next;
MyNode() {}
MyNode(E param) {
this->info = param;
next = nullptr;
}
};
template <class E>
class LinkedList {
private:
MyNode<E> *mHead;
public:
void add(MyNode<E> *pNode) {
if (mHead == NULL) {
mHead = pNode;
}
else {
MyNode<E> *n = mHead;
while (n->next != nullptr) n = n->next;
n->next = pNode;
}
}
MyNode<E> * getHead() const {
return this->mHead;
}
};
int main() {
LinkedList<string> *list = new LinkedList<string>();
list->add(new MyNode<string>("현종우"));
list->add(new MyNode<string>("우종완"));
list->add(new MyNode<string>("정동민"));
list->add(new MyNode<string>("김유황"));
MyNode<string> *n = list->getHead();
while (n != nullptr) {
cout << (string)n->info << endl;
n = n->next;
}
return 0;
}
댓글 없음:
댓글 쓰기
국정원의 댓글 공작을 지탄합니다.