I want to implement that using C#'s Generic LinkedList and the new features of C# 3.0. But I'm stuck now. the code add's the polynomials with matching exponents but doesn't add the rest of the nodes to the new LinkedList.

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

namespace Polynomials
{
    class PolynomialExpression
    {
        public Int32 coefficient { get; set; }
        public Int32 exponent { get; set; }
        public override string ToString()
        {
            //return base.ToString();
            return ((exponent==0)?(coefficient).ToString():(coefficient + "x^" + exponent).ToString());
        }
    }

    class Program
    {
        public static LinkedList<PolynomialExpression> SortExponent(LinkedList<PolynomialExpression> a)
        {
            IEnumerable<PolynomialExpression> sortedExpression =
                from x in a
                orderby x.exponent descending
                select x;
            var r = new LinkedList<PolynomialExpression>(sortedExpression);

            return r;
        }

        public static LinkedList<PolynomialExpression> CreateList()
        {
            Boolean accept = true;
            LinkedList<PolynomialExpression> x = new LinkedList<PolynomialExpression>();
            while (accept)
            {
                PolynomialExpression obj = new PolynomialExpression();
                Console.Write("\nEnter the coefficient : ");
                obj.coefficient = Int32.Parse(Console.ReadLine());
                if (obj.coefficient == 0)
                    continue;

                Console.Write("Enter the exponent : ");
                obj.exponent = Int32.Parse(Console.ReadLine());
                if (obj.exponent < 0)
                    continue;

                var current = new LinkedListNode<PolynomialExpression>(obj);
                x.AddLast(current);

                Console.WriteLine("Continue? (y/n)");
                accept = Console.ReadLine().StartsWith("y", true, System.Globalization.CultureInfo.CurrentCulture);
            }
            
            return x;
        }

        public static void TraverseList(LinkedList<PolynomialExpression> list)
        {
            foreach (var item in list)
            {
                Console.Write(item + (item.Equals(list.Last.Value) ? " " : " + "));
            }
        }

        public static LinkedList<PolynomialExpression> AddExpressions(LinkedList<PolynomialExpression> a, LinkedList<PolynomialExpression> b)
        {
            //var currentA = a.First;
            //var currentB = b.First;
            var added = new LinkedList<PolynomialExpression>();
            foreach (var itemA in a)
            {
                //currentA = a.Find(itemA);

                foreach (var itemB in b)
                {
                    //currentB = b.Find(itemB);

                    if (itemA.exponent == itemB.exponent)
                    {
                        PolynomialExpression obj = new PolynomialExpression
                        {
                            coefficient = itemA.coefficient + itemB.coefficient,
                            exponent = itemB.exponent
                        };

                        var x = new LinkedListNode<PolynomialExpression>(obj);
                        added.AddLast(x);
                    }
                    else if (itemA.exponent > itemB.exponent)
                    {
                        //if (!added.Contains(itemA))
                        //{
                        //    var x = new LinkedListNode<PolynomialExpression>(itemA);
                        //    added.AddLast(x);
                        //}
                        //break;
                    }
                    else if (itemA.exponent < itemB.exponent)
                    {
                        //if (!added.Contains(itemB))
                        //{
                        //    var x = new LinkedListNode<PolynomialExpression>(itemB);
                        //    added.AddLast(x);
                        //}
                        //continue;
                    }
                }
            }
            return added;
        }
        
        static void Main(string[] args)
        {
            //Input the first expression
            Console.WriteLine("Enter the First polynomial expression : ");
            var exp1 = CreateList();
            //Input the second expression
            Console.WriteLine("\nEnter the Second polynomial expression : ");
            var exp2 = CreateList();

            //Display both expressions
            Console.WriteLine("\n\nExpressions (sorted by exponent in decending order) ");
            Console.Write("\nFirst Expression : ");
            exp1 = SortExponent(exp1);
            TraverseList(exp1);
            Console.Write("\nSecond Expression : ");
            exp2 = SortExponent(exp2);
            TraverseList(exp2);

            var added = AddExpressions(exp1, exp2);
            added = SortExponent(added);
            Console.Write("\n\nAdded Expression : ");
            TraverseList(added);
            
            Console.WriteLine("\n\n");            
        }
    }
}

I also want to use the SortExponent function as an Exponent method, so I declare a static class Extentions in this namespace and add the static function, but I'm getting an error indicating that the scope of the function and the polynomialexpression class is not the same. I don't understand how to implement it.

