Insert In Order, Generic LinkedList
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Insert In Order, Generic LinkedList

  1. #1
    Join Date
    Dec 2013
    Posts
    2

    Insert In Order, Generic LinkedList

    Hey guys, I've been stuck on this for a few days now - I've practically given up!

    I have coded a generic linked list console application that you can add, remove, append, etc. But I've had soooo much trouble with 'Insert in Order'.

    Heres my code:

    LinkListGen.cs:
    Code:
              public void AddItem(T item)
            {
                list = new LinkGen<T>(item, list);
            }
    
          public void InsertInOrder(T item)
            {
                // is list in non-descending 
    
                LinkGen<T> temp = list;
                LinkListGen<T> newList = new LinkListGen<T>();
                if (list == null)
                {
                    AddItem(item);
                }
                else
                {
                    while (item.CompareTo(temp.Data) > 0)
                    {
                        temp = temp.Next;
                        AddItem(temp.Data);
                    }
    
                    LinkGen<T> temp2 = new LinkGen<T>(item);
    
                    temp2.Next = temp.Next;
                    temp.Next = temp2;
    
                }
    LinkGen.cs:
    Code:
    class LinkGen<T>
        {
            private T data;
            private LinkGen<T> next;
            private LinkGen<T> tailList;
            private LinkGen<T> headList;
    
            public LinkGen(T item)
            {
                data = item;
                next = null;
            }
    
            public LinkGen(T item, LinkGen<T> list)
            {
                data = item;
                next = list;
            }
    
            public T Data
            {
                set { this.data = value; }
                get { return this.data; }
            }
    
            public LinkGen<T> Next
            {
                set { this.next = value; }
                get { return this.next; }
            }
    
            public LinkGen<T> TailList
            {
                get { return tailList; }
            }
    
            public LinkGen<T> HeadList
            {
                get { return headList; }
            }
        }
    Can somebody help me fix it? I've been looking at it for days now, and sometimes it's like I just completely forget what parts of the code do what after so long. Also, I'm pretty sure some/part of this is wrong because I was told to use HeadList and TailList but I just don't know how, and seem to be alright without it.

    My deadlines Friday, and since I've missed so much uni due to being at home as my mums ill I think I'm gunna fail. I really need a help guys, if you wouldn't mind. This really has got me stumped.

  2. #2
    Join Date
    Jan 2014
    Posts
    3

    Re: Insert In Order, Generic LinkedList

    Your question is really a bit difficult, to insert in order is somehow complex. I've found something about the Generic List in C#. Hope it can be some kind of help.

    Code:
    public class MyObject
    {
        public MyObject(int myInt, string myString)
        {
            _myInt = myInt;
            _myString = myString;
        }
        private int _myInt;
        public int MyInt
        {
            get { return _myInt; }
            set { _myInt = value; }
        }
        private string _myString;
        public string MyString
        {
            get { return _myString; }
            set { _myString = value; }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center