[ artriba @ 06.01.2008. 20:15 ] @
Neradi mi funkcija RemoveAll koja bi trebala izbristi sve elmente liste koji odgovaraju tržanom elementu.Ako imam listu 5,1,3,1,8,1,4,7,5 i odaberem 1 trebale bi biti izbrisane sve jedinice. E sad ako imam listu u ovom obliku, u kojoj niti jedna jedinica nije jedna kraj druge program radi ali ako imam 5,1,3,1,1,1,8,1,4,7,5 uvek ostane jedna jedinica Evo koda Code: using System; class Testing { public static void Main ( string [] args ) { LinkedList sLL = new LinkedList (); int input = 0; int data = 0; int after = 0; while ( input != 8 ) { Console.WriteLine ( "1 - insert, 2 - insert at a beginning, 3 - insert at an end, 4 - remove..\n5 - remove all..., 6 - display all, 7 - count, 8 - finish" ); try { input = Convert.ToInt32 ( Console.ReadLine () ); } catch { Console.WriteLine ( "Please enter a integer value" ); } bool flag = false; switch ( input ) { case 1: try { Console.WriteLine ( "Insert new value" ); data = Convert.ToInt32 ( Console.ReadLine () ); Console.WriteLine ( "Insert after what value...you have this values in list" ); sLL.DisplayList (); after = Convert.ToInt32 ( Console.ReadLine () ); } catch { Console.WriteLine ( "Please enter a integer value" ); flag = true; } if ( flag == false ) { try { sLL.Insert ( data, after ); } catch { Console.WriteLine ( "There is no element {0} in the list", after ); } } break; case 2: try { Console.WriteLine ( "Insert new value at a beginning" ); data = Convert.ToInt32 ( Console.ReadLine () ); sLL.InsertAtBegenning ( data ); } catch { Console.WriteLine ( "Please enter a integer value" ); } break; case 3: try { Console.WriteLine ( "Insert new value" ); data = Convert.ToInt32 ( Console.ReadLine () ); sLL.InsertAtEnd ( data ); } catch { Console.WriteLine ( "Please enter a integer value" ); } break; case 4: Console.WriteLine ( "Type value to remove" ); try { data = Convert.ToInt32 ( Console.ReadLine () ); } catch { Console.WriteLine ( "Please enter a integer value" ); } try { sLL.Remove ( data ); } catch { Console.WriteLine ( "There is no element {0} in the list", data ); } break; case 5: try { Console.WriteLine ( "Enter value to be completly removed for the list" ); data = Convert.ToInt32 ( Console.ReadLine () ); } catch { Console.WriteLine ( "Please enter a integer value" ); } sLL.RemoveAll ( data ); break; case 6: sLL.DisplayList (); break; case 7: break; case 8: Console.WriteLine ( "Finished" ); break; default: Console.WriteLine ( "Try again..." ); break; } } } } class Node { private int data; private Node next; public int Data { get { return data; } set { data = value; } } public Node Next { get { return next; } set { next = value; } } } class LinkedList { private Node head; public LinkedList () { head = null; } public void RemoveAll ( int data ) { Node before = null; Node current = head; while ( current != null ) { if ( current.Data == data ) { if ( before == null ) { head = current.Next; GC.Collect (); } else { before.Next = current.Next; GC.Collect (); } } before = current; current = current.Next; } } public void Remove ( int data ) { Node before; Node current; current = head; before = current; if ( current.Data == data ) { head = head.Next; } else { while ( current.Data != data ) { before = current; current = current.Next; } before.Next = current.Next; GC.Collect(); } } public void InsertAtBegenning ( int data ) { if ( head == null ) { head = new Node (); head.Data = data; } else { Node temp; temp = new Node (); temp.Data = data; temp.Next = head; head = temp; } } public void InsertAtEnd ( int data ) { if ( head == null ) { head = new Node (); head.Data = data; } else { Node current = head; Node temp; temp = new Node (); temp.Data = data; while ( current.Next != null ) { current = current.Next; } current.Next = temp; } } public void Insert ( int data, int after ) { Node current = head; if ( current.Data == after ) { Node newNode = new Node (); newNode.Data = data; Node nextNode; nextNode = current.Next; current.Next = newNode; newNode.Next = nextNode; } else { while ( current.Data != after ) { current = current.Next; if ( current.Data == after ) { Node newNode = new Node (); newNode.Data = data; Node nextNode; nextNode = current.Next; current.Next = newNode; newNode.Next = nextNode; } } } } public void DisplayList () { Node temp; temp = head; while ( temp != null ) { Console.Write ( temp.Data + " " ); temp = temp.Next; Console.WriteLine (); } } } |