- #1
grouchy
- 73
- 0
Hi, i need to use a list class (provided and complete) and then use a Linkedlist class (need to add the methods) to get a stack going. I am having trouble with my remove from head method although I ain't too sure if my addHead method is right... I'd appreciate it if someone could look over my code and give me some pointers. I wrote in bold and caps where I aint sure. thx.
class Link
{
public Object data;
public Link next;
public Link(Object d, Link n)
{
data = d; next = n;
}
}
//LINKEDLIST CLASS AND MAIN
import java.util.Scanner;
class LinkList
{
private Link head; // reference to first Link
private Link tail; // reference to last Link
private int size;
public LinkList()
{
tail = null;
head = null;
}
public Object peekHead() // return reference to first Object
{
return head.data;
}
public Object peekTail() // return reference to last Object
{
return tail.data;
}
//THE ADD METHOD, NOT SURE IF DONE RIGHT
public void addHead(Object newData)
{
if (head == null)
{
head = new Link(newData, tail);
}
else if (head != null && tail == null)
{
tail = head;
head = new Link (newData, tail);
}
else
{
head.next = head;
head = new Link (newData, head.next);
}
}
public void addTail(Object newData)
{
}
//THE REMOVE METHOD
public Object removeHead()
{
Link removing = head;
if (head != null)
{
head = head.next;
}
return removing.data;
}
//MAIN METHOD
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int choice = 0;
LinkList first = new LinkList();
while (choice != 6)
{
System.out.println("What would you like to do? (enter the number)");
System.out.println("1 - Push onto Head of Stack.");
System.out.println("2 - Remove from Head of Stack.");
System.out.println("3 - Peek at Head of Stack.");
System.out.println("4 - Push at Tail of Stack.");
System.out.println("5 - Peak at Tail of Stack.");
System.out.println("6 - Close Program.");
choice = scan.nextInt();
switch(choice)
{
case 1:
System.out.println("What do you want to push on Head?");
Object pushingHead = scan.next();
first.addHead(pushingHead);
break;
case 2:
System.out.println("Removing: " + first.removeHead());
break;
case 3:
System.out.println("Peeking at Head of Stack: " + first.peekHead());
break;
case 4:
System.out.println("What do you want to push on Tail?");
Object pushingTail = scan.next();
first.addHead(pushingTail);
break;
case 5:
System.out.println("Peeking at Tail of Stack: " + first.peekTail());
break;
case 6:
System.out.println("Good Bye!");
break;
}
}
}
}
class Link
{
public Object data;
public Link next;
public Link(Object d, Link n)
{
data = d; next = n;
}
}
//LINKEDLIST CLASS AND MAIN
import java.util.Scanner;
class LinkList
{
private Link head; // reference to first Link
private Link tail; // reference to last Link
private int size;
public LinkList()
{
tail = null;
head = null;
}
public Object peekHead() // return reference to first Object
{
return head.data;
}
public Object peekTail() // return reference to last Object
{
return tail.data;
}
//THE ADD METHOD, NOT SURE IF DONE RIGHT
public void addHead(Object newData)
{
if (head == null)
{
head = new Link(newData, tail);
}
else if (head != null && tail == null)
{
tail = head;
head = new Link (newData, tail);
}
else
{
head.next = head;
head = new Link (newData, head.next);
}
}
public void addTail(Object newData)
{
}
//THE REMOVE METHOD
public Object removeHead()
{
Link removing = head;
if (head != null)
{
head = head.next;
}
return removing.data;
}
//MAIN METHOD
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int choice = 0;
LinkList first = new LinkList();
while (choice != 6)
{
System.out.println("What would you like to do? (enter the number)");
System.out.println("1 - Push onto Head of Stack.");
System.out.println("2 - Remove from Head of Stack.");
System.out.println("3 - Peek at Head of Stack.");
System.out.println("4 - Push at Tail of Stack.");
System.out.println("5 - Peak at Tail of Stack.");
System.out.println("6 - Close Program.");
choice = scan.nextInt();
switch(choice)
{
case 1:
System.out.println("What do you want to push on Head?");
Object pushingHead = scan.next();
first.addHead(pushingHead);
break;
case 2:
System.out.println("Removing: " + first.removeHead());
break;
case 3:
System.out.println("Peeking at Head of Stack: " + first.peekHead());
break;
case 4:
System.out.println("What do you want to push on Tail?");
Object pushingTail = scan.next();
first.addHead(pushingTail);
break;
case 5:
System.out.println("Peeking at Tail of Stack: " + first.peekTail());
break;
case 6:
System.out.println("Good Bye!");
break;
}
}
}
}