Insert Operation LinkedList

Discussion in 'C#' started by shah123, Apr 17, 2007.

  1. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    If you are using the ArrayList you can direct use its Add member to insert anything into the list dynamically.
     
  3. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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++;
            }
    
    
    
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  5. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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);
                   
                }
    
                   
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Have the loop like this
    Code:
    while (true) 
    {
        str = Console.ReadLine();
        if(str == "-1")
            break;
        llist.Insert(str);
    }
     
  7. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  9. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  11. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Have separate thread with appropriate title for your separate queries as that helps when some one else is finding something.
     
  13. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    Thanks. I will put in separate thread.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice