java中的循环链表问题
您好,我的循环链表类遇到问题。我假设有一个循环链接的类,它运行一定数量的元素。当它到达列表的末尾时,它会一直移回到列表的开头,并重新开始,就像循环自身一样。好吧,我的问题是我无法使用我所做的方法让我的列表循环。我假设有一个将元素添加到列表末尾的方法和一个将它们设置到列表前面的方法。好吧,我的设置前端无法正常工作,所以我想我发布并看看是否有任何代码有帮助。我也想用字符串运行一个循环,就像我想创建一个循环链表一样,它遍历一周中的每一天,从星期日开始,到星期六结束,然后将星期六链接到星期日,并在整个循环中进行循环,任何人都可以告诉我如何在测试我的代码时执行此操作。
我的输出就像
应打印 1 2 3 4 1 2 3 4 1 2 3
1 2 3 4 1 2 3 4 1 2 3
应打印 3 4 1 2 3 4 1 2 3 4 1
3 1 2 3 4 1 2 3 4 1 2
应打印 3 4 1 2 -1 3 4 1 2 -1 3
3 1 2 3 4 -1 3 1 2 3 4
应打印 3 1 2 -1 3 1 2 -1 3 1 2
3 1 2 3 -1 3 1 2 3 -1 3
代码:
public class LinkedListIterator<T> implements Iterator<T> {
private PublicNode<T>first;
private PublicNode<T>current;
public LinkedListIterator(PublicNode<T> first){
this.first =first;
current = first;
}
public boolean hasNext() {
return current!=null;
}
public T next() {
if(!hasNext()){
throw new NoSuchElementException();
}
T result = current.getElement();
current = current.getNext();
return result;
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public void setFirst(PublicNode<T> first) {
this.first = first;
}
public PublicNode<T> getFirst() {
return first;
}
}
和
public class CircularLinkedList<T> implements CircularList<T> {
private PublicNode<T> head;
private PublicNode<T> tail;
private int size;
public CircularLinkedList() {
head = null;
tail = null;
size = 0;
}
//bigO(1)
public PublicNode<T> getHead() {
return head;
}
//bigO(1)
public void setHead(PublicNode<T> head) {
this.head = head;
}
//bigO(1)
public PublicNode<T> getTail() {
return tail;
}
//bigO(1)
public void setTail(PublicNode<T> tail) {
this.tail = tail;
}
//bigO(1)
public int getSize() {
return size;
}
//bigO(1)
public void setSize(int count) {
this.size = count;
}
//bigO(1)
public boolean isEmpty() {
return tail == null || head == null;
}
// add element to the end of the list
public void addLast(T element) {
PublicNode<T> node = new PublicNode<T>(element);
if(this.tail==null){
node.setNext(null);
node.setPrevious(null);
this.tail=node;
}else{
PublicNode<T> oldTail = this.tail;
oldTail.setNext(node);
node.setNext(head);
node.setPrevious(oldTail);
this.tail =node;
}if(this.head==null){
this.head=node;
}
this.size++;
}
// set element to be front of the list
//bigO(n)
public void setFront(T element) {
PublicNode<T> node = new PublicNode<T>(element);
if (isEmpty()) {
throw new NoSuchElementException();
}else{
PublicNode<T> oldHead = this.head;
oldHead.setPrevious(node);
node.setNext(oldHead);
node.setPrevious(null);
this.head=node;
}
if(this.tail==null){
this.tail=node;
}
this.size++;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CircularList<Integer> list = new CircularLinkedList<Integer>();
for (int i = 1; i <= 4; i++) {
list.addLast(i);
}
System.out.println("\nShould print 1 2 3 4 1 2 3 4 1 2 3");
Iterator<Integer> iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
System.out.println();
list.setFront(3);
System.out.println("Should print 3 4 1 2 3 4 1 2 3 4 1");
iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
System.out.println();
list.addLast(-1);
System.out.println("Should print 3 4 1 2 -1 3 4 1 2 -1 3");
iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
System.out.println();
list.remove(4);
System.out.println("Should print 3 1 2 -1 3 1 2 -1 3 1 2");
iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
}
}
hi im having trouble with my circular linked list class. im suppose to have a circular linked class that runs through a set amount elements. when it reaches the end of the list it moves all the way back to the begining of the list and starts all over kind of like looping over itself. well my problem is i cant get my list to loop proably with the methods i have made. im suppose to have a method that adds elements to the end of the list and a method that sets them to the front of the list. well my set front is not working right so i thought i post and see if any one code help. also i want to run a loop with strings like i want to create a circular linked list that goes through the days of the week starting at sunday ending at saturday then linking saturday to sunday and doing the loop all over agian can anyone show me how to do this in testing my code.
my output is coming out like
Should print 1 2 3 4 1 2 3 4 1 2 3
1 2 3 4 1 2 3 4 1 2 3
Should print 3 4 1 2 3 4 1 2 3 4 1
3 1 2 3 4 1 2 3 4 1 2
Should print 3 4 1 2 -1 3 4 1 2 -1 3
3 1 2 3 4 -1 3 1 2 3 4
Should print 3 1 2 -1 3 1 2 -1 3 1 2
3 1 2 3 -1 3 1 2 3 -1 3
Code:
public class LinkedListIterator<T> implements Iterator<T> {
private PublicNode<T>first;
private PublicNode<T>current;
public LinkedListIterator(PublicNode<T> first){
this.first =first;
current = first;
}
public boolean hasNext() {
return current!=null;
}
public T next() {
if(!hasNext()){
throw new NoSuchElementException();
}
T result = current.getElement();
current = current.getNext();
return result;
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public void setFirst(PublicNode<T> first) {
this.first = first;
}
public PublicNode<T> getFirst() {
return first;
}
}
And
public class CircularLinkedList<T> implements CircularList<T> {
private PublicNode<T> head;
private PublicNode<T> tail;
private int size;
public CircularLinkedList() {
head = null;
tail = null;
size = 0;
}
//bigO(1)
public PublicNode<T> getHead() {
return head;
}
//bigO(1)
public void setHead(PublicNode<T> head) {
this.head = head;
}
//bigO(1)
public PublicNode<T> getTail() {
return tail;
}
//bigO(1)
public void setTail(PublicNode<T> tail) {
this.tail = tail;
}
//bigO(1)
public int getSize() {
return size;
}
//bigO(1)
public void setSize(int count) {
this.size = count;
}
//bigO(1)
public boolean isEmpty() {
return tail == null || head == null;
}
// add element to the end of the list
public void addLast(T element) {
PublicNode<T> node = new PublicNode<T>(element);
if(this.tail==null){
node.setNext(null);
node.setPrevious(null);
this.tail=node;
}else{
PublicNode<T> oldTail = this.tail;
oldTail.setNext(node);
node.setNext(head);
node.setPrevious(oldTail);
this.tail =node;
}if(this.head==null){
this.head=node;
}
this.size++;
}
// set element to be front of the list
//bigO(n)
public void setFront(T element) {
PublicNode<T> node = new PublicNode<T>(element);
if (isEmpty()) {
throw new NoSuchElementException();
}else{
PublicNode<T> oldHead = this.head;
oldHead.setPrevious(node);
node.setNext(oldHead);
node.setPrevious(null);
this.head=node;
}
if(this.tail==null){
this.tail=node;
}
this.size++;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CircularList<Integer> list = new CircularLinkedList<Integer>();
for (int i = 1; i <= 4; i++) {
list.addLast(i);
}
System.out.println("\nShould print 1 2 3 4 1 2 3 4 1 2 3");
Iterator<Integer> iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
System.out.println();
list.setFront(3);
System.out.println("Should print 3 4 1 2 3 4 1 2 3 4 1");
iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
System.out.println();
list.addLast(-1);
System.out.println("Should print 3 4 1 2 -1 3 4 1 2 -1 3");
iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
System.out.println();
list.remove(4);
System.out.println("Should print 3 1 2 -1 3 1 2 -1 3 1 2");
iter = list.iterator();
for (int i = 1; iter.hasNext() && i <= 11; i++) {
System.out.print(" " + iter.next());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
setFront()
的else
中尝试类似的操作try something like this in the
else
fromsetFront()
在 CircularLinkedList.setFront(T) 中,您将创建一个新节点并将其添加到循环列表的前面,而不是查找具有给定值的节点并修改 CircularLinkedList
CircularLinkedList.setFront(T) 。 T>.head
引用找到的节点。In
CircularLinkedList<T>.setFront(T)
you're creating a new node and adding that to the front of the circular list rather than finding the node with the given value and modifyingCircularLinkedList<T>.head
to reference the found node.