2 Replies - 913 Views - Last Post: 09 March 2011 - 02:20 PM Rate Topic: -----

#1 DeepBlueSomething  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-March 11

Unhandled exception: Quick sort used on single linked list.

Posted 08 March 2011 - 05:11 PM

hello everyone, i'm having few problems with quicksort on single linked list, i'm still beginner so dont lough at me. anyway this is my problem : here is my code, and I dont know where I'm makeing mistake, it happens in main programe, whhen i get first element of list, when i try to cast it to node ond sent to class quick it sends me null, not that object. ???? why? where?
could i ever make this code right or is it totally wrong, here is code:

List listaIntegera = new List();


            listaIntegera.InsertFront(67);
            listaIntegera.InsertFront(1);
            listaIntegera.InsertFront(4);
            listaIntegera.InsertFront(23);
            listaIntegera.InsertFront(9);
            listaIntegera.InsertFront(12);
            listaIntegera.InsertFront(27);

            listaIntegera.Display();


            object prviElement = listaIntegera.GetFirst(listaIntegera);
            Node prvi = new Node(0, null);
             prvi = prviElement as Node;
            
            Console.WriteLine("........................");
            Console.WriteLine("prvi element je " + prviElement);

            object zadnjiElement = listaIntegera.GetLast(listaIntegera);
            Node zadnji = zadnjiElement as Node;
            Console.WriteLine("........................");

            Console.WriteLine("zadnji element je "+ zadnji);
            Console.WriteLine("........................");

            Quick.Partition(listaIntegera,  prviElement, zadnjiElement);

            listaIntegera.Display();

            Quick.Sort(listaIntegera, prviElement, zadnjiElement);

            listaIntegera.Display();










class List
    {
        private Node head;
        private Node tail;
        public List() { head = tail = null; }
        public void InsertFront(int data)
        {
            if (IsEmpty())
                head = tail = new Node(data, null);
            else
                head = new Node(data, head);
        }

 public bool IsEmpty() { return head == null; }
        public void Display()
        {
            Node current = head;
            while (current != null)
            {
                Console.WriteLine(current.Element + " ");
                current = current.Next;
            }
            Console.WriteLine();
        }



        public object GetFirst(object data)
        {
            if (IsEmpty())
                return 0;
           object obj = null;
           obj = head.Element;
           return obj;
        }

        public object GetLast(object data)
        {
            if (IsEmpty())
                return 0;
            object obj = null;
            obj = tail.Element;
            return obj;
        }


        public object Sljedeci(object data)
        {
            Node trenutni = data as Node;
            Node sljedeci = new Node(trenutni.Element, trenutni.Next);
            return sljedeci;
        }

        public object Prethodni(object data, object lista)
        {
            Node trenutni = data as Node;
            List Lista = lista as List;
            object prviUlisti = Lista.GetFirst(lista);

            Node current = prviUlisti as Node;

            if (current.Next == trenutni)
                return current;
            while (current.Next != trenutni)
                current = current.Next;
            return current;
        }





class Quick
    {
        public static int Partition(object lista,
object left, object right)
        {
            Node Pivot = left as Node;
            Node Right = right as Node;
            if (Pivot == null)
                return 0;
            Node pivot = new Node(Pivot.Element, Pivot.Next);
            // object pivot = left;
            // Node Pivot = pivot as Node;

            if (pivot.Next == null)
                return 0;
            if (pivot.Element == 0)
                return 0;

            //int last = left;
            Node i = pivot.Next;
            while (i != Right)
                i = i.Next;
            if (i.Element >= pivot.Element)
                Swap(lista, pivot.Next, i);
            Swap(lista, pivot, pivot.Next);

            return pivot.Element;
        }


        public static void Swap(object lista, object first, object second)
        {
            Node prvi = first as Node;
            Node drugi = second as Node;


            Node temp = prvi.Next;
            prvi.Next = drugi.Next;
            drugi.Next = temp;

        }


        public static void Sort(object lista, object left, object right)
        {
            Node Left = left as Node;
            Node Right = right as Node;
            List Lista = lista as List;

            if (Left == null)
                return;
            if (Right == null)
                return;
            if (Left.Element >= Right.Element) return;

            int last = Partition(lista, Left, Right);
            Sort(lista, Left, Lista.Prethodni(Left, lista));
            Sort(lista, Lista.Sljedeci(Left), right);
        }
    }
}



Edited by macosxnerd101: Post your code BETWEEN your code tags. Like so: :code:.

Is This A Good Question/Topic? 0
  • +

Replies To: Unhandled exception: Quick sort used on single linked list.

#2 Sergio Tapia  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1212
  • View blog
  • Posts: 4,128
  • Joined: 27-January 10

Re: Unhandled exception: Quick sort used on single linked list.

Posted 08 March 2011 - 06:20 PM

Explain clearly:

1. What you're trying to do. What has to happen for this to be 100% complete.
2. Where the exception is firing.
3. Remove any lines of code that aren't relevant. We hate sifting through extra code.
Was This Post Helpful? 0
  • +
  • -

#3 DeepBlueSomething  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-March 11

Re: Unhandled exception: Quick sort used on single linked list.

Posted 09 March 2011 - 02:20 PM

I,m sorry, I guess I wasn't quite clear, here :
in main I find first element of the list like this

object prviElement = listaIntegera.GetFirst(listaIntegera);
     Node prvi = new Node(0, null);
    prvi = prviElement as Node;



which then skipes in class list and with this function finds element

public object GetFirst(object data)
	        {
	            if (IsEmpty())
	                return 0;
	           object obj = null;
	           obj = head.Element;
	           return obj;
	        }



So then I cast it in main in type node,I find the last element in same way, and than i call function partition from class quick with line
Quick.Partition(listaIntegera, prvi, zadnji);
I understand I didnt even needed to cast it because the method Partition is called with objects.
And here is where I made some mistake, it sends me null into the method so it doesnt do anything just returns me the same list. so i find element but when i call Partition with it, or sort, it becomes 0. How, why?? I have no idea, at all!!
so if somone sees my mistake, please help me,Im kind of tight with time.

edited by : You've already been asked once, post your code using CODE tags:
:code:

This post has been edited by insertAlias: 09 March 2011 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1