Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Insert Operation LinkedList (http://www.go4expert.com/forums/insert-operation-linkedlist-t3908/)

shah123 17Apr2007 15:39

Insert Operation LinkedList
 
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 17Apr2007 18:55

Re: Insert Operation LinkedList
 
If you are using the ArrayList you can direct use its Add member to insert anything into the list dynamically.

shah123 17Apr2007 19:05

Re: Insert Operation LinkedList
 
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 17Apr2007 22:06

Re: Insert Operation LinkedList
 
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 17Apr2007 23:19

Re: Insert Operation LinkedList
 
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 17Apr2007 23:28

Re: Insert Operation LinkedList
 
Have the loop like this
Code:

while (true)
{
    str = Console.ReadLine();
    if(str == "-1")
        break;
    llist.Insert(str);
}


shah123 18Apr2007 13:30

Re: Insert Operation LinkedList
 
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 18Apr2007 13:41

Re: Insert Operation LinkedList
 
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 18Apr2007 14:56

Re: Insert Operation LinkedList
 
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 18Apr2007 20:12

Re: Insert Operation LinkedList
 
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.


All times are GMT +5.5. The time now is 19:56.