setting up linked list Java
Posted
by
erp
on Stack Overflow
See other posts from Stack Overflow
or by erp
Published on 2012-11-05T04:46:51Z
Indexed on
2012/11/05
5:00 UTC
Read the original article
Hit count: 138
java
|linked-list
I'm working on some basic linked list stuff, like insert, delete, go to the front or end of the list, and basically i understand the concept of all of that stuff once i have the list i guess but im having trouble setting up the list. I was wondering of you guys could tell me if im going in the right direction. (mostly just the setup) this is what i have so far:
public class List {
private int size;
private List linkedList;
List head;
List cur;
List next;
/**
* Creates an empty list.
* @pre
* @post
*/
public List(){
linkedList = new List();
this.head = null;
cur = head;
}
/**
* Delete the current element from this list. The element after the deleted element becomes the new current.
* If that's not possible, then the element before the deleted element becomes the new current.
* If that is also not possible, then you need to recognize what state the list is in and define current accordingly.
* Nothing should be done if a delete is not possible.
* @pre
* @post
*/
public void delete(){
// delete
size--;
}
/**
* Get the value of the current element. If this is not possible, throw an IllegalArgumentException.
* @pre the list is not empty
* @post
* @return value of the current element.
*/
public char get(){
return getItem(cur);
}
/**
* Go to the last element of the list. If this is not possible, don't change the cursor.
* @pre
* @post
*/
public void goLast(){
while (cur.next != null){
cur = cur.next;
}
}
/**
* Advance the cursor to the next element. If this is not possible, don't change the cursor.
* @pre
* @post
*/
public void goNext(){
if(cur.next != null){
cur = cur.next;}
//else do nothing
}
/**
* Retreat the cursor to the previous element. If this is not possible, don't change the cursor.
* @pre
* @post
*/
public void goPrev(){
}
/**
* Go to top of the list. This is the position before the first element.
* @pre
* @post
*/
public void goTop(){
}
/**
* Go to first element of the list. If this is not possible, don't change the cursor.
* @pre
* @post
*/
public void goFirst(){
}
/**
* Insert the given parameter after the current element. The newly inserted element becomes the current element.
* @pre
* @post
* @param newVal : value to insert after the current element.
*/
public void insert(char newVal){
cur.setItem(newVal);
size++;
}
/**
* Determines if this list is empty. Empty means this list has no elements.
* @pre
* @post
* @return true if the list is empty.
*/
public boolean isEmpty(){
return head == null;
}
/**
* Determines the size of the list. The size of the list is the number of elements in the list.
* @pre
* @post
* @return size which is the number of elements in the list.
*/
public int size(){
return size;
}
public class Node {
private char item;
private Node next;
public Node() {
}
public Node(char item) {
this.item = item;
}
public Node(char item, Node next) {
this.item = item;
this.next = next;
}
public char getItem() {
return this.item;
}
public void setItem(char item) {
this.item = item;
}
public Node getNext() {
return this.next;
}
public void setNext(Node next) {
this.next = next;
}
}
}
I got the node class alright (well i think it works alright), but is it necessary to even have that class? or can i go about it without even using it (just curious). And for example on the method get() in the list class can i not call that getItem() method from the node class because it's getting an error even though i thought that was the whole point for the node class.
bottom line i just wanna make sure im setting up the list right.
Thanks for any help guys, im new to linked list's so bear with me!
© Stack Overflow or respective owner