Longest increasing sublist in a linked list

Here given code implementation process.

// C Program 
// Longest increasing sublist in a linked list
#include <stdio.h>

#include <stdlib.h> //for malloc function

// Linked List LinkNode
struct LinkNode
{
	int data;
	struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
	struct LinkNode *head;
	struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
	// Create memory of head and tail Nodes
	struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
	if (sll == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		sll->head = NULL;
		sll->tail = NULL;
	}
	return sll;
}
// Returns a new Node of linked list
struct LinkNode *createLinkNode(int data)
{
	// Create dynamic node
	struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
	if (node == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		// Set initial node value
		node->data = data;
		node->next = NULL;
	}
	return node;
}
// Add new Node at end of linked list 
void addNode(struct SingleLL *sll, int data)
{
	struct LinkNode *node = createLinkNode(data);
	if (sll->head == NULL)
	{
		sll->head = node;
	}
	else
	{
		// Append the node at last position
		sll->tail->next = node;
	}
	sll->tail = node;
}
// Display linked list element
void display(struct SingleLL *sll)
{
	if (sll->head == NULL)
	{
		printf("\n Empty linked list\n");
		return;
	}
	struct LinkNode *temp = sll->head;
	// iterating linked list elements
	while (temp != NULL)
	{
		printf(" %d →", temp->data);
		// Visit to next node
		temp = temp->next;
	}
	printf(" NULL\n");
}
// Finding and print longest increasing sublist
void increasingSublist(struct SingleLL *sll)
{
	if (sll->head == NULL)
	{
		printf("\n Empty linked list\n");
		return;
	}
	// Define some useful variables 
	struct LinkNode *result = sll->head;
	struct LinkNode *auxiliary = sll->head;
	struct LinkNode *start = NULL;
	// Define some counter variables
	int count = 1;
	int length = 1;
	// iterating linked list element
	while (auxiliary->next != NULL)
	{
		if (auxiliary->data < auxiliary->next->data)
		{
			count++;
		}
		else
		{
			if (count > length)
			{
				// When a new increasing sublist is found
				// Get the starting node of sublist
				result = start;
				// Update the sublist length
				length = count;
			}
			count = 1;
			start = auxiliary->next;
		}
		if (start == NULL)
		{
			start = auxiliary;
		}
		// Visit to next node
		auxiliary = auxiliary->next;
	}
	// Test last sublist
	if (count > length)
	{
		// When last pair of sublist larger
		result = start;
		length = count;
	}
	// Display linked list elements
	printf(" Linked List \n");
	display(sll);
	printf(" Longest increasing sublist is \n");
	// Display increasing sublist
	while (result != NULL && length > 0)
	{
		// Display node value
		printf(" %d →", result->data);
		// Visit to next node
		result = result->next;
		length--;
	}
	printf(" NULL\n");
}
int main()
{
	// Create a empty linked list
	struct SingleLL *sll = newLinkedList();
	//  Constructed linked list
	//  11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
	addNode(sll, 11);
	addNode(sll, 12);
	addNode(sll, -2);
	addNode(sll, 3);
	addNode(sll, 5);
	addNode(sll, 9);
	addNode(sll, 12);
	addNode(sll, 4);
	addNode(sll, 7);
	addNode(sll, 8);
	increasingSublist(sll);
	return 0;
}

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL
/*
  Java Program for
  Longest increasing sublist in a linked list
*/
// Linked list node
class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	//Add new Node at end of linked list 
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			System.out.print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		System.out.print(" NULL\n");
	}
	// Finding and print longest increasing sublist
	public void increasingSublist()
	{
		if (this.head == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		// Define some useful variables 
		LinkNode result = this.head;
		LinkNode auxiliary = this.head;
		LinkNode start = null;
		// Define some counter variables
		int count = 1;
		int length = 1;
		// iterating linked list element
		while (auxiliary.next != null)
		{
			if (auxiliary.data < auxiliary.next.data)
			{
				count++;
			}
			else
			{
				if (count > length)
				{
					// When a new increasing sublist is found
					// Get the starting node of sublist
					result = start;
					// Update the sublist length
					length = count;
				}
				count = 1;
				start = auxiliary.next;
			}
			if (start == null)
			{
				start = auxiliary;
			}
			// Visit to next node
			auxiliary = auxiliary.next;
		}
		// Test last sublist
		if (count > length)
		{
			// When last pair of sublist larger
			result = start;
			length = count;
		}
		// Display linked list elements
		System.out.print(" Linked List \n");
		display();
		System.out.print(" Longest increasing sublist is \n");
		// Display increasing sublist
		while (result != null && length > 0)
		{
			// Display node value
			System.out.print(" " + result.data + " →");
			// Visit to next node
			result = result.next;
			length--;
		}
		System.out.print(" NULL\n");
	}
	public static void main(String[] args)
	{
		// Create a empty linked list
		SingleLL sll = new SingleLL();
		//  Constructed linked list
		//  11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
		sll.addNode(11);
		sll.addNode(12);
		sll.addNode(-2);
		sll.addNode(3);
		sll.addNode(5);
		sll.addNode(9);
		sll.addNode(12);
		sll.addNode(4);
		sll.addNode(7);
		sll.addNode(8);
		sll.increasingSublist();
	}
}

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Longest increasing sublist in a linked list
*/

