943,733 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1455
  • C# RSS
Nov 28th, 2007
0

Need Quick Help LinkList

Expand Post »
I am having trouble with printing out the number to the console. Instead of 10 printing out I want 16. How can this be fix.
  

using System;
using System.Collections.Generic;
using System.Text;

namespace LinkList
{
    class Program
    {
        static void Main(string[] args)
        {
            List studentRecord = new List();
            studentRecord.InsertAtFront(10);
            studentRecord.InsertAtFront(16);
            studentRecord.InsertAtBack(32);

            studentRecord.PrintFromLeftToRight();
        }
    }

    public class ListNode
    {
        private object data;
        private ListNode next;
        private ListNode prev;

        public ListNode(object dataValue, ListNode n, ListNode p)
        {
            data = dataValue;
            next = n;
            prev = p;
        } // end constructor

        public ListNode Next
        {
            get { return next; }
            set { next = value; }
        } // end property Next

        public ListNode Prev
        {
            get { return prev; }
            set { next = value; }
        } // end property Prev

        public object Data
        {
            get { return data; }
            set { data = value; }
        } // end property Data
    } // end class ListNode

    public class List
    {
        private ListNode firstNode;
        private ListNode lastNode;
        ListNode temporary;

        public List()
        {
            firstNode = lastNode = null;
        } // end constructor List

        public ListNode FirstNode
        {
            get { return firstNode; }
            set { firstNode = value; }
        }

        public ListNode LastNode
        {
            get { return LastNode; }
            set { LastNode = value; }
        }

        public void InsertAtFront(object insertItem)
        {
            if (firstNode == null)     // link list is empty
                firstNode = lastNode = new ListNode(insertItem, null, null);
            else
                firstNode = new ListNode(insertItem, firstNode, null);
        } // end method InsertAtFront

        public void InsertAtBack(object insertItem)
        {
            if (firstNode == null)
                firstNode = lastNode = new ListNode(insertItem, null, null);
            else
                firstNode = new ListNode(insertItem, null, lastNode);
        } // end method InsertAtBack

        public void PrintFromLeftToRight()
        {
            if (firstNode == null)
            {
                Console.WriteLine("Link List is empty.");
                return;
            } // end if
            else
            {
                ListNode currentNode = lastNode;
                Console.WriteLine("{0,4}", currentNode.Data);
            } // end else
        } // end method PrintFromLeftToRight
    } // end class List
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wkid87 is offline Offline
2 posts
since Oct 2007
Nov 28th, 2007
0

Re: Need Quick Help LinkList

I found an error I think you should fix. Last line in InsertAtBack, shouldn't you be modifying lastNode and not firstNode? After that the linked-list works as it should, except it will print 32 when you call printfromlefttoright. That is because in that method you told it to print lastNode.

HTH
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Dec 1st, 2007
0

Re: Need Quick Help LinkList

Also there doesn't seem to be any funcionality in your linkedlist that isn't present already in the linkedlists the .NET framework has to offer. Unless you're doing it juts purely for a learning experience.
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Dec 1st, 2007
0

Re: Need Quick Help LinkList

His list supports mixed data types
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: 2D graphic
Next Thread in C# Forum Timeline: ComboBox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC