Hi All, I am working on linked list application which the structure is as: Code: public static void Main(String[] args) { Console.WriteLine("Please Enter the data to be Inserted in the LinkList..."); Object obj = new Object(); node n = new node(); linklistImp llist = new linklistImp(); ArrayList ast = new ArrayList(); obj = Console.ReadLine(); llist.Insert(ast); } // to Insert the data public void Insert(ArrayList ast) { // to Check if node is empty if (head == null) { head = new node(); } This is just some basic structure of Linked List. My question is i want to insert the data on runtime from user entry and store. I am not sure completely what to use in main() to store the elements and how? Can you please help. Thanks
If you are using the ArrayList you can direct use its Add member to insert anything into the list dynamically.
Thats ok, When inserting how to make sure that how many elements you want to insert, some kind of finishing criteria that after some insertion should come out of insertion operation and display elements. I changed a code small bit. Code: while (str != "-1") // When i do that it also display -1 as well also when counting it count -1 as well. How to prevent that? { str = Console.ReadLine(); llist.Insert(str); //int i = Int32.Parse(str); } //ast = Int32.Parse(str); //llist.Insert(i); Console.WriteLine("\n"); Console.WriteLine("The total number of nodes in list are :" + llist.Count()); Console.WriteLine("\n"); Console.WriteLine("The data in nodes are : "); llist.display(); Console.ReadLine(); } // to Insert the data public void Insert(String data) { // to Check if node is empty if (head == null) { head = new node(); head.data = data; } else { // if node is not null node temp = new node(); temp.data = data; temp.next = head; head = temp; } totalnodes++; }
I am not sure what you are trying to do. You can call the ArrayList's Add method to add a single element or use the AddRange method to insert a series of element.
I have insert method as you can see and now through console i want to add different elements or objects to linked list. But for that i have one query which is Code: while (str != "-1") // Q. When i do that it also display -1 as well also when counting it count -1 as well. How to prevent that? This is the question here { str = Console.ReadLine(); llist.Insert(str); }
Have the loop like this Code: while (true) { str = Console.ReadLine(); if(str == "-1") break; llist.Insert(str); }
Thanks a million Shabbir. I learned until now a lot from you. One more query i am finished now with display and insert operation of linked list. When i am about to do remove and remove all methods should i do new thread?
Try doing in the same thread as that way you know what is happening when. Thread you may endup doing other things than the link list.
Hi, Can Anyone help me in pseudo code or algorithms to write InsertAt() method for LinkedList. Basically when this method will be called it will ask user where he want to put that data somewhere in the middle of nodes and then that data will be inserted in between the nodes. I am new to C# programming and learning different things everyday. Even if anybody can help me in defining structure for "InsertAt()" method. I have done Insert() and Display(). Any ideas. Please help.. Thanks
Again its already there in C# for you to use and I am not sure what you are looking for. Also I would suggest having different thread for different issues as that will help the person searching for similar things.
I have main method as: Code: Console.WriteLine("Please Enter the data to be Inserted in the LinkList..."); link lnk = new link(); String str = ""; while (true) { str = Console.ReadLine(); if(str.Equals("e")) break; add(str); } Console.WriteLine("The data in nodes are : "); display(); Console.WriteLine("Do you want to put the data at some specific position?"); String myString = Console.ReadLine(); Console.WriteLine("Enter element to put at specified position :"); String s = Console.ReadLine(); Int32 ind = Int32.Parse(s); insertAt(str, ind); When i try to insert the data at some specific position it inserts the element "e" at some random position and also it puts any required element on that position. e.g. Console: Do you want to put the data at some specific position? pos: 1 Console.WriteLine("Enter element to put at specified position :"); 3 So when it display the list it shows like as : 3 5 6 7 e 1 8 Why it is inserting "e" in the middle? Please advise. Thanks
Have separate thread with appropriate title for your separate queries as that helps when some one else is finding something.