// Linked list node
class LinkNode
{
	public: 
    int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class SingleLL
{
	public: 
    LinkNode *head;
	LinkNode *tail;
	SingleLL()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	//Add new Node at end of linked list
	void addNode(int data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == NULL)
		{
			this->head = node;
		}
		else
		{
			// Append the node at last position
			this->tail->next = node;
		}
		this->tail = node;
	}
	// Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		LinkNode *temp = this->head;
		//iterating linked list elements
		while (temp != NULL)
		{
			cout << " " << temp->data << " →";
			// Visit to next node
			temp = temp->next;
		}
		cout << " NULL\n";
	}
	// Finding and print longest increasing sublist
	void increasingSublist()
	{
		if (this->head == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		// Define some useful variables
		LinkNode *result = this->head;
		LinkNode *auxiliary = this->head;
		LinkNode *start = NULL;
		// Define some counter variables
		int count = 1;
		int length = 1;
		// iterating linked list element
		while (auxiliary->next != NULL)
		{
			if (auxiliary->data < auxiliary->next->data)
			{
				count++;
			}
			else
			{
				if (count > length)
				{
					// When a new increasing sublist is found
					// Get the starting node of sublist
					result = start;
					// Update the sublist length
					length = count;
				}
				count = 1;
				start = auxiliary->next;
			}
			if (start == NULL)
			{
				start = auxiliary;
			}
			// Visit to next node
			auxiliary = auxiliary->next;
		}
		// Test last sublist
		if (count > length)
		{
			// When last pair of sublist larger
			result = start;
			length = count;
		}
		// Display linked list elements
		cout << " Linked List \n";
		this->display();
		cout << " Longest increasing sublist is \n";
		// Display increasing sublist
		while (result != NULL && length > 0)
		{
			// Display node value
			cout << " " << result->data << " →";
			// Visit to next node
			result = result->next;
			length--;
		}
		cout << " NULL\n";
	}
};
int main()
{
	// Create a empty linked list
	SingleLL sll = SingleLL();
	//  Constructed linked list
	//  11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
	sll.addNode(11);
	sll.addNode(12);
	sll.addNode(-2);
	sll.addNode(3);
	sll.addNode(5);
	sll.addNode(9);
	sll.addNode(12);
	sll.addNode(4);
	sll.addNode(7);
	sll.addNode(8);
	sll.increasingSublist();
	return 0;
}

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL
// Include namespace system
using System;
/*
  C# Program for
  Longest increasing sublist in a linked list
*/
// Linked list node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	//Add new Node at end of linked list
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			Console.Write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		Console.Write(" NULL\n");
	}
	// Finding and print longest increasing sublist
	public void increasingSublist()
	{
		if (this.head == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		// Define some useful variables
		LinkNode result = this.head;
		LinkNode auxiliary = this.head;
		LinkNode start = null;
		// Define some counter variables
		int count = 1;
		int length = 1;
		// iterating linked list element
		while (auxiliary.next != null)
		{
			if (auxiliary.data < auxiliary.next.data)
			{
				count++;
			}
			else
			{
				if (count > length)
				{
					// When a new increasing sublist is found
					// Get the starting node of sublist
					result = start;
					// Update the sublist length
					length = count;
				}
				count = 1;
				start = auxiliary.next;
			}
			if (start == null)
			{
				start = auxiliary;
			}
			// Visit to next node
			auxiliary = auxiliary.next;
		}
		// Test last sublist
		if (count > length)
		{
			// When last pair of sublist larger
			result = start;
			length = count;
		}
		// Display linked list elements
		Console.Write(" Linked List \n");
		display();
		Console.Write(" Longest increasing sublist is \n");
		// Display increasing sublist
		while (result != null && length > 0)
		{
			// Display node value
			Console.Write(" " + result.data + " →");
			// Visit to next node
			result = result.next;
			length--;
		}
		Console.Write(" NULL\n");
	}
	public static void Main(String[] args)
	{
		// Create a empty linked list
		SingleLL sll = new SingleLL();
		//  Constructed linked list
		//  11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
		sll.addNode(11);
		sll.addNode(12);
		sll.addNode(-2);
		sll.addNode(3);
		sll.addNode(5);
		sll.addNode(9);
		sll.addNode(12);
		sll.addNode(4);
		sll.addNode(7);
		sll.addNode(8);
		sll.increasingSublist();
	}
}

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL
<?php
/*
  Php Program for
  Longest increasing sublist in a linked list
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;

	function __construct($data)
	{
		$this->data = $data;
		$this->next = null;
	}
}
class SingleLL
{
	public $head;
	public $tail;

	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	//Add new Node at end of linked list
	public	function addNode($data)
	{
		$node = new LinkNode($data);
		if ($this->head == null)
		{
			$this->head = $node;
		}
		else
		{
			// Append the node at last position
			$this->tail->next = $node;
		}
		$this->tail = $node;
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == null)
		{
			echo "\n Empty linked list\n";
			return;
		}
		$temp = $this->head;
		//iterating linked list elements
		while ($temp != null)
		{
			echo " ". $temp->data ." →";
			// Visit to next node
			$temp = $temp->next;
		}
		echo " NULL\n";
	}
	// Finding and print longest increasing sublist
	public	function increasingSublist()
	{
		if ($this->head == null)
		{
			echo "\n Empty linked list\n";
			return;
		}
		// Define some useful variables
		$result = $this->head;
		$auxiliary = $this->head;
		$start = null;
		// Define some counter variables
		$count = 1;
		$length = 1;
		// iterating linked list element
		while ($auxiliary->next != null)
		{
			if ($auxiliary->data < $auxiliary->next->data)
			{
				$count++;
			}
			else
			{
				if ($count > $length)
				{
					// When a new increasing sublist is found
					// Get the starting node of sublist
					$result = $start;
					// Update the sublist length
					$length = $count;
				}
				$count = 1;
				$start = $auxiliary->next;
			}
			if ($start == null)
			{
				$start = $auxiliary;
			}
			// Visit to next node
			$auxiliary = $auxiliary->next;
		}
		// Test last sublist
		if ($count > $length)
		{
			// When last pair of sublist larger
			$result = $start;
			$length = $count;
		}
		// Display linked list elements
		echo " Linked List \n";
		$this->display();
		echo " Longest increasing sublist is \n";
		// Display increasing sublist
		while ($result != null && $length > 0)
		{
			// Display node value
			echo " ". $result->data ." →";
			// Visit to next node
			$result = $result->next;
			$length--;
		}
		echo " NULL\n";
	}
}

function main()
{
	// Create a empty linked list
	$sll = new SingleLL();
	//  Constructed linked list
	//  11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
	$sll->addNode(11);
	$sll->addNode(12);
	$sll->addNode(-2);
	$sll->addNode(3);
	$sll->addNode(5);
	$sll->addNode(9);
	$sll->addNode(12);
	$sll->addNode(4);
	$sll->addNode(7);
	$sll->addNode(8);
	$sll->increasingSublist();
}
main();

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL
/*
  Node Js Program for
  Longest increasing sublist in a linked list
*/
// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	//Add new Node at end of linked list
	addNode(data)
	{
		var node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	display()
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		var temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			process.stdout.write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" NULL\n");
	}
	// Finding and print longest increasing sublist
	increasingSublist()
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		// Define some useful variables
		var result = this.head;
		var auxiliary = this.head;
		var start = null;
		// Define some counter variables
		var count = 1;
		var length = 1;
		// iterating linked list element
		while (auxiliary.next != null)
		{
			if (auxiliary.data < auxiliary.next.data)
			{
				count++;
			}
			else
			{
				if (count > length)
				{
					// When a new increasing sublist is found
					// Get the starting node of sublist
					result = start;
					// Update the sublist length
					length = count;
				}
				count = 1;
				start = auxiliary.next;
			}
			if (start == null)
			{
				start = auxiliary;
			}
			// Visit to next node
			auxiliary = auxiliary.next;
		}
		// Test last sublist
		if (count > length)
		{
			// When last pair of sublist larger
			result = start;
			length = count;
		}
		// Display linked list elements
		process.stdout.write(" Linked List \n");
		this.display();
		process.stdout.write(" Longest increasing sublist is \n");
		// Display increasing sublist
		while (result != null && length > 0)
		{
			// Display node value
			process.stdout.write(" " + result.data + " →");
			// Visit to next node
			result = result.next;
			length--;
		}
		process.stdout.write(" NULL\n");
	}
}

