Merge Two Sorted Linked Lists Into A New Sorted List

Nov 2, 2014

How to go through each link item in both lists, and directly link them into the new list in order without using insert()

class Link {
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long dd) // constructor
{ dData = dd; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
} // end class Link

[Code] ......

View Replies


ADVERTISEMENT

Merging Two Sorted Arrays Into A Single Sorted Array?

Jun 13, 2014

The code is meant to input 2 arrays (they must be sorted even if this is not verified ) and then merge them in such a way that a sorted merged array is created at the end.I need to avoid a simple concatenation and then sorting the resulting array operation.I m interested in what i m doing wrong .

The input i used was :

Enter list1:
5 1 5 16 61 111
Enter list2:
4 2 4 5 6
import java.util.*;
public class C7_31 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);

[code]...

View Replies View Related

Combine Or Merge 2 Sorted Maps Into 1 Based On Common Key?

Jul 16, 2013

I have a small problem to solve by which I would like to merge 2 sorted maps into 1.

Map A
-------
Keys, Values
1, A
2, B
3, C
4, D
5, E

Map B
-------
Keys, Values
1, 10
2, 20
3, 30
4, 40
5, 50

Final Map should look like:

Keys, Values
A, 10
B, 20
C, 30
D, 40
E, 50

The final map would have all the values from Map A as a key and the values from Map B as values in the Final Map. Is there a way to do this using Java?

View Replies View Related

Putting List In Sorted Order (Least To Greatest)

Feb 14, 2015

I am trying to make a code that takes a list and puts the list in sorted order (least to greatest).

