Untitled

 avatar
unknown
csharp
a year ago
2.4 kB
5
Indexable
// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler

using System;
public class Node<T>
{
    public T Data { get; set; }
    public Node<T> Next { get; set; }

    // Constructors
    public Node()
    {
        Data = default(T);
        Next = null;
    }

    public Node(T data)
    {
        Data = data;
        Next = null;
    }

    public Node(T data, Node<T> next)
    {
        Data = data;
        Next = next;
    }
    public static bool ex3(Node<int> L)
    {
        int count = 0;
        Node <int> p1 = L;
        Node <int> m = L;
        Node <int>m1 = m;
        while (p1!=null)
        {
            count++;
            p1 = p1.Next;
            
        }

        p1 = L;

        for (int i = 0; i < count / 2; i++)
        {
            m = m.Next;

        }

        m1 = m;

        for (int i = 0; i < count / 2; i++)
        {
            bool found = false;
            for (int j = 0; j < count / 2; j++)
            {
                if (p1.Data == m1.Data)
                {
                    found = true;
                    // break;
                }
                else
                {
                    m1 = m1.Next;
                }

            } // end of internal loop

            if (found == false)
            {
                return false;
            }

            p1 = p1.Next;
            m1 = m;


        }
        
        return true;
    }
}

public class HelloWorld
{

    public static void Main(string[] args)
    {
        Node<int> head = new Node<int>(2,
            new Node<int>(5,
                new Node<int>(-1,
                    new Node<int>(12,
                        new Node<int>(3,
                            new Node<int>(4,
                                new Node<int>(12,
                                    new Node<int>(3,
                                        new Node<int>(2,
                                            new Node<int>(-1))))))))));

        // Call the function to check if each number in the first half is present in the second half
        bool result = Node<int>.ex3(head);

        // Print the result
        if (result)
            Console.WriteLine("Each number in the first half is present in the second half.");
        else
            Console.WriteLine("There are numbers in the first half that are not present in the second half.");
    }
}
Editor is loading...
Leave a Comment