Question

In: Computer Science

Write an application and perform the following:(NO USER INPUT) -Create at least three classes such as:...

Write an application and perform the following:(NO USER INPUT)

-Create at least three classes such as:

1. listnode- for data and link, constructors

2. Singlelinkedlist-for method definition

3. linkedlist-for objects

-Insert a node at tail/end in a linked list.

-and display all the nodes in the list.

-Delete a node at a position in the list

Note: Add these methods in the same application which we have done in the class.

this is what we've done in the class:

class listnode{
int data;//first part of the node(data)
listnode link;//second part of the node(address)
listnode()
{
data=0;
link=null;
}
listnode(int d,listnode l)//10|null
{
data=d;
link=l;
}
}
class singlelinklist{
void display(listnodes head){
listnodes current=head;
while(current.link!=null){
System.out.print(current.data+"-->");
current=current.link;
}
System.out.print(current.data);
  
}
public listnodes insert(listnodes head,int data)
{
//create new node
listnodes newnode=new listnodes(data,null);
//link the newnode to the head node
newnode.link=head;
//make newnode the first node
head=newnode;
return head;//return the first node
}
//insert at a position(after or before a node)
public listnodes InsertAtPostion(listnodes head,int data,int position){
listnodes newnode=new listnodes(data,null);   
listnodes previous=head;
int count=1;
while(count<=position-1){//(count<position)
previous=previous.link;
count++;
}
// listnodess current=previous.link;
listnodes current=previous;
current=previous.link;
newnode.link=current;
previous.link=newnode;
return head;


}
public listnodes deletefirst(listnodes head){
listnodes temp=head;//rename
head=head.link;//move head
temp.link=null;//temp alone
return temp;
}
public listnodes deletelast(listnodes head){

if(head==null){
return head;
}
listnodes last=head;
listnodes previoustolast=head;
while(last.link!=null){
previoustolast=last;
last=last.link;
}
previoustolast.link=null;
return last;
}
public int length(listnodes head){
listnodes curr=head;
int c=0;
while(curr!=null){
c++;
curr=curr.link;
  
}
return c;
}
public boolean find(listnodes head,int searchkey){
listnodes curr=head;
while(curr!=null){
if(curr.data==searchkey)
{
return true;
}
curr=curr.link;
}
return false;
}
}

public class Link {
public static void main(String[] args) {
//craete first node
listnodes head=new listnodes(10,null);
//create an object of class where all the methods are   
singlelinklist sl=new singlelinklist();
//insert at postion
sl.InsertAtPostion(head, 30, 1);

//insert at front
listnodes newhead=sl.insert(head,20);
sl.display(newhead);

//delete last
System.out.println(" ");
System.out.println("Delete a node at end");
listnodes l=sl.deletelast(head);
sl.display(l);
System.out.println(" ");
//delete first
System.out.println("\nDelete a node at begining and return head: \n");
listnodes first=sl.deletefirst(head);
sl.display(first);
System.out.println(" \n");
//find length
System.out.println("length is="+sl.length(head));
System.out.println(" ");
//Search a node
System.out.println("Search for a node");
if(sl.find(head, 10)){
System.out.println("key found");}
else
System.out.println("key not found");
}


}

Solutions

Expert Solution

    public ListNode insertLast(ListNode head, int data) {
        ListNode newnode = new ListNode(data, null);
        if (head == null) {
            return head;
        }
        ListNode last = head;
        //traverse till the end.
        while (last.link != null) {
            last = last.link;
        }
        //point last node to new node.
        last.link = newnode;
        //return head
        return head;
    }

    public void displayAllNodes(ListNode head) {
        ListNode current = head;
        while (current != null) {
            //print node and point current to next.
            System.out.println(current.data);
            current = current.link;
        }
    }

    //delete at a position(after or before a node)
    public ListNode deleteAtPosition(ListNode head, int position) {
        int currentPosition = 1;
        ListNode current = head;
        ListNode prev = head;
        //move current to node to be deleted.
        while (currentPosition < position) {
            prev = current;
            current = current.link;
            currentPosition++;
        }
        //point previous link to current link. Now prev next is current next, meaning current is deleted.
        prev.link = current.link;
        return head;
    }

Main function :

public static void main(String[] args) {
        System.out.println("Creating a new list with node value 3081");
        ListNode newHead = new ListNode(3081, null);
        SingleLinkList sll = new SingleLinkList();
        System.out.println("Inserting 3 numbers one by one in the end.");
        sll.insertLast(newHead, 3082);
        sll.insertLast(newHead, 3083);
        sll.insertLast(newHead, 3084);
        System.out.println("Displaying all numbers after 3 insertions.");
        sll.displayAllNodes(newHead);
        sll.deleteAtPosition(newHead, 2);
        System.out.println("After deleting node at position 2 :");
        sll.displayAllNodes(newHead);
    }

Related Solutions

Write application in C# that enables a user to: Use Methods for user input and calculations...
Write application in C# that enables a user to: Use Methods for user input and calculations input the grade and number of credit hours for any number of courses. Calculate the GPA on a 4.0 scale using those values. Grade point average (GPA) is calculated by dividing the total amount of grade points earned, sometimes referred to as quality points, by the total number of credit hours attempted. For each hour, an A receives 4 grade or quality points, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write an application that includes a constructor, user input, and operators.
Create a simple shopping cart application. Ask the user to input an item number and quantity....
Create a simple shopping cart application. Ask the user to input an item number and quantity. Update the shopping cart. Display the updated cart. Include the following when displaying cart: item number, name, quantity and total price. Ask for another update or end the program. Design Requires 2 classes: the main class and the cartItem class In the main class: Need one array named cartArray of type cartItem. Size = ? Need a loop that asks the user for input....
Create a simple shopping cart application. Ask the user to input an item number and quantity....
Create a simple shopping cart application. Ask the user to input an item number and quantity. Update the shopping cart. Display the updated cart. Include the following when displaying cart: item number, name, quantity and total price. Ask for another update or end the program. Design Requires 2 classes: the main class and the cartItem class In the main class: Need one array named cartArray of type cartItem. Size = ? Need a loop that asks the user for input....
Using Java create an interactive (the user should be able to input the values) Java Application to process your utility bill for at-least 6 months.
Java ProgrammingAssignment 7.1 (25 points)Using Java create an interactive (the user should be able to input the values) Java Application to process your utility bill for at-least 6 months. Use Class-Object methodology and use single dimensional arrays in your application. Write detailed comments in your program. You should have a two classes (for example, MontlyBill.java & MontlyBillTest.java). You can also be creative and create your “own problem scenario” and come up with your “solution”.Assignment 7.2 (25 points)Use the same program...
Write a program that accept an integer input from the user and display the least number...
Write a program that accept an integer input from the user and display the least number of combinations of 500s, 100s, 50s, 20s, 10s, 5s, and 1s. Test your solution using this samples] a. Input: 250 Output: 1x200s, 1x50s b. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s c. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s d. Input: 19 Output: 1x10s, 1x5s, 4x1s ​[Hints] o Use division to determine the number of occurrence of each element (i.e. 200, 100)...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt the plaintext with Vigenere Cipher. The application should then print out the plaintext and the ciphertext.
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments....
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to do the following: 1....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT