How can I insert a node into the middle of a singly linked list in c#?
-
So I have a project program which requires me to be able to insert a node at a certain location in a singly linked list. Curently I have two major classes and a test class with main The other classes are in seperate files and were provided as stock items, below is the SinglyLinkedList class I currently have, AddHead and AddTail work (Ignore the stubs, I'll fill them later) I'm wondering how I need to format the AddMiddle class.. SinglyLinkedList.cs =============== using System; namespace SinglyLinkedList { public class SinglyLinkedList<T> { private int _size; private Node<T> _head; private Node<T> _tail; public SinglyLinkedList() { _size = 0; _head = null; _tail = null; } // Add a node to the singly linked list public void AddTail(T nodeValue) { Node<T> n = new Node<T>(nodeValue); if (_size < 1) { _head = n; _tail = n; ++_size; } else { _tail.NextNode = n; _tail = n; ++_size; } } public void AddHead(T nodeValue) { //save current head to temp Node<T> temp = this._head; //store new head value Node<T> newhead = new Node<T>(nodeValue); //replace head with new head value this._head = newhead; //make the next value equal the old head _head.NextNode = temp; //increase list size _size++; } public bool AddMiddle(int nodeNum, T nodeValue) { Node<T> _current = new Node<T>(nodeValue); if (nodeNum == 1) { AddHead(nodeValue); return true; } else if (nodeNum > _size) { AddTail(nodeValue); return true; } else if (nodeNum > 0 && nodeNum < _size) { for(int i = 0; i < nodeNum; i++) { this._head = _head.NextNode; } Node<T> temp = this._head; AddTail(nodeValue); _tail.NextNode = temp; return true; } else return false; } // Node at head of singly linked list public Node<T> Head { get { return _head; } } // Node at tail of singly linked list public Node<T> Tail { get { return _tail; } } // Number of nodes in singly linked list public int Size { get { return _size; } } // Traverse all nodes in Singly Linked List public void TraverseSinglyLinkedList(Node<T> node) { // base cases // no nodes in list if (_size < 1) { //throw new SinglyLinkedListException Console.WriteLine("There are no nodes in the singly linked list."); } // only one node in list else if (_size == 1) { Console.WriteLine(Head.NodeValue); } // on the last node in the list else if (node.NextNode == null) { Console.WriteLine(node.NodeValue); } // recursive case, do it all again passing the nextNode // of the current node as the param else { Console.WriteLine(node.NodeValue); TraverseSinglyLinkedList(node.NextNode); } } public bool SearchList(T SearchValue) { int found = 0; if (found > -1) { return true; } else return false; } public void DeleteNode(int nodeNum) { } public void AddLarger(int nodeNum, T nodeValue) { } } }
-
Answer:
You have the right idea. One issue I'm seeing is that you're resetting the head with every iteration of the for loop, so although you're able to insert the new node, the original head becomes incorrect. --edit The logic seems correct aside from not resetting the head to point back to the beginning. You have the right idea: iterate the list until you reach the n-1th item, create a new node and set the n-1th tail to equal new node, set new node's tail to equal nth node. Make sure to create a separate variable that keeps track of the original head, so that you can set this._head back to the original.
Thacious at Yahoo! Answers Visit the source
Related Q & A:
- How can I remove a contact from a list in my address book?Best solution by windows.microsoft.com
- How can I insert a picture in Email without attaching it?Best solution by Super User
- How can I find a list of births in Wiesbaden, Germany?Best solution by Yahoo! Answers
- How can I stop a player in chess from freezing my game when I am winning in the middle or at the end of a game?Best solution by chess.com
- How can I get a job at a resort for a Summer?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.