Question

After having downloaded net.datastructures and set you classpath appropriately, create a Java program named “TestDS.java” that...

After having downloaded net.datastructures and set you classpath appropriately, create a Java program named “TestDS.java” that does the following:

- Imports SinglyLinkedList, LinkedQueue, and LinkedStack from net.datastructures (NOT from java.util)

- Creates an object of each type that can hold only the Double data type. (The data structures are generic, make them of type Double)

- Using a loop or series of commands, perform the following:

1) To the Linked List, add the numbers from 1.0 to 100.0 (both of these will be included) incrementing between each number by 0.5. That is, 1.0, 1.5, 2.0, 2.5, etc. will be stored in the list. When finished, print the list.

2) To the Stack, add the values 1.0, 0.5, 0.25, 0.125, … etc. down to 1/ 2^20. That is, each time, divide by two, and do this 20 times. When finished, print the number from the top of the stack without removing it.

3) To the Queue, add the values from the stack so that the stack is emptied when you are done. Then, add the values from the list so that the list is emptied when you are done.

- Print the queue and the values stored in the queue when this process is done.

Homework Answers

Answer #1

1.

import java.io.*;

  
// Java program to implement
// a Singly Linked List

public class LinkedList {

  

Node head; // head of list

  

// Linked list Node.

// This inner class is made static

// so that main() can access it

static class Node {

  

int data;

Node next;

  

// Constructor

Node(int d)

{

data = d;

next = null;

}

}

  

// Method to insert a new node

public static LinkedList insert(LinkedList list, int data)

{

// Create a new node with given data

Node new_node = new Node(data);

new_node.next = null;

  

// If the Linked List is empty,

// then make the new node as head

if (list.head == null) {

list.head = new_node;

}

else {

// Else traverse till the last node

// and insert the new_node there

Node last = list.head;

while (last.next != null) {

last = last.next;

}

  

// Insert the new_node at last node

last.next = new_node;

}

  

// Return the list by head

return list;

}

  

// Method to print the LinkedList.

public static void printList(LinkedList list)

{

Node currNode = list.head;

System.out.print("LinkedList: ");

// Traverse through the LinkedList

while (currNode != null) {

// Print the data at current node

System.out.print(currNode.data + " ");

// Go to next node

currNode = currNode.next;

}

}

// Driver code

public static void main(String[] args)

{

/* Start with the empty list. */

LinkedList list = new LinkedList();

  

//

// ******INSERTION******

//

  

// Insert the values

list = insert(list, 1.0);

list = insert(list, 1.5);

list = insert(list, 2.0);

list = insert(list, 2.5);

  


  

// Print the LinkedList

printList(list);

}
}

OUTPUT: 1.0 1.5 2.0 2.5

2.

import static java.lang.System.exit;

// Create Stack Using Linked list

class StackUsingLinkedlist {

// A linked list node

private class Node {

int data;

Node link;

}

// Reference variable

Node top;

// Constructor

StackUsingLinkedlist()

{

this.top = null;

}

// Function to add an element x

// in the stack by inserting

// at the beginning of LL

public void push(int x)

{

// Create new node temp

Node temp = new Node();

// If stack is full

if (temp == null) {

System.out.print("\nHeap Overflow");

return;

}

// Initialize data into temp

temp.data = x;

// Reference into temp link

temp.link = top;

// Update top reference

top = temp;

}

// Function to check if the stack

// is empty or not

public boolean isEmpty()

{

return top == null;

}

// Function to return top element

// in a stack

public int peek()

{

// Check for empty stack

if (!isEmpty()) {

// Return the top data

return top.data;

}

// Otherwise stack is empty

else {

System.out.println("Stack is empty");

return -1;

}

}

// Function to pop top element from

// the stack by removing element

// at the beginning

public void pop()

{

// Check for stack underflow

if (top == null) {

System.out.print("\nStack Underflow");

return;

}

// Update the top pointer to

// point to the next node

top = (top).link;

}

// Function to print the elements and

// restore the stack

public static void

DisplayStack(StackUsingLinkedlist s)

{

// Create another stack

StackUsingLinkedlist s1

= new StackUsingLinkedlist();

// Until stack is empty

while (!s.isEmpty()) {

s1.push(s.peek());

// Print the element

System.out.print(s1.peek()

+ " ");

s.pop();

}

}
}

// Driver Code

public class GFG {

// Driver Code

public static void main(String[] args)

{

// Create Object of class

StackUsingLinkedlist obj

= new StackUsingLinkedlist();

// Insert Stack value

obj.push(1.0);

obj.push(0.5);

obj.push(0.25);

obj.push(0.125);

// Function Call

obj.DisplayStack(obj);

}
}

OUTPUT:
1.0 0.5 0.25 0.125

3.

import java.util.*;

  

public class GFG {

public static void main(String[] args)

throws IllegalStateException

{

  

// create object of Queue

Queue<Integer> Q

= new LinkedList<Integer>();

  

// Add numbers to end of Queue

Q.add(7855642);

Q.add(35658786);

Q.add(5278367);

Q.add(74381793);

  

// before removing print queue

System.out.println("Queue: " + Q);

}
}

Output:
Queue: [7855642, 35658786, 5278367, 74381793]

Know the answer?
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
Similar Questions
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...
c++ Program Description You are going to write a computer program/prototype to process mail packages that...
c++ Program Description You are going to write a computer program/prototype to process mail packages that are sent to different cities. For each destination city, a destination object is set up with the name of the city, the count of packages to the city and the total weight of all the packages. The destination object is updated periodically when new packages are collected. You will maintain a list of destination objects and use commands to process data in the list....
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these classes: Product, Customer, and Store. All data members of each class should be marked as **private** (a leading underscore in the name). Since they're private, if you need to access them from outside the class, you should do so via get or set methods. Any get or set methods should be named per the usual convention ("get_" or "set_" followed by the name of...