function main()
{
	// Create a empty linked list
	var sll = new SingleLL();
	//  Constructed linked list
	//  11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
	sll.addNode(11);
	sll.addNode(12);
	sll.addNode(-2);
	sll.addNode(3);
	sll.addNode(5);
	sll.addNode(9);
	sll.addNode(12);
	sll.addNode(4);
	sll.addNode(7);
	sll.addNode(8);
	sll.increasingSublist();
}
main();

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL
#  Python 3 Program for
#  Longest increasing sublist in a linked list

#  Linked list node
class LinkNode :
	
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class SingleLL :
	
	def __init__(self) :
		self.head = None
		self.tail = None
	
	# Add new Node at end of linked list 
	def addNode(self, data) :
		node = LinkNode(data)
		if (self.head == None) :
			self.head = node
		else :
			#  Append the node at last position
			self.tail.next = node
		
		self.tail = node
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		temp = self.head
		# iterating linked list elements
		while (temp != None) :
			print("", temp.data ,"→", end = "")
			#  Visit to next node
			temp = temp.next
		
		print(" NULL")
	
	#  Finding and print longest increasing sublist
	def increasingSublist(self) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		#  Define some useful variables 
		result = self.head
		auxiliary = self.head
		start = None
		#  Define some counter variables
		count = 1
		length = 1
		#  iterating linked list element
		while (auxiliary.next != None) :
			if (auxiliary.data < auxiliary.next.data) :
				count += 1
			else :
				if (count > length) :
					#  When a new increasing sublist is found
					#  Get the starting node of sublist
					result = start
					#  Update the sublist length
					length = count
				
				count = 1
				start = auxiliary.next
			
			if (start == None) :
				start = auxiliary
			
			#  Visit to next node
			auxiliary = auxiliary.next
		
		#  Test last sublist
		if (count > length) :
			#  When last pair of sublist larger
			result = start
			length = count
		
		#  Display linked list elements
		print(" Linked List ")
		self.display()
		print(" Longest increasing sublist is ")
		#  Display increasing sublist
		while (result != None and length > 0) :
			#  Display node value
			print("", result.data ,"→", end = "")
			#  Visit to next node
			result = result.next
			length -= 1
		
		print(" NULL")
	