public static class Expressions
{
public static LinkedList<PolynomialExpression> SortExponent(LinkedList<PolynomialExpression> a)
        {
            IEnumerable<PolynomialExpression> sortedExpression =
                from x in a
                orderby x.exponent descending
                select x;
            var r = new LinkedList<PolynomialExpression>(sortedExpression);

            return r;
        }
}

Any additional suggestion to improve the code will be helpful.

That's too hard to debug your code, please send us the exact problem you face.

Firstly, Sorry for the long delay. I went out for a short trip.

umm, too hard to debug? There is nothing to debug.

The second class Expressions( should be Extensions, but never mind) , you need to insert it just after the namespace declaration, such that it is the first class here. So when you create a LinkedList<PolynomialExpression>, you can access this method by using the dot (.) operator.

OMG! I just realized that the code I posted for the Second SortedExpressions, the parameter declaration is wrong. it as follows:

public static LinkedList<PolynomialExpression> SortExponent(this LinkedList<PolynomialExpression> a)
        {
            IEnumerable<PolynomialExpression> sortedExpression =
                from x in a
                orderby x.exponent descending
                select x;
            var r = new LinkedList<PolynomialExpression>(sortedExpression);

            return r;
        }

you use that method in a static class, so you can use that method like this:

exp2.SortExponent();

however, when I declare/define the class and try to debug it, it's showing 2 errors. I'll post those.

Error 1 Inconsistent accessibility: return type 'System.Collections.Generic.LinkedList<Polynomials.PolynomialExpression>' is less accessible than method 'Polynomials.Expressions.SortExponent(System.Collections.Generic.LinkedList<Polynomials.PolynomialExpression>)' C:\Users\ChaseVoid\Documents\Visual Studio 2008\Projects\C#\Polynomials\Polynomials\Program.cs 10 56 Polynomials

Error 2 Inconsistent accessibility: parameter type 'System.Collections.Generic.LinkedList<Polynomials.PolynomialExpression>' is less accessible than method 'Polynomials.Expressions.SortExponent(System.Collections.Generic.LinkedList<Polynomials.PolynomialExpression>)' C:\Users\ChaseVoid\Documents\Visual Studio 2008\Projects\C#\Polynomials\Polynomials\Program.cs 10 56 Polynomials

Can you tell me what's wrong with the Extension/Expression class?

Also I don't know in the Add function:

public static LinkedList<PolynomialExpression> AddExpressions(LinkedList<PolynomialExpression> a, LinkedList<PolynomialExpression> b)
        {
            //var currentA = a.First;
            //var currentB = b.First;
            var added = new LinkedList<PolynomialExpression>();
            foreach (var itemA in a)
            {
                //currentA = a.Find(itemA);

                foreach (var itemB in b)
                {
                    //currentB = b.Find(itemB);

                    if (itemA.exponent == itemB.exponent)
                    {
                        PolynomialExpression obj = new PolynomialExpression
                        {
                            coefficient = itemA.coefficient + itemB.coefficient,
                            exponent = itemB.exponent
                        };

                        var x = new LinkedListNode<PolynomialExpression>(obj);
                        added.AddLast(x);
                    }
                    else if (itemA.exponent > itemB.exponent)
                    {
                        //if (!added.Contains(itemA))
                        //{
                        //    var x = new LinkedListNode<PolynomialExpression>(itemA);
                        //    added.AddLast(x);
                        //}
                        //break;
                    }
                    else if (itemA.exponent < itemB.exponent)
                    {
                        //if (!added.Contains(itemB))
                        //{
                        //    var x = new LinkedListNode<PolynomialExpression>(itemB);
                        //    added.AddLast(x);
                        //}
                        //continue;
                    }
                }
            }
            return added;
        }

It's just adding the polynomials, with matching exponent, but I don't know to add the remaining node to the final list.

First Expression : 4x^5+5x^4+2x^3+3x^2+7x
Second Expression : 9x^6+6x^4+3x^2

Final Expression should be : 9x^6+4x^5+11x^4+2x^3+6x^2+7x

but it's only displaying
Added Expression : 11x^4 + 6x^2

I need to figure that out, but if you uncomment the parts in the function, it still doesn't work >.<

Do you have any idea? And also why was it hard to debug? Did I not explain properly?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.