Question

In: Computer Science

Ex1) Download the code from the theory section, you will find zipped file contains the code...

Ex1) Download the code from the theory section, you will find zipped file contains the code of Singly linked list and Doubly linked list, in addition to a class Student.

In Class SignlyLinkedList,

1. There is a method display, what does this method do?

2. In class Test, and in main method, create a singly linked list objet, test the methods of the list.

3. To class Singly linked list, add the following methods:

a- Method get(int n), it returns the elements in the node number n, assume the first node number is 1. You need to check that n is within the range of nodes in the list, otherwise method get returns null.

What is the complexity of your method? Test the method in main.

b- Method insertAfter(int n, E e), its return type is void, it inserts element e in a new node after the node number n, assume the first node number is 1. You need to check that n is within the range of nodes in the list, otherwise through an exception.

What is the complexity of your method?

Test the method in main.

c- Method remove(int n): it removes the node number n, and returns the element in that node, assuming the first node number is 1. You need to check that n is within the range of nodes in the list, otherwise method get returns null.

What is the complexity of your method?

Test the method in main.

d- Method reverse( ): it has void return type. It reverse the order of the elements in the singlylinked list.

What is the complexity of your method?

Test the method in main.

Ex2) In Class DoublyLinkedList

1. There are two methods printForward, printBackward, what do they do?

2. In class Test, and in main method, create a doubly linked list objet, test the methods of the list.

4. To class Doubly linked list, add the following methods:

e- Method get(int n), it returns the elements in the node number n, assume the first node number is 1. You need to check that n is within the range of nodes in the list, otherwise method get returns null.

To make your code more efficient, you should start from the end (header or trailer) that is closer to the target node.

What is the complexity of your method?

Test the method in main.

f- Method insertAfter(int n, E e), its return type is void, it inserts element e in a new node after the node number n, assume the first node number is 1. You need to check that n is within the range of nodes in the list, otherwise through an exception. . To make your code more efficient, you should start from the end (header or trailer) that is closer to the target node.

What is the complexity of your method?

Test the method in main.

g- Method remove(int n): it removes the node number n, and returns the element in that node, assuming the first node number is 1. You need to check that n is within the range of nodes in the list, otherwise method get returns null. To make your code more efficient, you should start from the end (header or trailer) that is closer to the target node.

What is the complexity of your method?

Test the method in main.

Assignment:

To class DoublyLinedList, add method remove(E e) which removes all nodes that has data e. This method has void return type.

doublylinkedlist class:

package doubly;

public class DoublyLinkedList <E>{
   private Node<E> header;
   private Node<E> trailer;
   private int size=0;
   public DoublyLinkedList() {
       header=new Node<>(null,null,null);
       trailer=new Node<>(null,header,null);
       header.setNext(trailer);
   }
   public int size() { return size;}
   public boolean isEmpty() {return size==0;}
   public E first()
   {
   if (isEmpty()) return null;
       return header.getNext().getData();
   }
   public E last()
   {
       if (isEmpty()) return null;
           return trailer.getPrev().getData();
   }
  
   private void addBetween(E e, Node<E> predecessor, Node<E> successor)
   {
       Node<E> newest=new Node<>(e,predecessor,successor);
       predecessor.setNext(newest);
       successor.setPrev(newest);
       size++;
      
   }
   private E remove(Node<E> node)
   {
       Node<E> predecessor=node.getPrev();
       Node<E> successor=node.getNext();
       predecessor.setNext(successor);
       successor.setPrev(predecessor);
       size--;
       return node.getData();
   }
   public void addFirst(E e){
       addBetween(e,header,header.getNext());
   }
   public void addLast(E e){
       addBetween(e,trailer.getPrev(),trailer);
   }
  
   public E removeFirst(){
       if(isEmpty()) return null;
       return remove(header.getNext());
   }
  
   public E removeLast()
   {
   if(isEmpty()) return null;
   return remove(trailer.getPrev());
   }
   public void printForward()
   {
       for (Node<E> tmp=header.getNext();tmp!=trailer;tmp=tmp.getNext())
           System.out.println(tmp.getData());
   }
  
   public void printBackward()
   {
       for (Node<E> tmp=trailer.getPrev();tmp!=header;tmp=tmp.getPrev())
           System.out.println(tmp.getData());
   }
  
  
}

node class:

package doubly;

public class Node <E>{
   private E data;
   private Node<E> prev;
   private Node<E> next;
  
   public Node(E d, Node<E> p,Node<E> n)
   {
   data=d;
   prev=p;
   next=n;
   }
   public E getData() { return data; }
   public Node<E> getNext(){ return next; }
   public Node<E> getPrev(){ return prev; }
   public void setNext(Node<E> n) { next=n;}
   public void setPrev(Node<E> p) { prev=p;}

}

student class:

package doubly;

public class Student {
private int id;
private String name;
public Student(int id, String name) {
   super();
   this.id = id;
   this.name = name;
}
public int getId() {
   return id;
}
public void setId(int id) {
   this.id = id;
}
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
@Override
public String toString() {
   return "[id=" + id + ", name=" + name + "]";
}

}

test class:

package doubly;

public class Test {
public static void main(String arg[])
{
   DoublyLinkedList<Student> myList=new DoublyLinkedList<>();
   myList.addFirst(new Student(1,"Ahmed"));
   myList.addFirst(new Student(2,"Khaled"));
   myList.addLast(new Student(3,"Ali"));
   myList.removeLast();
   myList.printForward();
  
  
  
  
}
}

Solutions

Expert Solution

Assignment Solution:

Doublylinkedlist class:

