Insert Operation LinkedList

shah123's Avatar
Contributor
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
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
If you are using the ArrayList you can direct use its Add member to insert anything into the list dynamically.
shah123's Avatar
Contributor
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++;
        }
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
shah123's Avatar
Contributor
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);
               
            }
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Have the loop like this
Code:
while (true) 
{
    str = Console.ReadLine();
    if(str == "-1")
        break;
    llist.Insert(str);
}
shah123's Avatar
Contributor
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?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by shah123
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.
shah123's Avatar
Contributor
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
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.