0% found this document useful (0 votes)
3 views2 pages

Range Main

The document contains a Java program that defines a class `RangeNodeMain` with methods for processing linked lists of integers. It includes methods to create ranges from consecutive integers, sum values in a series, count nodes in a list, and check if a list can be divided into three equal parts with identical values. The program utilizes a custom `Node` class to manage linked list operations.

Uploaded by

leptoplenov1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Range Main

The document contains a Java program that defines a class `RangeNodeMain` with methods for processing linked lists of integers. It includes methods to create ranges from consecutive integers, sum values in a series, count nodes in a list, and check if a list can be divided into three equal parts with identical values. The program utilizes a custom `Node` class to manage linked list operations.

Uploaded by

leptoplenov1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;
public class RangeNodeMain
{

static Scanner reader= new Scanner (System.in);

public static Node <RangeNode> range (Node <Integer> list)


{
Node <RangeNode> result= new Node <RangeNode> (null);
Node <RangeNode> last= result;

while(list.hasNext())
{
int from= list.getValue();
int to=list.getValue();
while(list.hasNext()&&list.getValue()
+1==list.getNext().getValue())
{
to= list.getNext().getValue();
list=list.getNext();
}
RangeNode r = new RangeNode(from,to);
Node <RangeNode> node= new Node <RangeNode>(r);
System.out.println(r);
last.setNext(node);
last= node;
if (list.hasNext())
list=list.getNext();
//System.out.println(last);

}
return result.getNext();
}

public static Node <Integer> tatSerese (Node <Integer> list)


{
Node <Integer> result= new Node <Integer>(list.getValue());
Node <Integer> last;
last=result;
int sum=0;
list=list.getNext();

while(list.hasNext())
{
if(list.getValue()<list.getNext().getValue())
{
sum+=list.getValue();
list=list.getNext();
}

else
{
System.out.println(last);
sum+=list.getValue();
Node <Integer> node=new Node <Integer> (sum);
last.setNext(node);
last=node;
sum=0;
list=list.getNext();

}
}

return result.getNext();

public static int count (Node <Integer> list)


{
int count=0;
while(list!=null)
{
count++;
list=list.getNext();
}
return count;
}

public static boolean thirdList (Node <Integer> list)


{
int count= count(list);

if (count == 0 || count % 3 != 0)
return false;

// 1 2 3 4 5 6 7 8 9
Node <Integer> frog1 = list;
Node <Integer> frog2 = list;

for(int i=0; i<count/3; i++){


frog2 = frog2.getNext();
}

Node <Integer> frog3 = frog2;


for(int i=0; i<count/3; i++){
frog3 = frog3.getNext();
}

for(int i=0;i<count/3;i++)
{
if(frog1.getValue()!=frog2.getValue() ||
frog2.getValue()!= frog3.getValue())
{
return false;
}
frog1 = frog1.getNext();
frog2 = frog2.getNext();
frog3 = frog3.getNext();
}

return true;
}

You might also like