public class DoublyLinkedList <E>{
private Node<E> header;
private Node<E> trailer;
private int size=0;
public DoublyLinkedList() {
header=new Node<>(null,null,null);
trailer=new Node<>(null,header,null);
header.setNext(trailer);
}
public int size() { return size;}
public boolean isEmpty() {return size==0;}
public E first()
{
if (isEmpty()) return null;
return header.getNext().getData();
}
public void remove(Node<E> e)
{
if (isEmpty()) return null;
Node<E> next;
for (Node<E> tmp=header.getNext();tmp!=trailer; )
{

if (tmp.getData() == e.getData())
{

next = tmp.getNext();
  
header = remove(tmp);
  
tmp = next;
}
  
else
tmp = tmp.getNext();
  
}
  

  
}

public E last()
{
if (isEmpty()) return null;
return trailer.getPrev().getData();
}
  
private void addBetween(E e, Node<E> predecessor, Node<E> successor)
{
Node<E> newest=new Node<>(e,predecessor,successor);
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
  
}
private E remove(Node<E> node)
{
Node<E> predecessor=node.getPrev();
Node<E> successor=node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getData();
}
public void addFirst(E e){
addBetween(e,header,header.getNext());
}
public void addLast(E e){
addBetween(e,trailer.getPrev(),trailer);
}
  
public E removeFirst(){
if(isEmpty()) return null;
return remove(header.getNext());
}
  
public E removeLast()
{
if(isEmpty()) return null;
return remove(trailer.getPrev());
}
public void printForward()
{
for (Node<E> tmp=header.getNext();tmp!=trailer;tmp=tmp.getNext())
System.out.println(tmp.getData());
}
  
public void printBackward()
{
for (Node<E> tmp=trailer.getPrev();tmp!=header;tmp=tmp.getPrev())
System.out.println(tmp.getData());
}
  
  
}


Related Solutions

Ex1) Download the code from the theory section, you will find zipped file contains the code...
Ex1) Download the code from the theory section, you will find zipped file contains the code of Singly linked list and Doubly linked list, in addition to a class Student. In Class SignlyLinkedList, 1. There is a method display, what does this method do? 2. In class Test, and in main method, create a singly linked list objet, test the methods of the list. 3. To class Singly linked list, add the following methods: a- Method get(int n), it returns...
Go to the Files section and download the AFE_Test file from the Datasets folder. We are...
Go to the Files section and download the AFE_Test file from the Datasets folder. We are interested in a one­tail test described in the following fashion: Ho: u < or = to 200 CFM; H1: u > 200 CFM. At 5% significance level, we can reject the null hypothesis given the sample information in AFE_Test1. we can reject the null hypothesis given the sample information in AFE_Test2. we cannot reject the null hypothesis. we can reject the null hypothesis given...
Question 2. Go to the Blackboard and download the MS excel file, ‘stock_return.xlsx’. It contains a...
Question 2. Go to the Blackboard and download the MS excel file, ‘stock_return.xlsx’. It contains a year of monthly stock price data of Amazon, Pfizer, and S&P 500 (Market Index). Using the data, answer the following questions. (50 points) (1) Compute the monthly return of Amazon and Pfizer. You should get 12 monthly returns for each. To get a monthly return, you need to use previous month’s stock price. For example, Amazon’s stock return of 2018-01 will be [(Stock price...
C programing language A file "data.txt" contains only integers. Write a code to find average of...
C programing language A file "data.txt" contains only integers. Write a code to find average of all values and print the average How would you use execlp function to execute "ps –e –a –l" command char *dt = "The five boxing wizards jump quickly"; write a program to count frequency of each letter, ignore case. Print the letter and frequency of each letter. // 1A: . Ask the user to enter a password string, store it in pass. Password should...
This is an assignment for python. Download the code “ GradeBook.py ” You will be modifying...
This is an assignment for python. Download the code “ GradeBook.py ” You will be modifying the code to do the following: 1) create an empty dictionary named “FinalAverages” 2) In one for loop you will zip all lists together and get their individual members on each iteration. You can name these what ever you want. 3) on each iteration: Calculate the WEIGHTED average using the weights provided, and then add a new dictionary value to the dictionary “ FinalAverages...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any other C++ IDE and run it. You will have to implement a small intentional bug in your program // This program uses a function that returns a value. #include <iostream> using namespace std; // Function prototype int sum(int num1, int num2); int main() {    int value1 = 20,   // The first value        value2 = 40,   // The second value        total;         //...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any other C++ IDE and run it. You will have to implement a small intentional bug in your program and post it for other students to debug. To be able to receive your full discussion points, you need to submit the following. Following is your check list and rubric       Attach your .cpp file/s with an implemented bug - 20pnts       Describe what the code...
On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains...
On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains data on past students in this course. Under Midterm is information on whether past studentsgot an A grade (A−, A, A+) an F or D grade (D is a passing grade but most students need aC− for it to count towards their program) or Other (any grade in between). Under FinalExam is information on whether students got a D or F grade or anything...
Write a code to find the following in a text file (Letter). language: Python (a) Find...
Write a code to find the following in a text file (Letter). language: Python (a) Find the 20 most common words (b) How many unique words are used? (c) How many words are used at least 5 times? (d) Write the 200 most common words, and their counts, to a file. text file: Look in thy glass and tell the face thou viewest, Now is the time that face should form another, Whose fresh repair if now thou not renewest,...
Find and fix the compile time bugs in the code at the end of this section....
Find and fix the compile time bugs in the code at the end of this section. Compile time bugs show as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here. This assignment is meant to test your attention to detail and strengthen your debugging skills. Here is the code. // Week 4 Assignment-1 // Description: Compile time errors //---------------------------------- //**begin #include files************ #include <iostream> //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT