data structures - Can we assign address of two nodes as a next of a one LinkedListNode in C#? -
i solving 1 program linked list data structure in c#, need check given linked list null terminated or ends cycle. want check different test cases, not able pass cyclic linked list input.
how pass cyclic linked list input?
problem hackerrank give idea, trying achieve?
here code achieve linked list showed in image
private static linkedlist<int> initializelinkedlist () { linkedlist<int> linkedlist = new linkedlist<int>(); linkedlistnode<int> item1 = new linkedlistnode<int>(1); linkedlistnode<int> item2 = new linkedlistnode<int>(2); linkedlistnode<int> item3 = new linkedlistnode<int>(3); linkedlistnode<int> item4 = new linkedlistnode<int>(4); linkedlistnode<int> item5 = new linkedlistnode<int>(5); linkedlistnode<int> item6 = new linkedlistnode<int>(6); linkedlist.addlast(item1); linkedlist.addlast(item2); linkedlist.addlast(item3); linkedlist.addlast(item4); linkedlist.addlast(item5); linkedlist.addafter(item3, item6); return linkedlist; }
it's impossible create cycle, linkedlist<> built linkedlistnode<> these node contain item value list, next , previous used navigating, these members have internal setters can't manually assign these. these set when call addfirst
, addlast
, addafter
or addbefore
on linkedlist.
in theory set first , last same linkedlistnode
var item1 = new linkedlistnode<string>("one"); var item2 = new linkedlistnode<string>("two"); var item3 = new linkedlistnode<string>("three"); var list = new linkedlist<string>(); list.addfirst(item1); list.addafter(item1, item2); list.addafter(item2, item3); list.addlast(item1);
but if try following we'll "unhandled exception: system.invalidoperationexception: linkedlist node belongs linkedlist." exception
Comments
Post a Comment