增加节点 this java,在Java中创建节点类

So I'm fairly new to Java and programming and I was wondering how to create a node class?

So far I have:

public class ItemInfoNode{

private ItemInfoNode next;

private ItemInfoNode prev;

private ItemInfo info;

public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev){

info = info;

next = next;

prev = prev;

}

public void setInfo(ItemInfo info){

info = info;

}

public void setNext(ItemInfoNode node){

next = node;

}

public void setPrev(ItemInfoNode node){

prev = node;

}

public ItemInfo getInfo(){

return info;

}

public ItemInfoNode getNext(){

return next;

}

public ItemInfoNode getPrev(){

return prev;

}

}

Pretty much the question asked for those methods so I put those down but, the next question asks me to refer to the head and tail of ItemInfoNode nodes. Just a bit confused here. Thanks

EDIT: Thanks for the help guys! I'm trying to create an "InsertInfo" method that puts information like the name, price, tag number, etc. Into one node. How do I go about creating this method?

So far I got this.. I have an Iteminfo constructor in a different class that has all of these but, I'm not sure how to use that/if I am even supposed to do..

public void InsertInfo(String name, String rfdnumber, double price, String original_position){

head = new ItemInfoNode (Iteminfo, head);

}

解决方案

Welcome to Java!

This Nodes are like a blocks, they must be assembled to do amazing things!

In this particular case, your nodes can represent a list, a linked list, You can see an example here:

public class ItemLinkedList {

private ItemInfoNode head;

private ItemInfoNode tail;

private int size = 0;

public int getSize() {

return size;

}

public void addBack(ItemInfo info) {

size++;

if (head == null) {

head = new ItemInfoNode(info, null, null);

tail = head;

} else {

ItemInfoNode node = new ItemInfoNode(info, null, tail);

this.tail.next =node;

this.tail = node;

}

}

public void addFront(ItemInfo info) {

size++;

if (head == null) {

head = new ItemInfoNode(info, null, null);

tail = head;

} else {

ItemInfoNode node = new ItemInfoNode(info, head, null);

this.head.prev = node;

this.head = node;

}

}

public ItemInfo removeBack() {

ItemInfo result = null;

if (head != null) {

size--;

result = tail.info;

if (tail.prev != null) {

tail.prev.next = null;

tail = tail.prev;

} else {

head = null;

tail = null;

}

}

return result;

}

public ItemInfo removeFront() {

ItemInfo result = null;

if (head != null) {

size--;

result = head.info;

if (head.next != null) {

head.next.prev = null;

head = head.next;

} else {

head = null;

tail = null;

}

}

return result;

}

public class ItemInfoNode {

private ItemInfoNode next;

private ItemInfoNode prev;

private ItemInfo info;

public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev) {

this.info = info;

this.next = next;

this.prev = prev;

}

public void setInfo(ItemInfo info) {

this.info = info;

}

public void setNext(ItemInfoNode node) {

next = node;

}

public void setPrev(ItemInfoNode node) {

prev = node;

}

public ItemInfo getInfo() {

return info;

}

public ItemInfoNode getNext() {

return next;

}

public ItemInfoNode getPrev() {

return prev;

}

}

}

EDIT:

Declare ItemInfo as this:

public class ItemInfo {

private String name;

private String rfdNumber;

private double price;

private String originalPosition;

public ItemInfo(){

}

public ItemInfo(String name, String rfdNumber, double price, String originalPosition) {

this.name = name;

this.rfdNumber = rfdNumber;

this.price = price;

this.originalPosition = originalPosition;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getRfdNumber() {

return rfdNumber;

}

public void setRfdNumber(String rfdNumber) {

this.rfdNumber = rfdNumber;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public String getOriginalPosition() {

return originalPosition;

}

public void setOriginalPosition(String originalPosition) {

this.originalPosition = originalPosition;

}

}

Then, You can use your nodes inside the linked list like this:

public static void main(String[] args) {

ItemLinkedList list = new ItemLinkedList();

for (int i = 1; i <= 10; i++) {

list.addBack(new ItemInfo("name-"+i, "rfd"+i, i, String.valueOf(i)));

}

while (list.size() > 0){

System.out.println(list.removeFront().getName());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值