def main() :
	#  Create a empty linked list
	sll = SingleLL()
	#   Constructed linked list
	#   11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
	sll.addNode(11)
	sll.addNode(12)
	sll.addNode(-2)
	sll.addNode(3)
	sll.addNode(5)
	sll.addNode(9)
	sll.addNode(12)
	sll.addNode(4)
	sll.addNode(7)
	sll.addNode(8)
	sll.increasingSublist()

if __name__ == "__main__": main()

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL
#   Ruby Program for
#   Longest increasing sublist in a linked list

#  Linked list node
class LinkNode  
	# Define the accessor and reader of class LinkNode  
	attr_reader :data, :next
	attr_accessor :data, :next
 
	
	def initialize(data) 
		self.data = data
		self.next = nil
	end

end

class SingleLL  
	# Define the accessor and reader of class SingleLL  
	attr_reader :head, :tail
	attr_accessor :head, :tail
 
	
	def initialize() 
		self.head = nil
		self.tail = nil
	end

	# Add new Node at end of linked list 
	def addNode(data) 
		node = LinkNode.new(data)
		if (self.head == nil) 
			self.head = node
		else 
			#  Append the node at last position
			self.tail.next = node
		end

		self.tail = node
	end

	#  Display linked list element
	def display() 
		if (self.head == nil) 
			print("\n Empty linked list\n")
			return
		end

		temp = self.head
		# iterating linked list elements
		while (temp != nil) 
			print(" ", temp.data ," →")
			#  Visit to next node
			temp = temp.next
		end

		print(" NULL\n")
	end

	#  Finding and print longest increasing sublist
	def increasingSublist() 
		if (self.head == nil) 
			print("\n Empty linked list\n")
			return
		end

		#  Define some useful variables 
		result = self.head
		auxiliary = self.head
		start = nil
		#  Define some counter variables
		count = 1
		length = 1
		#  iterating linked list element
		while (auxiliary.next != nil) 
			if (auxiliary.data < auxiliary.next.data) 
				count += 1
			else 
				if (count > length) 
					#  When a new increasing sublist is found
					#  Get the starting node of sublist
					result = start
					#  Update the sublist length
					length = count
				end

				count = 1
				start = auxiliary.next
			end

			if (start == nil) 
				start = auxiliary
			end

			#  Visit to next node
			auxiliary = auxiliary.next
		end

		#  Test last sublist
		if (count > length) 
			#  When last pair of sublist larger
			result = start
			length = count
		end

		#  Display linked list elements
		print(" Linked List \n")
		self.display()
		print(" Longest increasing sublist is \n")
		#  Display increasing sublist
		while (result != nil && length > 0) 
			#  Display node value
			print(" ", result.data ," →")
			#  Visit to next node
			result = result.next
			length -= 1
		end

		print(" NULL\n")
	end

