Question

117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a link

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IntLinkedList.java

class IntNode

{

protected int data;

protected IntNode link;

/* Constructor */

public IntNode()

{

link = null;

data = 0;

}   

/* Constructor */

public IntNode(int d,IntNode n)

{

data = d;

link = n;

}   

/* Function to set link to next IntNode */

public void setLink(IntNode n)

{

link = n;

}   

/* Function to set data to current IntNode */

public void setData(int d)

{

data = d;

}   

/* Function to get link to next node */

public IntNode getLink()

{

return link;

}   

/* Function to get data from current IntNode */

public int getData()

{

return data;

}

}

public class intLinkedList

{

protected IntNode start;

protected IntNode end ;

public int size ;

  

public intLinkedList(int arr[])   

{

for(int i=0;i<arr.length;i++)

{

IntNode nptr = new IntNode(arr[i], null);   

size++ ;   

if(start == null)

{

start = nptr;

end = start;

}

else

{

end.setLink(nptr);

end = nptr;

}

}

}

public intLinkedList() {

start=null;

end=null;

size=0;

}

public int size()

{

return size;

}   

public boolean containts(int data)

{

IntNode head=start;

while(head!=null)

{

if(head.getData()==data)

return true;

head=head.getLink();

}

return false;

}

public void removeFirst()

{

IntNode temp;

temp=start;

temp=temp.getLink();

start=temp;

size--;

}

@Override

public String toString()

{

String str="";

str+="<<";

IntNode ptr = start;

str+=ptr.getData()+",";

ptr = ptr.getLink();

while (ptr.getLink() != null)

{

str+=ptr.getData()+ ",";

ptr = ptr.getLink();

}

str+=ptr.getData()+">>";

return str;

}

public intLinkedList clone()

{

intLinkedList newList=new intLinkedList();

IntNode head=this.start;

while(head!=null)

{

IntNode nptr = new IntNode(head.getData(), null);   

if(newList.start == null)

{

newList.start = nptr;

newList.end = newList.start;

}

else

{

newList.end.setLink(nptr);

newList.end = nptr;

}

head=head.getLink();

}

newList.size=this.size;

return newList;

}

}

main,java

public class main {

public static void main(String[] args)

{

int []arr={1,2,3,4};

intLinkedList list1=new intLinkedList(arr);

System.out.println(list1);

System.out.println("After removing the fisrt element.");

list1.removeFirst();

System.out.println(list1);

intLinkedList list2=new intLinkedList();

list2=list1.clone();

System.out.println("List 2 that is clone");

System.out.println(list2);

System.out.println("2 is preset in the list 2: "+list2.containts(2));

}

  

}

output

Linnked list of int- NetBeans IDE 8.0.1 Search (Ctrl+1) Eile Edit View Navigete Source Refgctor Run Debug Profile Team IoolsIf you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • Using an appropriate definition of ListNode, design a simple linked list class called StringList with the...

    Using an appropriate definition of ListNode, design a simple linked list class called StringList with the following member functions: void add (std::string); int positionOf (std::string); bool setNodeVal(int, std::string); std::vector<std::string> getAsVector(); a default constructor a copy constructor a destructor The add() function adds a new node containing the value of the parameter to the end of the list. The positionOf() function returns the (zero-based) position in the list for the first occurrence of the parameter in the list, or -1 if...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • Implement an application based on a singly linked list that maintains a list of the top...

    Implement an application based on a singly linked list that maintains a list of the top five performers in a video game. An entry on the list consists of a name and a score (you can make this into a blueprint class if you like), and the list must be maintained in descending order of scores. Here is an example of such a list when it only has three elements. ​Spike​120 ​Whiz​105 ​G-man​ 99 Use a class based on singly...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT