------------ public class IntLinkList { private IntNode top; //The reference to
ID: 3918246 • Letter: #
Question
------------
public class IntLinkList {
private IntNode top; //The reference to the first Node
//=========== Solution code =============================
public IntLinkList(int[] data){
for (int i = 0; i<data.length; i++){
add(data[i]);
}
}
public boolean empty(){
if (top == null)
return true;
return false;
}
public int first(){
return top.getData();
}
public void removeFirst(){
if (top != null)
top = top.getLink();
}
public IntLinkList clone(){
IntLinkList a = new IntLinkList();
if (top != null){
IntNode p = top;
while (p != null){
a.add(p.getData());
p = p.getLink();
}
}
return a;
}
private static boolean equals(IntNode top1, IntNode top2){
//Your code here
return false; //Dummy statement for testing - remove it.
}
//=========== Supplied code =============================
public IntLinkList() {
//A constructor that creates an empty list.
top = null;
}
public void add(int newItem) {
//Add the newItem at the FRONT of the list.
top = new IntNode(newItem,top);
}//add
public String toString() {
String answer = "<<";
IntNode next = top;
while(next!=null){
answer += next.getData()+" ";
next = next.getLink();
}
return answer+">>";
}
public void ordInsert(int newItem) {
//Add the newItem so that the list remains sorted into
//ascending order. This will not work unless the list
//is currently in ascending order.
IntNode prev = null;
IntNode next = top;
while(next!=null && next.getData()<newItem){
prev = next;
next = next.getLink();
}
//This item belongs between prev and next
IntNode newNode = new IntNode(newItem,next);
if(prev==null)
top = newNode;
else
prev.setLink(newNode);
}
public boolean equals(IntLinkList other){
return equals(top,other.top);
}
}
Explanation / Answer
class IntNode{
int data;
IntNode link;
public IntNode(){
link=null;
}
public IntNode(int val ,IntNode n) {
IntNode newnode=new IntNode();
newnode.data=val;
newnode.link=n;
}
public void setLink(IntNode n) {
link=n;
}
public int getData() {
return data;
}
public IntNode getLink() {
return link;
}
}
public class IntLinklist {
private IntNode top;
public IntLinklist(int[] data){
for (int i = 0; i<data.length; i++){
add(data[i]);
}
}
public boolean empty(){
if (top == null)
return true;
return false;
}
public int first(){
return top.getData();
}
public void removeFirst(){
if (top != null)
top = top.getLink();
}
public IntLinklist clone(){
IntLinklist a = new IntLinklist();
ikf (top != null){
IntNode p = top;
while (p != null){
a.add(p.getData());
p = p.getLink();
}
}
return a;
}
private static boolean equals(IntNode top1, IntNode top2){
if(top1==null&&top2==null)return true;
if(top1==null||top2==null)return false;
if(top1.data==top2.data)equals(top1.link,top2.link);
return false; //Dummy statement for testing - remove it.
}
//=========== Supplied code =============================
public IntLinklist() {
//A constructor that creates an empty list.
top = null;
}
public void add(int newItem) {
//Add the newItem at the FRONT of the list.
top = new IntNode(newItem,top);
}//add
public String toString() {
String answer = "<<";
IntNode next = top;
while(next!=null){
answer += next.getData()+" ";
next = next.getLink();
}
return answer+">>";
}
public void ordInsert(int newItem) {
//Add the newItem so that the list remains sorted into
//ascending order. This will not work unless the list
//is currently in ascending order.
IntNode prev = null;
IntNode next = top;
while(next!=null && next.getData()<newItem){
prev = next;
next = next.getLink();
}
//This item belongs between prev and next
IntNode newNode = new IntNode(newItem,next);
if(prev==null)
top = newNode;
else
prev.setLink(newNode);
}
public boolean equals(IntLinklist other){
return equals(top,other.top);
}
public void insort(IntNode a) {
if(a==null)return;
ordInsert(a.data);
insort(a.link);
}
}
for any query please comment.
please upvote if find it helpful.
Thank you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.