end

def main() 
	#  Create a empty linked list
	sll = SingleLL.new()
	#   Constructed linked list
	#   11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
	sll.addNode(11)
	sll.addNode(12)
	sll.addNode(-2)
	sll.addNode(3)
	sll.addNode(5)
	sll.addNode(9)
	sll.addNode(12)
	sll.addNode(4)
	sll.addNode(7)
	sll.addNode(8)
	sll.increasingSublist()
end

main()

Output

 Linked List 
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is 
 -2 → 3 → 5 → 9 → 12 → NULL
/*
  Scala Program for
  Longest increasing sublist in a linked list
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class SingleLL(var head: LinkNode , var tail: LinkNode)
{
	def this()
	{
		this(null, null);
	}
	//Add new Node at end of linked list
	def addNode(data: Int): Unit = {
		var node: LinkNode = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// Finding and print longest increasing sublist
	def increasingSublist(): Unit = {
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		// Define some useful variables
		var result: LinkNode = this.head;
		var auxiliary: LinkNode = this.head;
		var start: LinkNode = null;
		// Define some counter variables
		var count: Int = 1;
		var length: Int = 1;
		// iterating linked list element
		while (auxiliary.next != null)
		{
			if (auxiliary.data < auxiliary.next.data)
			{
				count += 1;
			}
			else
			{
				if (count > length)
				{
					// When a new increasing sublist is found
					// Get the starting node of sublist
					result = start;
					// Update the sublist length
					length = count;
				}
				count = 1;
				start = auxiliary.next;
			}
			if (start == null)
			{
				start = auxiliary;
			}
			// Visit to next node
			auxiliary = auxiliary.next;
		}
		// Test last sublist
		if (count > length)
		{
			// When last pair of sublist larger
			result = start;
			length = count;
		}
		// Display linked list elements
		print(" Linked List \n");
		this.display();
		print(" Longest increasing sublist is \n");
		// Display increasing sublist
		while (result != null && length > 0)
		{
			// Display node value
			print(" " + result.data + " →");
			// Visit to next node
			result = result.next;
			length -= 1;
		}
		print(" NULL\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create a empty linked list
		var sll: SingleLL = new SingleLL();
		//  Constructed linked list
		//  11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
		sll.addNode(11);
		sll.addNode(12);
		sll.addNode(-2);
		sll.addNode(3);
		sll.addNode(5);
		sll.addNode(9);
		sll.addNode(12);
		sll.addNode(4);
		sll.addNode(7);
		sll.addNode(8);
		sll.increasingSublist();
	}
}

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL
/*
  Swift 4 Program for
  Longest increasing sublist in a linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	//Add new Node at end of linked list
	func addNode(_ data: Int)
	{
		let node: LinkNode? = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			// Append the node at last position
			self.tail!.next = node;
		}
		self.tail = node;
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		var temp: LinkNode? = self.head;
		//iterating linked list elements
		while (temp  != nil)
		{
			print("", temp!.data ,"→", terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" NULL");
	}
	// Finding and print longest increasing sublist
	func increasingSublist()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		// Define some useful variables
		var result: LinkNode? = self.head;
		var auxiliary: LinkNode? = self.head;
		var start: LinkNode? = nil;
		// Define some counter variables
		var count: Int = 1;
		var length: Int = 1;
		// iterating linked list element
		while (auxiliary!.next  != nil)
		{
			if (auxiliary!.data < auxiliary!.next!.data)
			{
				count += 1;
			}
			else
			{
				if (count > length)
				{
					// When a new increasing sublist is found
					// Get the starting node of sublist
					result = start;
					// Update the sublist length
					length = count;
				}
				count = 1;
				start = auxiliary!.next;
			}
			if (start == nil)
			{
				start = auxiliary;
			}
			// Visit to next node
			auxiliary = auxiliary!.next;
		}
		// Test last sublist
		if (count > length)
		{
			// When last pair of sublist larger
			result = start;
			length = count;
		}
		// Display linked list elements
		print(" Linked List ");
		self.display();
		print(" Longest increasing sublist is ");
		// Display increasing sublist
		while (result  != nil && length > 0)
		{
			// Display node value
			print("", result!.data ,"→", terminator: "");
			// Visit to next node
			result = result!.next;
			length -= 1;
		}
		print(" NULL");
	}
}
func main()
{
	// Create a empty linked list
	let sll: SingleLL = SingleLL();
	//  Constructed linked list
	//  11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
	sll.addNode(11);
	sll.addNode(12);
	sll.addNode(-2);
	sll.addNode(3);
	sll.addNode(5);
	sll.addNode(9);
	sll.addNode(12);
	sll.addNode(4);
	sll.addNode(7);
	sll.addNode(8);
	sll.increasingSublist();
}
main();

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL
/*
  Kotlin Program for
  Longest increasing sublist in a linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	var head: LinkNode ? ;
	var tail: LinkNode ? ;
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	//Add new Node at end of linked list
	fun addNode(data: Int): Unit
	{
		var node: LinkNode ? = LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail?.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	fun display(): Unit
	{
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode ? = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// Finding and print longest increasing sublist
	fun increasingSublist(): Unit
	{
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		// Define some useful variables
		var result: LinkNode ? = this.head;
		var auxiliary: LinkNode ? = this.head;
		var start: LinkNode ? = null;
		// Define some counter variables
		var count: Int = 1;
		var length: Int = 1;
		// iterating linked list element
		while (auxiliary?.next != null)
		{
			if (auxiliary.data < auxiliary.next!!.data)
			{
				count += 1;
			}
			else
			{
				if (count > length)
				{
					// When a new increasing sublist is found
					// Get the starting node of sublist
					result = start;
					// Update the sublist length
					length = count;
				}
				count = 1;
				start = auxiliary.next;
			}
			if (start == null)
			{
				start = auxiliary;
			}
			// Visit to next node
			auxiliary = auxiliary.next;
		}
		// Test last sublist
		if (count > length)
		{
			// When last pair of sublist larger
			result = start;
			length = count;
		}
		// Display linked list elements
		print(" Linked List \n");
		this.display();
		print(" Longest increasing sublist is \n");
		// Display increasing sublist
		while (result != null && length > 0)
		{
			// Display node value
			print(" " + result.data + " →");
			// Visit to next node
			result = result.next;
			length -= 1;
		}
		print(" NULL\n");
	}
}
fun main(args: Array < String > ): Unit
{
	// Create a empty linked list
	var sll: SingleLL = SingleLL();
	//  Constructed linked list
	//  11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 →NULL
	sll.addNode(11);
	sll.addNode(12);
	sll.addNode(-2);
	sll.addNode(3);
	sll.addNode(5);
	sll.addNode(9);
	sll.addNode(12);
	sll.addNode(4);
	sll.addNode(7);
	sll.addNode(8);
	sll.increasingSublist();
}

Output

 Linked List
 11 → 12 → -2 → 3 → 5 → 9 → 12 → 4 → 7 → 8 → NULL
 Longest increasing sublist is
 -2 → 3 → 5 → 9 → 12 → NULL


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved