Skip to main content

Move a specific occurrence to end of linked list in swift

Swift program for Move a specific occurrence to end of linked list. Here problem description and explanation.

import Foundation
// Swift 4 program for
// Move given occurrence to the end of 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? ;
	init()
	{
		self.head = nil;
	}
	// Add new node at the end of linked list
	func insert(_ value: Int)
	{
		// Create a node
		let node: LinkNode? = LinkNode(value);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			var temp: LinkNode? = self.head;
			// Find last node
			while (temp!.next  != nil)
			{
				// Visit to next node
				temp = temp!.next;
			}
			// Add node at last position
			temp!.next = node;
		}
	}
	// Display all Linked List elements
	func display()
	{
		if (self.head  != nil)
		{
			var temp: LinkNode? = self.head;
			while (temp  != nil)
			{
				// Display node value
				print(" ",temp!.data, terminator: " ");
				// Visit to next node
				temp = temp!.next;
			}
		}
		else
		{
			print("Empty Linked list");
		}
	}
	// Move all occurrence of given element into the 
	// end of linked list position
	func moveNodes(_ element: Int)
	{
		if (self.head == nil)
		{
			print("Empty linked list");
		}
		else
		{
			// Some useful auxiliary variables
			var temp: LinkNode? = self.head;
			var prev: LinkNode? = nil;
			var nodes: LinkNode? = nil;
			var current: LinkNode? = nil;
			// Move a particular occurrence to end of linked list
			while (temp  != nil)
			{
				current = temp;
				// Visit to next node
				temp = temp!.next;
				if (current!.data == element)
				{
					// Find move element
					if (self.head === current)
					{
						// Move the front node element 
						// to the end position
						self.head = temp;
					}
					if (prev  != nil)
					{
						// When have a intermediate node
						// Unlink the move node
						prev!.next = temp;
					}
					current!.next = nodes;
					nodes = current;
				}
				else
				{
					prev = current;
				}
			}
			if (self.head == nil && nodes  != nil)
			{
				// All same nodes are exist in linked list
				self.head = nodes;
			}
			else if (prev  != nil && nodes  != nil)
			{
				// Add occurrence to end
				prev!.next = nodes;
			}
			else
			{
				// When given occurrences element are not found
				print("Occurrences element are not found : "
					+ String(element));
			}
		}
	}
	static func main(_ args: [String])
	{
		let sll: SingleLL = SingleLL();
		// Linked list
		// 1 → 2 → 3 → 4 → 4 → 5 → 6 → 7 → NULL
		sll.insert(7);
		sll.insert(1);
		sll.insert(2);
		sll.insert(7);
		sll.insert(3);
		sll.insert(2);
		sll.insert(7);
		let occurrence: Int = 7;
		print("Before move occurrence "
			+ String(occurrence) + " to end");
		//display all node
		sll.display();
		print("\nAfter move occurrence "
			+ String(occurrence) + " to end");
		sll.moveNodes(occurrence);
		sll.display();
	}
}
SingleLL.main([String]());

Output

Before move occurrence 7 to end
  7   1   2   7   3   2   7
After move occurrence 7 to end
  1   2   3   2   7   7   7




Comment

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