Delete a node from linked list specific position in swift

Swift program for Delete a node from linked list specific position. Here problem description and explanation.

import Foundation
// Swift 4 Program for
// Delete node at given index 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? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	// Add new node at the end of linked list
	func addNode(_ value: Int)
	{
		// Create a new node
		let node: LinkNode? = LinkNode(value);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			self.tail!.next = node;
		}
		self.tail = 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");
	}
	// Delete node at given position
	func deleteAtIndex(_ index: Int)
	{
		var temp: LinkNode? = self.head;
		if (self.head == nil)
		{
			print("Empty linked list");
			return;
		}
		else if (index < 0)
		{
			print("Invalid positions");
			return;
		}
		else if (index == 1)
		{
			// Means deleting a first node
			if (self.tail === self.head)
			{
				// Only one node
				self.tail = nil;
			}
			// Make second node as head
			self.head = self.head!.next;
		}
		else
		{
			var count: Int = index - 1;
			// iterating linked list elements and find deleted node
			while (temp  != nil && count > 1)
			{
				// Visit to next node
				temp = temp!.next;
				count -= 1;
			}
			if (count == 1 && temp!.next  != nil)
			{
				// Get deleted node
				let n: LinkNode? = temp!.next;
				if (n === self.tail)
				{
					// When delete last node
					self.tail = temp;
				}
				// Unlink the deleting node
				temp!.next = n!.next;
			}
			else
			{
				print(" Index "
					+ String(index) + " are not present");
				return;
			}
		}
	}
	static func main(_ args: [String])
	{
		let sll: SingleLL? = SingleLL();
		// Test with your index
		// Note index start with 1
		let index: Int = 5;
		// Linked list
		// 10 → 20 → 30 → 40 → 50 → 60 → 70 → 80 → NULL
		sll!.addNode(10);
		sll!.addNode(20);
		sll!.addNode(30);
		sll!.addNode(40);
		sll!.addNode(50);
		sll!.addNode(60);
		sll!.addNode(70);
		sll!.addNode(80);
		print(" Before Delete Linked List");
		sll!.display();
		// Delete node at 5-th position
		sll!.deleteAtIndex(index);
		// Display linked list
		print(" After Delete Linked List");
		sll!.display();
	}
}
SingleLL.main([String]());

Output

 Before Delete Linked List
10 → 20 → 30 → 40 → 50 → 60 → 70 → 80 → NULL
 After Delete Linked List
10 → 20 → 30 → 40 → 60 → 70 → 80 → 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