Skip to main content

Fold linked list in swift

Swift program for Fold linked list. Here more information.

import Foundation
/*
  Swift 4 program for
  Fold 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 node at the beginning
	func addNode(_ data: Int)
	{
		// Create new node
		let node: LinkNode? = LinkNode(data);
		node!.next = self.head;
		// Set new head
		self.head = node;
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			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");
	}
	// This is handle the request of 
	// folding given linked list nodes
	func foldList()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		// Some auxiliary variables
		var temp: LinkNode? = self.head;
		var auxiliary: LinkNode? = self.head;
		var hold: LinkNode? = nil;
		// Loops used to find the middle node
		while (temp  != nil && 
          temp!.next  != nil && 
          temp!.next!.next  != nil)
		{
			// Visit to next node
			auxiliary = auxiliary!.next;
			// Visit to second next node
			temp = temp!.next!.next;
		}
		if (auxiliary  != nil)
		{
			hold = auxiliary;
			auxiliary = auxiliary!.next;
			// Separating half nodes
			hold!.next = nil;
			// Start with first node of next half section
			temp = auxiliary;
			auxiliary = nil;
			// Reverse second half nodes 
			while (temp  != nil)
			{
				hold = temp;
				temp = temp!.next;
				hold!.next = auxiliary;
				auxiliary = hold;
			}
			// Start with first node of linked list
			temp = self.head;
			// Combine first and second half nodes 
			// in alternate manner
			while (temp  != nil)
			{
				hold = temp;
				temp = temp!.next;
				hold!.next = auxiliary;
				if (auxiliary  != nil)
				{
					hold = auxiliary;
					auxiliary = auxiliary!.next;
					hold!.next = temp;
				}
			}
		}
	}
	static func main()
	{
		// Create a empty linked lists
		let sll1: SingleLL = SingleLL();
		let sll2: SingleLL = SingleLL();
		//  Constructed first linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
		sll1.addNode(8);
		sll1.addNode(7);
		sll1.addNode(6);
		sll1.addNode(5);
		sll1.addNode(4);
		sll1.addNode(3);
		sll1.addNode(2);
		sll1.addNode(1);
		// Constructed second linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
		sll2.addNode(7);
		sll2.addNode(6);
		sll2.addNode(5);
		sll2.addNode(4);
		sll2.addNode(3);
		sll2.addNode(2);
		sll2.addNode(1);
		// Test A
		print(" Before folding");
		sll1.display();
		// Form of folding
		//      1 → 2 → 3 → 4 ──┐
		// NULL ← 8 ← 7 ← 6 ← 5⤶
		sll1.foldList();
		// After merging
		// 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
		print(" After folding");
		sll1.display();
		// Test B
		print(" Before folding");
		sll2.display();
		// Form of folding
		//      1 → 2 → 3 → 4 
		// NULL ← 7 ← 6 ← 5⤶
		sll2.foldList();
		// After merging
		// 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
		print(" After folding");
		sll2.display();
	}
}
SingleLL.main();

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL




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