/// <summary> /// Adds the node at first into linked list. /// </summary> /// <param name="list1">The list1 is an instance of the Linked List.</param> /// <param name="list">The list.</param> /// <param name="data">The data.</param> /// <returns>updated list</returns> public LinkedList1 <T> AddFirstIntoLinkedList(LinkedList1 <T> list1, List <T> list, T data) { NewNode <T> newNode = new NewNode <T>(); newNode.NodeData = data; newNode.Next = null; try { if (list1.Head == null) { list1.Head = newNode; return(list1); } else { newNode.Next = list1.Head; list1.Head = newNode; } } catch (Exception e) { Console.WriteLine(e.Message); } return(list1); }
/// <summary> /// Adds the last node into linked list. /// </summary> /// <param name="list">The list.</param> /// <param name="list1">The list1.</param> /// <param name="data">The data.</param> /// <returns>updated list</returns> public LinkedList1 <T> AddLastIntoLinkedList(LinkedList1 <T> list, List <T> list1, T data) { NewNode <T> newNode = new NewNode <T>(); newNode.NodeData = data; newNode.Next = null; try { if (list.Head == null) { list.Head = newNode; } else { //// The current node is temporary node for traversing into Linked List NewNode <T> currentNode = list.Head; // Traverse till the end of the list.... while (currentNode.Next != null) { currentNode = currentNode.Next; } // Add new node as the Next node to the last node. currentNode.Next = newNode; } } catch (Exception e) { Console.WriteLine(e.Message); } return(list); }
/// <summary> /// Reads the data from linked list into list. /// </summary> /// <param name="list">The list.</param> /// <returns>list containing node data</returns> public List <T> ReadIntoList(LinkedList1 <T> list1) { NewNode <T> currentNode = this.Head; while (currentNode != null) { list.Add(currentNode.NodeData); } return(list); }
/// <summary> /// Insert elements into the queue. /// </summary> /// <param name="list1">The list1 is the list of element.</param> /// <param name="number">The number to be inserted into queue.</param> /// <returns>updated List</returns> public LinkedList1 <T> EnQueue(List <T> list1, T number) { try { ////Add the data into the linked list this.list = this.list.AddLastIntoLinkedList(this.list, list1, number); } catch (Exception e) { Console.WriteLine(e.Message); } return(this.list); }
/// <summary> /// Removes elements from the queue. /// </summary> /// <param name="list1">The list1 is the instance of the linked list.</param> /// <param name="list">The list is the list of elements.</param> /// <returns>updated linked list</returns> public LinkedList1 <T> DeQueue(LinkedList1 <T> list1, List <T> list) { try { T number; ////remove top element from the linked list number = list1.RemoveFirstLinkedList(); //// add that removed node from list1.AddFirstIntoLinkedList(list1, list, number); } catch (Exception e) { Console.WriteLine(e.Message); } return(list1); }