public class SortedIntList {
static final int capacity = 10;
private int [] data;
private boolean unique;
private int size;
public SortedIntList(){
size =0;
data = new int [10];

[Code] ....

Here what the code produces.

Testing SortedIntList()
error when adding these values to list: 4 6 4
list should = [4, 4, 6]
actual list = [4, 6, 4]
was working properly prior to adding last value of 4

View Replies View Related

Merge Sort Java And Linked List?

Dec 11, 2014

I've playing around with linked lists and methods for sorting. So far I've tested the iterative sort, insertion sort, quick sort and they all worked perfectly. Now, I am trying to implement merge sort that would take a linked list of jobs and sort them according to their priority. I found a few solutions on the web, of which I am trying to implemented this one: LeetCode.

My system is a simple one, I do have a linked list of print jobs, each of which has the priority. The following code should sort my print queue and return the link node of the first sorted element. Here's the code.

//defining a job that has priority
public class Job {
private int priority;

[Code]....

The problem I've been trying to solve is that I am getting the stack overflow at line

ListNode<T> h1 = mergeSort(left);

meaning that I am getting into a loop somewhere down through the process of breaking the linked list into half, half or halfs and so on.

View Replies View Related

What Is Comparator In Sorted Set

Jul 11, 2014

I read the document [URL] ....

But I am not able to get what is actual use of this in set ?

View Replies View Related

Check If A String Array Is Sorted?

Jul 8, 2014

I have an assignment and one of the prompts is to do a binary search on an array if and only if the array of Strings is sorted. The binary search part I think I have completed, but it is the sorted array check that is throwing everything off. If the array is already sorted, return true; else, return false.

// Check if the array is sorted
public static boolean isSorted(String[] arr) {
//for (int i = 0; i < arr.length-1; i++)
//{
//if (arr[i].compareTo(arr[i+1]) > 0)
//return false;
//}
String[] arrSorted = arr;
Arrays.sort(arrSorted);

[code]....

View Replies View Related

JCF Show Sorted Table With Index

Oct 6, 2014

I am new to java or at least the collection series. My job is to make a method like this:

public void frequency(int[] arr)

And should contain numbers like: [ 2,5,2,9,7,1,100,2,3,5,77,9,1,2,6,5 ]

Now the next part i should sort the tabel and show how many times each number shows in the array:

with the index.

What is the best easiest way in collection?

View Replies View Related

Copying All Elements In Array A To B Sorted In Ascending Order

Apr 7, 2014

How do u copy all the elements in an array eg A into another array eg B? This is the question:

An array A contains integers that first increase in value and then decrease in value,

For example, 17 24 31 39 44 49 36 29 20 18 13

It is unknown at which point the numbers start to decrease. Write efficient code to code to copy the numbers in A to another array B so that B is sorted in ascending order. Your code must take advantage of the way the numbers are arranged in A.

This is my program:

This is the error message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at Quest30.CopyAndSortArray.main(CopyAndSortArray.jav a:16)

View Replies View Related

Display All Student With Their Grade Sorted From Highest To Lowest

Oct 22, 2014

Write a program that promts a professor to input grades for five different courses for 10 students. Prompt the professor to enter only A,B,C,D, or F for grades(A is the highest grade, F fail). use variables for student number(1 through 10) and grade numbers(1 through 5). create a menu for Search. if the user select search it will prompt a letter correspond to grade. display all student with selected grade. if the user just enter nothing, display all student with their grade sorted from highest to lowest.

View Replies View Related

Longest Sorted Sequence - Index Out Of Bounds Exception

Jul 13, 2014

My problem is that I can't even run the program, because it gives me

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at domashno.ten.longestSortedSequence(ten.java:37)
at domashno.ten.main(ten.java:17)

Code :

public static void main(String[] args) {
int[] arr = {3, 8, 10, 1,9, 14, -3, 0, 14, 207, 56, 98, 12};
 longestSortedSequence(arr);
System.out.println(longestSortedSequence(arr));
}
public static int longestSortedSequence(int[] arr) {
 
[Code] ....

View Replies View Related

Linked Lists In Java

Nov 27, 2014

I currently don't understand how the node's work.

private static class Node<AnyType>
{
private AnyType data;
private Node<AnyType> next;
public Node(AnyType data, Node<AnyType> next)
{
this.data = data;
this.next = next;
}
}

Why do I make private Node<AnyType> next;And why do I have an inner class of Node for a linked list?I had the same topic in C, but there it was somehow easier than in java. Because there you have pointers.

View Replies View Related

Iterating Through Linked Lists

Apr 27, 2014

Write a Java function Sum2List that takes two lists L1 and L2 of the same size and returns list L that contains the sum of data inside the corresponding nodes of lists L1 and L2.

ex: L1 = {1,2,3}
L2 = {4,5,6}
L = {5,7,9}

I do not know how to iterate through two different lists >>

public LinkedList Sum2List(LinkedList l1, LinkedList l2){
LinkedList l3 = new LinkedList();
ListNode Current1 = L1.getFront();
ListNode Current2 = L2.getFront();
while(Current1 != null){
l3.Add(Current1.getItem() + Current2.getItem());

[Code] .....

This my attempt to write the code , but the dr. side that we have not to use the build in methods ( getNext()...etc)
in the function .

View Replies View Related

How To Iterate Through Linked Lists And Add Them To A Map

Mar 10, 2014

I have an XML sheet and my project is top retrieve the required elements from XML sheet. So my format of XML was like follows:

<Class>
<Employees>
<EMPLOYEE>
<ENum> Abc123</ENum>
<Ename> John<?Ename>
<EType>Mathematics</EType>

[Code] ....

I have used unmarshalling concept to retrieve the data elements... I have to check whether the elements satisfy few regulations when compared with data in Database. So, i thought of grouping the employees depending on EType. I have created a Map with linkedlist of employees. Say Map<String, LinkedList<Employe>>EmpMap=new Map<String, LinkedList<Employe>>();

I have already created a class named Employee which has all the setter and getter methods for employee.

Here am going to take Etype(Employee type) as key and linkedlist(list of employees of certain type) as value. How to iterate these linked lists and place them in my Map.

View Replies View Related

Delete Method With Linked Lists?

Jul 19, 2014

Why is this method throwing a null pointer exception when I call it from main?

I'm trying to submit an integer to indicate the number of nodes in a linked list to delete and then delete them headfirst.

public boolean delete(int x){
if (x > count || head == null){
return false;
}else{ while (x>0){
head = head.getPrevious();

[Code] ....

View Replies View Related

Sorting Linked Lists Via Consecutive Nodes

Feb 13, 2014

I have the following code that supposed to perfrom sorting on the linked list using method sort in order usind node concept of Linked List but seems to give inlogic results. the following code tests only the values lower than the first value but i can't manage to sort the data higher than the first entered value;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.*;
public class ListNode<T extends Comparable> {
ListNode<T> nextNode;
T data;
public ListNode(T item)

[Code] .....

View Replies View Related

Doubly Linked Lists And Using Java Generics

Oct 25, 2014

I'm working with Doubly Linked Lists and using Java Generics..

My nodes looks like this:
class DNode<E> {
DNode<E> previous;
DNode<E> next;
E element;

//and all methods inside
}

My list of Nodes looks like this:
class DLL<E>{
private DNode<E> head;
private DNode<E> tail;
private int size;

[code]....

As you can see, as arguments they get "E o"...I need to write a program, which from the main function asks the users how long is the list, and after they type it's length, I ask them to start typing the elements (integers)...and this is how my main method is written, but I can't seem to make it work, specialy when I call the "insLast" method,I guess it's because the arguments i'm giving to the function...how to read the elements and write them into the list?

public static void main(String[] args) throws IOException {
DLL<Integer> lista=new DLL<Integer>();
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s = stdin.readLine();
int N = Integer.parseInt(s);
s = stdin.readLine();
String[] pomniza = s.split(" ");
for (int i = 0; i < N; i++) {
lista.instLast(Integer.parseInt(pomniza[i]));
}

}

View Replies View Related

Head Of Multiple Linked Lists All Getting Set To The Same Thing?

Apr 16, 2014

I am creating a chained hash table that uses my own LinkedListclass to handle collisons. Here is where my issue is occuring: I start with an array of LinkedLists, my "hash table", intially set to null. When i wish to add a word, if the desired location is null, I create a new LinkedList, place it in that array index. I then create a new LinkedListNode and set the head of the new LinkedList to the new node.

My issue is occuring here: Whenever I set the head of my new LinkedList to my new LinkedListNode all of my previously created linkedlists also have their head changed to the new node.

My debugger shows that I am truly creating new linkedlists every time. I was afraid each array index was just pointing to a single array, but that is evidently not the issue. Below is all the relevant code

public class SetOfStrings {
private int arraySize;
private LinkedList[] mainArray;
private int totalCount;
//other methods here
public boolean add(String toAdd) {
int hashToAdd = hash(toAdd);

[code]....

SUMMARY: Whenever I set the head of a Linked List to a new node, it changes the head of all my linked lists to point to the same new node

View Replies View Related

Linked Lists - Reading Information From A File

Oct 29, 2014

Basically, the program is to be able to read information from a file like this;

11111 63.5
22222 59.9
33333 82.4
44444 73.1
55555 77.3
66666 99.5
77777 68.7
88888 53.2
99999 66.8

where the first number is student number and the second is their grade. I need to read this information from a .txt file and dynamically create a new node containing that student's number and mark, and insert it in the correct position in the linked list (in descending order based on grade). So I get that each Node needs to contain two data types, an Int for Student # and a Double for their grade, and I'm pretty sure I've done it correctly with my StudentNode class which can be found in the source code linked above.

But what I don't get it using that class to create Nodes in my main class and then sort them based on their Double grade value WHILE they are being sorted. I just don't understand where to actually put the methods and such that does these things. Apparently I'm supposed to have three classes.

One named StudentNode which is just the node info, which I have done.

The second is called StudentList which is apparently supposed to contain the head of LinkedList and the methods I need? I'm not sure how it ties into StudentNode though.

The third and final is just the main class which I'll use to test it.

Then after all that I need to print out the median mark through a recursive method that isn't allowed to use any loops or call and functions/methods that use loops. The function should return the node in the list which contains the median mark. Secondly, in order to find the median, you need to know how many items in total are in the list. Your recursive function must calculate that number (also recursively); you may not keep track of this count elsewhere your program. Your recursive solution should only examine each node only once, and the depth of the recursion should be equal to the number of nodes in the list.

Here's my current in-progress code.

import java.io.FileNotFoundException;
import java.util.Scanner;
class StudentNode {
private int studentNum;
private double grade;
private StudentNode next;

[Code] .....

I'm pretty sure the StudentNode class is fine, it's the StudentList class that I'm not sure about. I'm not sure how to add nodes since there isn't a getNext() method in the StudentList class.

View Replies View Related

Big-O Involving Arrays And Linked Lists When Using PriorityQueue

Oct 25, 2014

I'm not sure if my understanding of PriorityQueues is correct, so I'm trying to check if my reasoning is valid. I'm supposed to compare the Big-O for arrays and linked lists for the following instructions:

Insert 100 objects having the priorities 1, 2, 3, ... , 99, 100
Big-O for Array: __________
Big-O for Linked List: ___________

Insert 100 objects having the priorities 100, 99, 98, ... , 2, 1
Big-O for Array: __________
Big-O for Linked List(Assume no tail reference): ___________

If my understanding is correct, priority queues take in items randomly with no particular order, but they are removed according to the priority of each element. If what I've said is true, wouldn't that mean that inserting any number of objects would be O(1) for both linked lists and arrays? If the PriorityQueue has no particular order, then wouldnt each add() simply insert something to the next array index/linked list node?

View Replies View Related

Implement A Set Abstract Data Type Using Singly Linked Lists

May 6, 2015

I am trying to implement product method below which returns the set representing the Cartesian product of the current set and a given set (the Cartesian product contains all ordered pairs (a, b) where a belongs to the current set, and b belongs to the given set). The product should be a ListSet <Tuple<E>> object where each ordered pair is a Tuple element. (I have a Tuple class which implements an ordered tuple)

What am I trying to do in the product method : Make 2 for loop and inside the for loop make an array of <E> then set the 2 elements of the tuple then again set tuple and add it to arrayList. how to set 2 elements of the tuple and set tuple ??

public class ListSet<E> implements Iterable<E>{
SinglyLinkedList<E> sl;
public ListSet(){
sl = new SinglyLinkedList<E>();

[code]....

View Replies View Related

Sort Linked List Through The Nodes Of List - Data Of Calling Object

Feb 14, 2014

I have some class called sorted to sort the linked list through the nodes of the list. and other class to test this ability, i made object of the sort class called "list1" and insert the values to the linked list.

If i make other object called "list2" and want to merge those two lists by using method merge in sort class. And wrote code of

list1.merge(list2);

How can the merge method in sort class know the values of list1 that called it as this object is created in other class.

View Replies View Related

Linked List Implementation Of List Interface?

Oct 5, 2013

So we have an assignment regarding a linked list implementation of a given list interface.

In my list interface, the method contains(T anEntry) is defined.

In the LList implementation, contains is already implemented as part of getting the core methods in.

Now I am being tasked with the following:

Provide a second implementation of the method contains2(T anEntry) that calls a private recursive method

Private boolean contains (T anEntry, Node startNode) that returns whether the list that starts at startNode contains the entry anEntry.

I've written the private recursive method already. That's not an issue (at least not right now).

But what I don't understand is how startNode is supposed to be populated when this private contains method is called from the public contains2 method? contains2 only takes one parameter: anEntry. the private method takes two parameters: anEntry and startNode. How am i supposed to provide startNode when I am calling contains2?

View Replies View Related

Sum2List - Returns List That Contains Sum Of Data Inside Corresponding Nodes Of Lists

Apr 30, 2014

Write a Java function Sum2List that takes two lists L1 and L2 of the same size and returns list L that contains the sum of data inside the corresponding nodes of lists L1 and L2.

View Replies View Related

List Interface Class That Has Operations For Linked List And LList Class

Oct 6, 2014

I have this ListInterface class that has operations for my linked list and a LList class. The Llist and ListInterface classes are perfect. My job is to create a driver, or a demo class that showcases these operations. That being said, heres the driver so far:

import java.util.*;
public abstract class DriverWilson implements ListInterface
{
public static void main(String[] args)
{

LList a = new LList();

[code]....

View Replies View Related

Inserting A Set Into Linked List?

Mar 21, 2014

What I'm supposed to do is make a method to insert a set of Tiles to the list,i.e.,a detour(make sure that the inserted detouris compatible with thecurrent path so that the resultingpathdoesnot have any gaps). But I'm confused on how to go about doing it. I was thinking of maybe just adding 1 to the current Node.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;
public class Path {
static Tile startTile;

[code].....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved