I'm writing this code to implement a stack as a linked list but I keep getting an error when I compile this part of the code and can't figure out why
I definitely know that it shouldn't be giving me this error since there are enough brackets and none of them are out of place I hope
The Error:
C:\Users\Michelle\Desktop\ITB\Semester 5\Data Structures & Algorithms\Assignment\ListReference.java:108: error: reached end of file while parsing
}
^
1 error
My Code:
public class ListReference implements StackInterface {
private Node top;
//private Object item;
private int NumItems;
public ListReference() {
NumItems = 0;
top = new Node(null);
}
public boolean isEmpty() {
return NumItems == 0;
}
public int size() {
return NumItems;
}
/*public Node find(int index) {
Node curr = tail;
for(int skip = 1; skip < index; skip++) {
curr.getNext();
}
return curr;
}*/
public Object get(int index) {
if(index <= 0)
return null;
Node ListReference = top.getNext();
for(int i=1; i < index; i++) {
if (ListReference.getNext() == null)
return null;
ListReference = ListReference.getData();
}
}
public void add(int index, Object item) {
Node ListReferenceTemp = new Node(item);
Node ListRefCurr = top;
while(ListRefCurr.getNext() != null) {
ListRefCurr = ListRefCurr.getNext();
}
ListRefCurr.setNext(ListReferenceTemp);
NumItems++;
}
public void add(Object data) {
Node ListReferenceTemp = new Node(item);
Node ListRefernce = top;
for(int i=1; i < index && ListReference.getNext() != null; i++) {
ListReference = ListReference.getNext();
}
ListReferenceTemp.setNext(ListReference.getNext());
ListReference.setNext(ListReferenceTemp);
NumItems++;
}
// removes the element at the specified position in this list.
public boolean remove(int index) {
// if the index is out of range, exit
if (index < 1 || index > size())
return false;
Node ListRefCurr = top;
for (int i = 1; i < index; i++) {
if (ListRefCurr.getNext() == null) {
return false;
}
ListRefCurr = ListRefCurr.getNext();
}
ListRefCurr.setNext(ListRefCurr.getNext().getNext());
NumItems--; // decrement the number of elements variable
return true;
}
public String toString() {
Node ListReference = top.getNext();
String output = "";
while (ListREference != null) {
output += "[" + ListRefCurr.getData().toString() + "]";
ListRefCurr = ListRefCurr.getNext();
}
return output;
}
Related
public class ourStack1 {
private int elements[];
private int index; // indicate the next position to put a new data
private int size;
public ourStack1() {
elements = new int[10];
index = 0;
size = 0;
}
public void push(int value) {
if(size == 10) {
System.out.println("Stack is full, no push");
return;
}
elements[index] = value;
++index;
++size;
}
public int pop() {
if(size == 0) {
System.out.println("Stack is empty, no pop");
return -1;
}
int temp = elements[index - 1];
--index;
--size;
return temp;
}
public int peek() {
if(size == 0) {
System.out.println("Stack is empty, no peek");
return -1;
}
return elements[index - 1];
}
/*
public int mySize() {
// you know how to do this
}
*/
public static void main(String[] args) {
ourStack1 x = new ourStack1();
for(int i = 0; i < 10; ++i)
x.push(i);
for(int i = 0; i < 10; ++i)
System.out.println(x.pop());
}
}
I'm confused on how to overwrite the last element added to the full stack. I want to add element to replace the last element while not exceeding the array size[10]
public void replaceLast(int value) {
if (this.size > 0) {
this.pop();
}
this.push(value);
}
I'm trying to sum up items in a node both iteratively and recursively. I already wrote the program to do the iterative way and I'm having problems on how this can be done recursively.
code:
public int sumIterative() {
Node newNode= new Node(item, next);
int sum = 0;
while(newNode != null){
sum = sum + item;
newNode = newNode.next;
}
return sum;
}
My effort on making the recursive way:
public int sumRecursive() {
Node newNode = new Node(item,next);
int sum = 0;
int result;
if(newNode == null){
result = 0;
} else {
return sumRecursive();
}
return sum;
}
I am trying to make this with the logic of "there are no nodes after this"
How is it possible for this to be done recursively?
Edit
This is the public driver method, I can post my whole code on demand
public int sumRecursive() {
int sum = 0;
if (top != null) {
sum = top.sumRecursive();
}
return sum;
}
class LinkedList {
private class Node {
public int item;
public Node next;
public Node(int newItem, Node newNext) {
item = newItem;
next = newNext;
}
public int getItem() {
return item;
}
public Node getNext() {
return next;
}
public void setNext(Node newNext) {
next = newNext;
}
// My Other Node methods
// Recursively add up the numbers stored in this
// and all the nodes after this.
// Base case: there are no nodes after this.
public int sumRecursive() {
Node node = new Node(item,next);
/*Node newNode = new Node(item,next);
int sum = 0;
if (null == newNode) {
return sum;
} else {
sum += sumRecursive(newNode.next);
}*/
if (node == null) {
return node.item;
} else {
return node.item + sumRecursive(node.next);
}
}
// Iteratively add up the numbers stored in this
// and all the nodes after this.
public int sumIterative() {
Node newNode= new Node(item, next);
int sum = 0;
while(newNode != null) {
sum = sum + item;
newNode = newNode.next;
}
return sum;
}
} // end class Node
private Node top; // a pointer to the first node in the list (when it's not empty)
public LinkedList() {
top = null; // empty list
}
// Insert a node at the front of the list
public void insertAtFront(int newItem) {
top = new Node(newItem, top);
}
// Print out the list (10 numbers per line)
public void printList() {
int count = 1;
Node curr;
if (top == null) {
System.out.println( "List is empty" );
} else {
curr = top;
while (curr != null) {
System.out.print( " " + curr.item );
count++;
if (count % 10 == 0) {
System.out.println();
}
curr = curr.next;
}
if (count % 10 != 0) {
System.out.println();
}
}
}
// public driver method for sumRecursive
public int sumRecursive() {
int sum = 0;
if (top != null) {
sum = top.sumRecursive();
}
return sum;
}
// public driver method for sumIterative
public int sumIterative() {
int sum = 0;
if (top != null) {
sum = top.sumIterative();
}
return sum;
}
}
public int sumRecursive() {
return sumRecursive(top);
}
public int sumRecursive(Node node){
if (node == null) {
return 0;
}
return node.item + sumRecursive(node.next);
}
// or to match your driver function
public int sumRecursive() {
if (top == null) {
return 0;
} else {
return top.sumRecursive();
}
}
// in Node class
public int sumRecursive(){
int sum = this.item;
if (this.next != null) {
sum += this.next.sumRecursive();
}
return sum;
}
public int sumRecursive(Node node){
if(node == null){
return 0;
}else{
return node.item + sumRecursive(node.next);
}
}
On my count method, i am trying to compare the object being sent with the object calling it, and I have no idea why i can not cal o.get(i) to get the value for Object o being passed. It tells me the method cannot be found even though i can call get(i) normally. Any ideas how i can get the value for the object getting passed so i can compare them?
public class GenericLinkedList<E> implements List<E> {
private class Node<E>{
private E data;
private Node<E> next;
}
private E data;
private Node<E> head = null;
private int size = 0;
private Node<E> nodeAt(int index){
Node<E> curNode = head;
int curIndex = 0;
while(curIndex < index){
curNode = curNode.next;
curIndex++;
}
return curNode;
}
#Override
public E get(int index) {
return nodeAt(index).data;
}
#Override
public void add(E value) {
Node<E> node = new Node<E>();
node.data = value;
if(size == 0){
head = node;
}else{
Node<E> curNode = nodeAt(size - 1);
curNode.next = node;
}
size++;
}
public void add(int index, E value){
Node<E> node = new Node<E>();
node.data = value;
if(size == 0){
head = node;
}else if(index == 0){
node.next = head;
head = node;
}else{
Node<E> curNode = nodeAt(index - 1);
node.next = curNode.next;
curNode.next = node;
}
size++;
}
#Override
public void remove(int index) {
if(index == 0){
head = head.next;
}else{
Node<E> curNode = nodeAt(index - 1);
curNode.next = curNode.next.next;
}
size--;
}
#Override
public void set(int index, E value) {
nodeAt(index).data = value;
}
public String toString(){
String s = "";
Node<E> curNode = head;
s += curNode.data +" -> ";
while(curNode.next != null){
curNode = curNode.next;
s += curNode.data +" -> ";
}
s += "null";
return s;
}
public void clear() {
for (int i = 0; i < size; i++)
set(i, null);
size = 0;
}
#Override
public void removeAll(E o)//Clears out the array object by setting everything to null
{
for(int i = 0; i < this.size; i++)
{
}
}
#Override
public int count(Object o)
{
System.out.println(o);
int count = 0;
for(int i = 0; i< size;i++)
{
if(get(i) == o.get(i))
{
count++;
}
}
return count;
/*int count = 0;
for(int i = 0;i<this.size;i++)
{
for(int j = 0;i<size;j++)
{
}
}
return count;*/
}
public void reverse()
{
int x = size;
for(int i = 0; i < this.size; i++)
{
E temp = this.get(x);
this.set(x, get(i));
this.set(i, temp);
x++;
}
}
public Object subList(int beginIndex, int endIndex)
{
int j = 0;
GenericLinkedList<E> LinkedList = new GenericLinkedList<E>();
if(beginIndex == endIndex)
return LinkedList;
else if(beginIndex > endIndex||endIndex>size||beginIndex<0)
{
return null;
}
else
{
for(int i = beginIndex; i <= endIndex;i++)
{
LinkedList.add(get(i));
j++;
}
}
return LinkedList;
}
public static void main(String[] args) {
GenericLinkedList<Integer> myList = new GenericLinkedList<Integer>();
myList.add(4);
myList.add(7);
myList.add(123123);
System.out.println(myList.get(2));
GenericLinkedList<Integer> myList2 = new GenericLinkedList<Integer>();
myList2.add(4);
myList2.add(7);
myList2.add(8);
myList2.add(3,999);
System.out.println(myList2);
System.out.println(myList.count(myList2));
//System.out.println(myList2);
// myList2.clear();//Testing our clear class
// System.out.println(myList2);
//System.out.println(myList2.subList(1, 3));//Testing our clear class
// System.out.println(myList2);
}
}
You have a problem when trying to call o.get() because it is an object of the base type Object which has no method get defined. To get the proper result you have two possible solutions....
The short solution is to type cast.
((GenericLinkedList<Object>)o).get(i)
Or a more easily readable solution is to change the signature of your count method:
public int count(GenericLinkList<E> list) {
...
}
Change your count method to public int GenericLinkedList(Object o)
If you use Object as the type of the parameter, you can only call the methods which are available on Object.
Here is my code, but I keep getting the "Note: LinkedListAdd.java uses unchecked or unsafe operations." I'm trying to do what I can to get rid of it, but nothing seems to be working. The base class compiles fine and has no warning, and the check seems to be coming from the add all method. Any ideas?
public class LinkedListAdd<E extends Comparable <E> > extends LinkedList<E>
{
public LinkedListAdd()
{
super();
}
public <E extends Comparable<E> > boolean addAll(final int index, final E[] array)
{
if(index < 0 || index > this.size())
throw new IndexOutOfBoundsException("Index out of bounds");
if(array == null)
throw new NullPointerException("Null array");
Node nn = null;
for(int y = array.length - 1; y >= 0; y--)
{
nn = new Node(array[y], null);
if(this.size() == 0)
{
this.head.next = nn;
this.size += 1;
}
else
{
Node cur = this.head.next, prev = null;
for(int x = 0; x < index; x++)
{
prev = cur;
cur = cur.next;
}
if(prev == null)
{
nn.next = cur;
this.head.next = nn;
this.size += 1;
}
else
{
prev.next = nn;
nn.next = cur;
this.size += 1;
}
}
}
if(nn == null)
return false;
return true;
}
}//end class LinkedListSort
Here is the base class
public class LinkedList<E extends Comparable <E> >
{
protected static class Node<E extends Comparable <E> >
{
public E data;
public Node<E> next;
public Node()
{
this.next = null;
this.data = null;
}// end DVC
public Node(final E data, final Node<E> next)
{
this.next = next;
this.data = data;
}// end EVC
}// end class Node
protected Node <E> head;
protected int size;
public LinkedList()
{
this.head = new Node<>();
this.size = 0;
}
public void clear()
{
this.head = null;
this.size = 0;
}// end clear
public int size(){return this.size;}
public void addFirst(final E data)
{
this.head.next = new Node<>(data, this.head.next);
size ++;
}// end
public String toString()
{
String temp = "List size: " + this.size;
if(this.head.next == null)
return temp += "\nEmpty List";
else
{
temp += "\n";
Node<E> cur = head.next;
while(cur != null)
{
temp += cur.data + " ";
cur = cur.next;
}
return temp;
}// end else
}// end toString
}// end class
There's a couple of things I'd recommend doing; firstly I'd add methods for get/set on your head/next/size/data and make those fields private
With regards to your generics;
the method signature should be
public boolean addAll(final int index, final E[] array)
and your declarations for nodes in that method need to be
Node<E> node
I must create Stack by using Linked List. Here is my LinkedList class:
package stack;
import java.util.EmptyStackException;
public class List {
Element head;
int size = 0;
public void Add(Object value) {
if (size == 0) {
head = new Element(value);
size++;
return;
}
Insert(size, value);
}
public void Insert(int index, Object value) {
if (index > size) {
throw new IndexOutOfBoundsException("Bad index");
}
Element tmp = head;
for (int i = 0; i < index -1 ; i++) {
tmp = tmp.next;
}
Element newElement = new Element(value);
newElement.next = tmp.next;
tmp.next = newElement;
size++;
}
public int Size() {
return size;
}
public Object Get(int index) {
if (index > size - 1) {
throw new IndexOutOfBoundsException("Index doesn't exist");
}
Element tmp = head;
for (int i = 0; i < index; i++) {
tmp = tmp.next;
}
System.out.println(tmp.value.toString());
return tmp;
}
public Object Set(int index, Object value) {
if (index > size - 1) {
throw new IndexOutOfBoundsException("Index doesn't exist");
}
Element tmp = head;
for (int i = 0; i < index; i++) {
tmp = tmp.next;
}
Element returnValue = new Element(value);
returnValue = tmp;
tmp.value = value;
return returnValue;
}
public Object Delete(int index) {
Element tmp;
tmp = head;
for (int i = 0; i < index - 1; i++) {
tmp = tmp.next;
}
Element returnValue;
returnValue = tmp;
head = tmp.next;
size--;
return returnValue;
}
public boolean Contains(Object value) {
Element tmp = head;
for (int i = 0; i < size - 1; i++) {
tmp = tmp.next;
if (tmp.value == value) {
System.out.println("true");
return true;
}
}
System.out.println("False");
return false;
}
public boolean IsEmpty() {
return Size() > 0;
}
public void Clear() {
size = 0;
}
public void Display() throws EmptyStackException {
if (size <= 0) {
throw new EmptyStackException();
}
Element tmp = head;
for (int i = 0; i < size; i++) {
System.out.println(tmp.value.toString());
tmp = tmp.next;
}
}
}
and i got Stack interface
public interface IStack {
public interface Stack {
public void push(Object value); // odłóż na stos
public Object pop() throws EmptyStackException; //pobierz ze stosu
public Object peek() throws EmptyStackException; //odczytaj ze stosu,
// and some others
Any1 have am idea how to create pop method?
LinkedList is already a stack, it has pop, push and peek methods. Just create an adapter class and use those methods