Search Results

Search found 163 results on 7 pages for 'linkedlist'.

Page 1/7 | 1 2 3 4 5 6 7  | Next Page >

  • Replace LinkedList element value through LinkedList.Enumerator

    - by Yan Cheng CHEOK
    I realize there are no way for me to replace value through LinkedList.Enumerator. For instead, I try to port the below Java code to C# // Java ListIterator<Double> itr1 = linkedList1.listIterator(); ListIterator<Double> itr2 = linkedList2.listIterator(); while(itr1.hasNext() && itr2.hasNext()){ Double d = itr1.next() + itr2.next(); itr1.set(d); } // C# LinkedList<Double>.Enumerator itr1 = linkedList1.GetEnumerator(); LinkedList<Double>.Enumerator itr2 = linkedList2.GetEnumerator(); while(itr1.MoveNext() && itr2.MoveNext()){ Double d = itr1.Current + itr2.Current; // Opps. Compilation error! itr1.Current = d; } Any other technique I can use?

    Read the article

  • removeFirst and addLast methods of LinkedList Class are Unknown

    - by user318068
    I have a problem with my code in C# . if i click in compiler button , I get the following errors 'System.Collections.Generic.LinkedList<int?>' does not contain a definition for 'removeFirst' and no extension method 'removeFirst' accepting a first argument of type 'System.Collections.Generic.LinkedList<int?>' could be found (are you missing a using directive or an assembly reference?). and 'System.Collections.Generic.LinkedList<Hanoi_tower.Sol>' does not contain a definition for 'addLast' and no extension method 'addLast' accepting a first argument of type 'System.Collections.Generic.LinkedList<Hanoi_tower.Sol>' could be found (are you missing a using directive or an assembly reference?) This is my program using System.; using System.Collections.Generic; using System.Linq; using System.Text; namespace Hanoi_tower { public class Sol { public LinkedList<int?> tower1 = new LinkedList<int?>(); public LinkedList<int?> tower2 =new LinkedList<int?>(); public LinkedList<int?> tower3 =new LinkedList<int?>(); public int depth; public LinkedList<Sol> neighbors; public Sol(LinkedList<int?> tower1, LinkedList<int?> tower2, LinkedList<int?> tower3) { this.tower1 = tower1; this.tower2 = tower2; this.tower3 = tower3; neighbors = new LinkedList<Sol>(); } public virtual void getneighbors() { Sol temp = this.copy(); Sol neighbor1 = this.copy(); Sol neighbor2 = this.copy(); Sol neighbor3 = this.copy(); Sol neighbor4 = this.copy(); Sol neighbor5 = this.copy(); Sol neighbor6 = this.copy(); if (temp.tower1.Count != 0) { if (neighbor1.tower2.Count != 0) { if (neighbor1.tower1.First.Value < neighbor1.tower2.First.Value) { neighbor1.tower2.AddFirst(neighbor1.tower1.RemoveFirst); neighbors.AddLast(neighbor1); } } else { neighbor1.tower2.AddFirst(neighbor1.tower1.RemoveFirst()); neighbors.AddLast(neighbor1); } if (neighbor2.tower3.Count != 0) { if (neighbor2.tower1.First.Value < neighbor2.tower3.First.Value) { neighbor2.tower3.AddFirst(neighbor2.tower1.RemoveFirst()); neighbors.AddLast(neighbor2); } } else { neighbor2.tower3.AddFirst(neighbor2.tower1.RemoveFirst()); neighbors.AddLast(neighbor2); } } //------------- if (temp.tower2.Count != 0) { if (neighbor3.tower1.Count != 0) { if (neighbor3.tower2.First.Value < neighbor3.tower1.First.Value) { neighbor3.tower1.AddFirst(neighbor3.tower2.RemoveFirst()); neighbors.AddLast(neighbor3); } } else { neighbor3.tower1.AddFirst(neighbor3.tower2.RemoveFirst()); neighbors.AddLast(neighbor3); } if (neighbor4.tower3.Count != 0) { if (neighbor4.tower2.First.Value < neighbor4.tower3.First.Value) { neighbor4.tower3.AddFirst(neighbor4.tower2.RemoveFirst()); neighbors.AddLast(neighbor4); } } else { neighbor4.tower3.AddFirst(neighbor4.tower2.RemoveFirst()); neighbors.AddLast(neighbor4); } } //------------------------ if (temp.tower3.Count() != 0) { if (neighbor5.tower1.Count() != 0) { if(neighbor5.tower3.ElementAtOrDefault() < neighbor5.tower1.ElementAtOrDefault()) { neighbor5.tower1.AddFirst(neighbor5.tower3.RemoveFirst()); neighbors.AddLast(neighbor5); } } else { neighbor5.tower1.AddFirst(neighbor5.tower3.RemoveFirst()); neighbors.AddLast(neighbor5); } if (neighbor6.tower2.Count() != 0) { if(neighbor6.tower3.element() < neighbor6.tower2.element()) { neighbor6.tower2.addFirst(neighbor6.tower3.removeFirst()); neighbors.addLast(neighbor6); } } else { neighbor6.tower2.addFirst(neighbor6.tower3.removeFirst()); neighbors.addLast(neighbor6); } } } public override string ToString() { string str; str="tower1"+ tower1.ToString() + " tower2" + tower2.ToString() + " tower3" + tower3.ToString(); return str; } public Sol copy() { Sol So; LinkedList<int> l1= new LinkedList<int>(); LinkedList<int> l2=new LinkedList<int>(); LinkedList<int> l3 = new LinkedList<int>(); for(int i=0;i<=this.tower1.Count() -1;i++) { l1.AddLast(tower1.get(i)); } for(int i=0;i<=this.tower2.size()-1;i++) { l2.addLast(tower2.get(i)); } for(int i=0;i<=this.tower3.size()-1;i++) { l3.addLast(tower3.get(i)); } So = new Sol(l1, l2, l3); return So; } public bool Equals(Sol sol) { if (this.tower1.Equals(sol.tower1) & this.tower2.Equals(sol.tower2) & this.tower3.Equals(sol.tower3)) return true; return false; } public virtual bool containedin(Stack<Sol> vec) { bool found =false; for(int i=0;i<= vec.Count-1;i++) { if(vec.get(i).tower1.Equals(this.tower1) && vec.get(i).tower2.Equals(this.tower2) && vec.get(i).tower3.Equals(this.tower3)) { found=true; break; } } return found; } } }

    Read the article

  • How can you Merge sort a .Net framework LinkedList (of T)

    - by Andronicus
    There's a few questions discussing Merge sorting a LinkedList, but how can I do it with the C# LinkedList? Since the LinkedListNode Next and Previous properties are read-only most of the in-place algorithms I've come across are not possible. I've resorted to removing all the nodes, sorting them with a .OrderBy(node = node.Value) and then re-inserting them into the Linked list, but it's fairly crude.

    Read the article

  • Java, LinkedList of Strings. Insert in alphabetical order

    - by user69514
    I have a simple linked list. The node contains a string (value) and an int (count). In the linkedlist when I insert I need to insert the new Node in alphabetical order. If there is a node with the same value in the list, then I simply increment the count of the node. I think I got my method really screwed up. public void addToList(Node node){ //check if list is empty, if so insert at head if(count == 0 ){ head = node; head.setNext(null); count++; } else{ Node temp = head; for(int i=0; i<count; i++){ //if value is greater, insert after if(node.getItem().getValue().compareTo(temp.getItem().getValue()) > 0){ node.setNext(temp.getNext()); temp.setNext(node); } //if value is equal just increment the counter else if(node.getItem().getValue().compareTo(temp.getItem().getValue()) == 0){ temp.getItem().setCount(temp.getItem().getCount() + 1); } //else insert before else{ node.setNext(temp); } } } }

    Read the article

  • What is the problem with my LinkedList class?

    - by user258367
    class LinkedList { private: int data; LinkedList *ptr; public: LinkedList(int i_data) { data = i_data; ptr = 0; { ~LinkedList() { delete ptr ; } void insert(LinkedList *node) { while(this->next != 0) this = this->next; this->next = node; } } I will be creating a head node like head = new LinkedList(4) and then will be calling like head->insert(new LinkedList(5)) and subsequently . Can you please tell me does above class represent a linkedlist . i think yes it has node which contain address of next node . Please correct me if i am wrong

    Read the article

  • LinkedList Wrong Display(string builder)

    - by Chris
    Hello, The following program is a basic linked list divided in 3 classes. In the tester class (main) i add several numbers to the list (sorted). But insteed of getting the numbers as a result i get the result: LinkedList.LinkedList Is something wrong with the stringbuilder (the program was first in java where a string buffer was used, but that should be the same i think?) LinkedListTester.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinkedList { public class LinkedListTester { static void Main(string[] args) { LinkedList ll = new LinkedList(); ll.addDataSorted(5); ll.addDataSorted(7); ll.addDataSorted(13); ll.addDataSorted(1); ll.addDataSorted(17); ll.addDataSorted(8); Console.WriteLine(ll); } } }/LinkedList/ LinkedList.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinkedList { public class LinkedList { //toestand private LinkedListNode first; private LinkedListNode last; //gedrag public LinkedList() { first = null; last = null; } public void addDataInFront(int data) { first = new LinkedListNode(data, first); if (last == null){ last = first; } }/*addDataInFront*/ public void addDataToBack(int data) { if (first == null) { addDataInFront(data); } else { last.setNext(new LinkedListNode(data, null)); last = last.getNext(); } }/*addDataToBack*/ public void addDataSorted(int data) { if (first == null || first.getData() > data) { addDataInFront(data); } else { LinkedListNode currentNode = first; while (currentNode.getNext() != null && currentNode.getNext().getData() < data) { currentNode = currentNode.getNext(); } currentNode.setNext(new LinkedListNode(data, currentNode.getNext())); currentNode = currentNode.getNext(); if (currentNode.getNext() == null) { last = currentNode; } } }/*addDataSorted*/ public String toString() { StringBuilder Buf = new StringBuilder(); LinkedListNode currentNode = first; while (currentNode != null) { Buf.Append(currentNode.getData()); Buf.Append(' '); currentNode = currentNode.getNext(); } return Buf.ToString(); }/*toString*/ }/*LinkedList*/ }/LinkedList/ LinkedListNode: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinkedList { public class LinkedListNode { //toestand private int data; private LinkedListNode next; private LinkedListNode previous; //gedrag public LinkedListNode(int data, LinkedListNode next) { this.data = data; this.next = next; this.previous = null; } public LinkedListNode(int data, LinkedListNode next, LinkedListNode previous) { this.data = data; this.next = next; this.previous = previous; } public LinkedListNode getNext() { return next; } public LinkedListNode getPrevious() { return previous; } public void setNext(LinkedListNode next) { this.next = next; } public void setPrevious(LinkedListNode previous) { this.previous = previous; } public int getData() { return data; } }/*LinkedListNode*/ }/LinkedList/

    Read the article

  • LinkedHashMap vs HashMap != LinkedList vs ArrayList

    - by Markos Fragkakis
    I have read that LinkedHashMap has faster iteration speed than HashMap because its elements are doubly linked to each other. Additionally, because of this, LinkedHashMap is slower when inserting or deleting elements. Presumably because these links also need to be updated. Although I can see an analogy to LinkedList vs ArrayList, in that the elements of LinkedList are also doubly-linked, I read that it iterates slower than ArrayList, and has faster insertion and deletion times. Why is this? Perhaps I am making a mistake somewhere? Cheers1

    Read the article

  • Help with abstract class in Java with private variable of type List<E>

    - by Nazgulled
    Hi, It's been two years since I last coded something in Java so my coding skills are bit rusty. I need to save data (an user profile) in different data structures, ArrayList and LinkedList, and they both come from List. I want to avoid code duplication where I can and I also want to follow good Java practices. For that, I'm trying to create an abstract class where the private variables will be of type List<E> and then create 2 sub-classes depending on the type of variable. Thing is, I don't know if I'm doing this correctly, you can take a look at my code: Class: DBList import java.util.List; public abstract class DBList { private List<UserProfile> listName; private List<UserProfile> listSSN; public List<UserProfile> getListName() { return this.listName; } public List<UserProfile> getListSSN() { return this.listSSN; } public void setListName(List<UserProfile> listName) { this.listName = listName; } public void setListSSN(List<UserProfile> listSSN) { this.listSSN = listSSN; } } Class: DBListArray import java.util.ArrayList; public class DBListArray extends DBList { public DBListArray() { super.setListName(new ArrayList<UserProfile>()); super.setListSSN(new ArrayList<UserProfile>()); } public DBListArray(ArrayList<UserProfile> listName, ArrayList<UserProfile> listSSN) { super.setListName(listName); super.setListSSN(listSSN); } public DBListArray(DBListArray dbListArray) { super.setListName(dbListArray.getListName()); super.setListSSN(dbListArray.getListSSN()); } } Class: DBListLinked import java.util.LinkedList; public class DBListLinked extends DBList { public DBListLinked() { super.setListName(new LinkedList<UserProfile>()); super.setListSSN(new LinkedList<UserProfile>()); } public DBListLinked(LinkedList<UserProfile> listName, LinkedList<UserProfile> listSSN) { super.setListName(listName); super.setListSSN(listSSN); } public DBListLinked(DBListLinked dbListLinked) { super.setListName(dbListLinked.getListName()); super.setListSSN(dbListLinked.getListSSN()); } } 1) Does any of this make any sense? What am I doing wrong? Do you have any recommendations? 2) It would make more sense for me to have the constructors in DBList and calling them (with super()) in the subclasses but I can't do that because I can't initialize a variable with new List<E>(). 3) I was thought to do deep copies whenever possible and for that I always override the clone() method of my classes and code it accordingly. But those classes never had any lists, sets or maps on them, they only had strings, ints, floats. How do I do deep copies in this situation?

    Read the article

  • Copy a LinkedList that has a Random Pointer in it

    - by Bragaadeesh
    Hi, First of all this is not a homework, this is an interview question that I got from a company I attended today. You have a singly linked list with the Node structure as the following class Node{ int data; Node next; Node random; } You have a typical singly linked list of length n. The random pointer in each node in the linkedlist randomly points to some Node within the linked list. The Question is to create a copy of the linked list efficiently into a different LinkedList. I said that I will first calculate the Random pointer's position in the linked list and store it in an array. Then create a new linked list normally. Then iterate through the linked list by setting the random pointer where they belong by reading the values stored from the array. I know its a very brute force technique and the interviewer asked me to come up with a better solution but I couldnt. Please can someone answer this? I can explain if the question is not clear.

    Read the article

  • Why does my C++ LinkedList cause a EXC_BAD_ACCESS?

    - by Anthony Glyadchenko
    When I call the cmremoveNode method in my LinkedList from outside code, I get an EXC_BAD_ACCESS. /* * LinkedList.h * Lab 6 * * Created by Anthony Glyadchenko on 3/22/10. * Copyright 2010 __MyCompanyName__. All rights reserved. * */ #include <stdio.h> #include <iostream> #include <fstream> #include <iomanip> using namespace std; class ctNode { friend class ctlinkList ; // friend class allowed to access private data private: string sfileWord ; // used to allocate and store input word int iwordCnt ; // number of word occurrances ctNode* ctpnext ; // point of Type Node, points to next link list element }; class ctlinkList { private: ctNode* ctphead ; // initialized by constructor public: ctlinkList () { ctphead = NULL ; } ctNode* gethead () { return ctphead ; } string cminsertNode (string svalue) { ctNode* ctptmpHead = ctphead ; if ( ctphead == NULL ) { // allocate new and set head ctptmpHead = ctphead = new ctNode ; ctphead -> ctpnext = NULL ; ctphead -> sfileWord = svalue ; } else { //find last ctnode do { if ( ctptmpHead -> ctpnext != NULL ) ctptmpHead = ctptmpHead -> ctpnext ; } while ( ctptmpHead -> ctpnext != NULL ) ; // fall thru found last node ctptmpHead -> ctpnext = new ctNode ; ctptmpHead = ctptmpHead -> ctpnext ; ctptmpHead -> ctpnext = NULL ; ctptmpHead -> sfileWord = svalue ; } return ctptmpHead -> sfileWord ; } string cmreturnNode (string svalue) { return NULL; } string cmremoveNode (string svalue) { if (ctphead == NULL) return NULL; ctNode *tmpHead = ctphead; while (tmpHead->sfileWord != svalue || tmpHead->ctpnext != NULL){ tmpHead = tmpHead->ctpnext; } if (tmpHead == NULL){ return NULL; } else { while (tmpHead != NULL){ tmpHead = tmpHead->ctpnext; } } return tmpHead->sfileWord; } string cmlistList () { string tempList; ctNode *tmpHead = ctphead; if (ctphead == NULL){ return NULL; } else{ while (tmpHead != NULL){ cout << tmpHead->sfileWord << " "; tempList += tmpHead->sfileWord; tmpHead = tmpHead -> ctpnext; } } return tempList; } }; Why is this happening?

    Read the article

  • LinkedList parameters in Java

    - by Walt
    Hi there, I created a program that contains linked lists that are passed various methods. While this works just fine in Java... a style checker program we have to use doesn't like it It says: Declaring variables, return values or parameters of type 'LinkedList' is not allowed. If I declare them as simply List then I don't have access to the methods I want. What should I do?

    Read the article

  • Java - PriorityQueue vs sorted LinkedList

    - by msr
    Hello, Which implementation is less "heavy": PriorityQueue or a sorted LinkedList (using a Comparator)? I want to have all the items sorted. The insertion will be very frequent and ocasionally I will have to run all the list to make some operations. Thank you!

    Read the article

  • When to use LinkedList<> over ArrayList<>?

    - by sdellysse
    I've always been one to simply use List<String> names = new ArrayList<String>(); I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code. When should LinkedList should be used over ArrayList and vice-versa?

    Read the article

  • LinkedList insert tied to inserted object

    - by wrongusername
    I have code that looks like this: public class Polynomial { List<Term> term = new LinkedList<Term>(); and it seems that whenever I do something like term.add(anotherTerm), with anotherTerm being... another Term object, it seems anotherTerm is referencing the same thing as what I've just inserted into term so that whenever I try to change anotherTerm, term.get(2) (let's say) get's changed too. How can I prevent this from happening?

    Read the article

  • LinkedList.contains execution speed

    - by Le_Coeur
    Why Methode LinkedList.contains() runs quickly than such implementation: for (String s : list) if (s.equals(element)) return true; return false; I don't see great difference between this to implementations(i consider that search objects aren't nulls), same iterator and equals operation

    Read the article

  • Java: Swap elements in LinkedList class

    - by Algorist
    Hi, I want to maintain order of the elements being added in a list. So, I used a linkedlist in Java. Now I want to be able to swap two elements in the linked list. First of all, I cannot find an elementAt method for linked list. Also, there is no way to add element at a specified position. Thank you.

    Read the article

  • LinkedList cannot be serialised?

    - by iTayb
    Here are my classes: http://pastebin.com/3dc5Vb1t When I try to run BookStore b = new BookStore(); b.LoadFromXML(Server.MapPath("list.xml")); Label1.Text = b.ToString(); I get the following error: You must implement a default accessor on System.Collections.Generic.LinkedList`1[[Book, App_Code.cxsacizw, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]] because it inherits from ICollection. The error source is XmlSerializer s = new XmlSerializer(typeof(BookStore)); When I tried to look for a solution on google, I found it that LinkedList has some problems with serialization. How can I deal with it? Thank you very much.

    Read the article

  • WithEvents LinkedList is it Impossible?

    - by serhio
    What is the optimal approach to a WithEvents Collection - VB.NET? Have you any remarks on the code bellow (skipping the Nothing verifications)? The problem is when I obtain the LinkedListNode(Of Foo) in a For Each block I can set myNode.Value = something, and here is a handlers leak... -Could I override the FooCollection's GetEnumerator in this case? -No. :( cause NotInheritable Class LinkedListNode(Of T) Class Foo Public Event SelectedChanged As EventHandler End Class Class FooCollection Inherits LinkedList(Of Foo) Public Event SelectedChanged As EventHandler Protected Overloads Sub AddFirst(ByVal item As Foo) AddHandler item.SelectedChanged, AddressOf OnSelectedChanged MyBase.AddFirst(item) End Sub Protected Overloads Sub AddLast(ByVal item As Foo) AddHandler item.SelectedChanged, AddressOf OnSelectedChanged MyBase.AddLast(item) End Sub ' ------------------- ' Protected Overloads Sub RemoveFirst() RemoveHandler MyBase.First.Value.SelectedChanged, _ AddressOf OnSelectedChanged MyBase.RemoveFirst() End Sub Protected Overloads Sub RemoveLast(ByVal item As Foo) RemoveHandler MyBase.Last.Value.SelectedChanged, _ AddressOf OnSelectedChanged MyBase.RemoveLast() End Sub ' ------------------- ' Protected Sub OnSelectedChanged(ByVal sender As Object, ByVal e As EventArgs) RaiseEvent SelectedChanged(sender, e) End Sub End Class

    Read the article

  • Java LinkedList iterator being exhausted prematurely

    - by Sujeet
    I am using LinkedList and retrieving an Iterator object by using list.iterator(). After that, I am checking it.hasNext(), real issue is while checking it.hasNext(), sometimes it returns false. I need help why this is happening, though I have elements in the list. Some code: public synchronized void check(Object obj) throws Exception { Iterator itr = list.iterator(); while(itr.hasNext()) { //This Line I get false.. though i have list size is 1 Item p = (Item)itr.next(); if(p.getId() == null) {continue;} if(p.getId().getElemntId() == obj.getId() || obj.getId() == 0 ) { p.setResponse(obj); notifyAll(); return; } } Log.Error("validate failed obj.getId="+obj.getId()+" **list.size="+list.size()*This shows 1*); throw new Exception("InvalidData"); }

    Read the article

  • jquery-sortable using behavior of a linkedlist

    - by BabaBooey
    I suspect I'm not looking at this issue in the right way so here goes. I have essentially a LinkedList of data on a web page (http://en.wikipedia.org/wiki/Linked_list) that I'd like to manipulate using traditional Linked List behavior (i.e. just updating the reference/id of the "next" object) for performance reasons. Where this gets a bit tricky is I'd ideally like to use Jquery's sortable to do this. Like the user would drag something up/down and I could just do an Ajax call to the server with the id of the object that moved and the new parent id of that object (and then behind the scenes I could figure out how to reconnect things..maybe need more data than that...). But every example I've seen where sortable is used they were sending the whole re-indexed list to the database to update which seems unnecessary to me. With a linked list to change an element's "index" I only need to make 3 updates which depending on the size of the list could be a big performance savings. Anyone have an example of what I'm trying to do...am I too far in left field?

    Read the article

  • How to use a linkedList and multiple classes with my Java GUI

    - by Asj
    How should I use a linked list with my GUI program? The program is supposed to have blocks with textareas and dropdown menus. The number of blocks depends on the number of times the user presses a button. It's supposed to be possible to put blocks within blocks. I want to store the information created, using a linked list and then saving to a file. I'm thinking, for the sub blocks, there would be linked lists within the linked list. Should I use Java.util.LinkedList? How do I add the information? A Node class? Should that be in a separate file? I started to try sitting up a linked list, but it's getting me confused. I'm still unsure about how a person is supposed to make GUI's. I've only seen really simple GUI's. Can anyone tell me how I should arrange things? At the moment, I have three files, one for the main GUI window, one for the question blocks to be inserted within that, and one for some tools to use within those two files to make the code easier to understand. But, there seems to be something wrong with the question block file, because I've been making the background white for each JPanel, and there's a gray outline around the question blocks when I run the program. I probably shouldn't paste a ton of code here... These are my files: http://asj127.webs.com/BuildAssessmentWindow.java http://asj127.webs.com/QuestionBlock.java http://asj127.webs.com/JPanelTools.java

    Read the article

  • error in C# code

    - by user318068
    hi all . I have a problem with my code in C# . if i click in compiler button , I get the following errors 'System.Collections.Generic.LinkedList' does not contain a definition for 'removeFirst' and no extension method 'removeFirst' accepting a first argument of type 'System.Collections.Generic.LinkedList' could be found (are you missing a using directive or an assembly reference?). and 'System.Collections.Generic.LinkedList' does not contain a definition for 'addLast' and no extension method 'addLast' accepting a first argument of type 'System.Collections.Generic.LinkedList' could be found (are you missing a using directive or an assembly reference?) This is part of a simple program using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Hanoi { public class Sol { public LinkedList<int?> t1 = new LinkedList<int?>(); public LinkedList<int?> t2 =new LinkedList<int?>(); public LinkedList<int?> t3 =new LinkedList<int?>(); public int depth; public LinkedList<Sol> neighbors; public Sol(LinkedList<int?> t1, LinkedList<int?> t2, LinkedList<int?> t3) { this.t1 = t1; this.t2 = t2; this.t3 = t3; neighbors = new LinkedList<Sol>(); } public virtual void getneighbors() { Sol temp = this.copy(); Sol neighbor1 = this.copy(); Sol neighbor2 = this.copy(); Sol neighbor3 = this.copy(); Sol neighbor4 = this.copy(); Sol neighbor5 = this.copy(); Sol neighbor6 = this.copy(); if (temp.t1.Count != 0) { if (neighbor1.t2.Count != 0) { if (neighbor1.t1.First.Value < neighbor1.t2.First.Value) { neighbor1.t2.AddFirst(neighbor1.t1.RemoveFirst()); neighbors.AddLast(neighbor1); } } else { neighbor1.t2.AddFirst(neighbor1.t1.RemoveFirst()); neighbors.AddLast(neighbor1); } if (neighbor2.t3.Count != 0) { if (neighbor2.t1.First.Value < neighbor2.t3.First.Value) { neighbor2.t3.AddFirst(neighbor2.t1.RemoveFirst()); neighbors.AddLast(neighbor2); } } else I hope that you find someone to help me

    Read the article

  • Java threads not working correctly with linkedlist

    - by user69514
    Hi I am working on the sleeping barber problem. with the addition of having priority customer when they arrive they go in the front of the line and they are the next ones to get a haircut. I'm using a linkedlist and if I see a priority customer I put him in the beginning of the list, if the customer is not priority he goes to the end of the list. then I call the wantHaircut method getting the first element of the list. my problem is that the customer are being processed in the order they arrive, and the priority customer have to wait. here is the code where it all happens. any ideas what I am doing wrong? thanks public void arrivedBarbershop(Customer c){ if(waiting < numChairs && c.isPriority()){ System.out.println("Customer " + c.getID() + ": is a priority customer - SITTING -"); mutex.up(); customer_list.addFirst(c); } else if(waiting >= numChairs && c.isPriority()){ System.out.println("Customer " + c.getID() + ": is a priority customer - STANDING -"); mutex.up(); customer_list.addFirst(c); } else if(waiting < numChairs && !c.isPriority()){ waiting++; System.out.println("Customer " + c.getID() + ": arrived, sitting in the waiting room"); customer_list.addLast(c); customers.up(); // increment waiting customers } else if(waiting >= numChairs && !c.isPriority()) { System.out.println("Customer " + c.getID() + ": went to another barber because waiting room was full - " + waiting + " waiting"); mutex.up(); } if(!customer_list.isEmpty()){ this.wantHairCut(customer_list.removeFirst()); } } public void wantHairCut(Customer c) { mutex.up(); barber.down(); // waits for being allowed in barber chair System.out.println("Customer " + c.getID() + ": getting haircut"); try { /** haircut takes between 1 and 2 seconds **/ Thread.sleep(Barbershop.randomInt(1, 2) * 1000); } catch (InterruptedException e) { } System.out.println("Barber: finished cutting customer " + c.getID() + "'s hair"); c.gotHaircut = true; cutting.up(); // signals cutting has finished /** customer must pay now **/ this.wantToCashout(c); }

    Read the article

  • Implementing clone on a LinkedList

    - by devoured elysium
    I am trying to implement a clone() method on a DoubleLinkedList. Now, the problem is that implementing it by "the convention" is a lot more troublesome than just creating a new DoubleLinkedList and filling it with all the elements of my current DoubleLinkedList. Is there any inconvenient I am not seeing when doing that? Here is my current approach: @Override public DoubleLinkedList<T> clone() { DoubleLinkedList<T> dll = new DoubleLinkedList<T>(); for (T element : dll) { dll.add(element); } return dll; } Here is what it would be by the convention: @Override public DoubleLinkedList<T> clone() { try { DoubleLinkedList<T> dll = (DoubleLinkedList<T>)super.clone(); //kinda complex code to copy elements return dll; } catch (CloneNotSupportedException e) { throw new InternalError(e.toString()); } }

    Read the article

  • System.InvalidOperationException in Output Window

    - by user318068
    I constantly get the following message in my output/debug windows. The app doesn't crash but I was wondering what the deal with it is: A first chance exception of type 'System.InvalidOperationException' occurred in System.dll my code :sol.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Sol { public LinkedList<int> tower1 = new LinkedList<int>(); public LinkedList<int> tower2 = new LinkedList<int>(); public LinkedList<int> tower3 = new LinkedList<int>(); public static LinkedList<string> BFS = new LinkedList<string>(); public static LinkedList<string> DFS = new LinkedList<string>(); public static LinkedList<string> IDS = new LinkedList<string>(); public int depth; public LinkedList<Sol> neighbors; public Sol() { } public Sol(LinkedList<int> tower1, LinkedList<int> tower2, LinkedList<int> tower3) { this.tower1 = tower1; this.tower2 = tower2; this.tower3 = tower3; neighbors = new LinkedList<Sol>(); } public virtual void getneighbors() { Sol temp = this.copy(); Sol neighbor1 = this.copy(); Sol neighbor2 = this.copy(); Sol neighbor3 = this.copy(); Sol neighbor4 = this.copy(); Sol neighbor5 = this.copy(); Sol neighbor6 = this.copy(); if (temp.tower1.Count != 0) { if (neighbor1.tower2.Count != 0) { if (neighbor1.tower1.First.Value < neighbor1.tower2.First.Value) { neighbor1.tower2.AddFirst(neighbor1.tower1.First); neighbor1.tower1.RemoveFirst(); neighbors.AddLast(neighbor1); } } else { neighbor1.tower2.AddFirst(neighbor1.tower1.First); neighbor1.tower1.RemoveFirst(); neighbors.AddLast(neighbor1); } if (neighbor2.tower3.Count != 0) { if (neighbor2.tower1.First.Value < neighbor2.tower3.First.Value) { neighbor2.tower3.AddFirst(neighbor2.tower1.First); neighbor2.tower1.RemoveFirst(); neighbors.AddLast(neighbor2); } } else { neighbor2.tower3.AddFirst(neighbor2.tower1.First); neighbor2.tower1.RemoveFirst(); neighbors.AddLast(neighbor2); } } //------------- if (temp.tower2.Count != 0) { if (neighbor3.tower1.Count != 0) { if (neighbor3.tower2.First.Value < neighbor3.tower1.First.Value) { neighbor3.tower1.AddFirst(neighbor3.tower2.First); neighbor3.tower2.RemoveFirst(); neighbors.AddLast(neighbor3); } } else { neighbor3.tower1.AddFirst(neighbor3.tower2.First); neighbor3.tower2.RemoveFirst(); neighbors.AddLast(neighbor3); } if (neighbor4.tower3.Count != 0) { if (neighbor4.tower2.First.Value < neighbor4.tower3.First.Value) { neighbor4.tower3.AddFirst(neighbor4.tower2.First); neighbor4.tower2.RemoveFirst(); neighbors.AddLast(neighbor4); } } else { neighbor4.tower3.AddFirst(neighbor4.tower2.First); neighbor4.tower2.RemoveFirst(); neighbors.AddLast(neighbor4); } } //------------------------ if (temp.tower3.Count() != 0) { if (neighbor5.tower1.Count() != 0) { if (neighbor5.tower3.ElementAtOrDefault(0) < neighbor5.tower1.ElementAtOrDefault(0)) { neighbor5.tower1.AddFirst(neighbor5.tower3.First); neighbor5.tower3.RemoveFirst(); neighbors.AddLast(neighbor5); } } else { neighbor5.tower1.AddFirst(neighbor5.tower3.First); neighbor5.tower3.RemoveFirst(); neighbors.AddLast(neighbor5); } if (neighbor6.tower2.Count() != 0) { if (neighbor6.tower3.ElementAtOrDefault(0) < neighbor6.tower2.ElementAtOrDefault(0)) { neighbor6.tower2.AddFirst(neighbor6.tower3.First); neighbor6.tower3.RemoveFirst(); neighbors.AddLast(neighbor6); } } else { neighbor6.tower2.AddFirst(neighbor6.tower3.First); neighbor6.tower3.RemoveFirst(); neighbors.AddLast(neighbor6); } } } public override string ToString() { string str; str = "tower1" + tower1.ToString() + " tower2" + tower2.ToString() + " tower3" + tower3.ToString(); return str; } public Sol copy() { Sol So; LinkedList<int> l1 = new LinkedList<int>(); LinkedList<int> l2 = new LinkedList<int>(); LinkedList<int> l3 = new LinkedList<int>(); for (int i = 0; i <= this.tower1.Count() - 1; i++) { l1.AddLast(tower1.ElementAt(i)); } for (int i = 0; i <= this.tower2.Count - 1; i++) { l2.AddLast(tower2.ElementAt(i)); } for (int i = 0; i <= this.tower3.Count - 1; i++) { l3.AddLast(tower3.ElementAt(i)); } So = new Sol(l1, l2, l3); return So; } public bool Equals(Sol sol) { if (this.tower1.Equals(sol.tower1) & this.tower2.Equals(sol.tower2) & this.tower3.Equals(sol.tower3)) return true; return false; } public virtual bool containedin(Stack<Sol> vec) { bool found = false; for (int i = 0; i <= vec.Count - 1; i++) { if (vec.ElementAt(i).tower1.Equals(this.tower1) && vec.ElementAt(i).tower2.Equals(this.tower2) && vec.ElementAt(i).tower3.Equals(this.tower3)) { found = true; break; } } return found; } public virtual bool breadthFirst(Sol start, Sol goal) { Stack<Sol> nextStack = new Stack<Sol>(); Stack<Sol> traversed = new Stack<Sol>(); bool found = false; start.depth = 0; nextStack.Push(start); while (nextStack.Count != 0) { Sol sol = nextStack.Pop(); BFS.AddFirst("poped State:" + sol.ToString() + "level " + sol.depth); traversed.Push(sol); if (sol.Equals(goal)) { found = true; BFS.AddFirst("Goal:" + sol.ToString()); break; } else { sol.getneighbors(); foreach (Sol neighbor in sol.neighbors) { if (!neighbor.containedin(traversed) && !neighbor.containedin(nextStack)) { neighbor.depth = (sol.depth + 1); nextStack.Push(neighbor); } } } } return found; } public virtual bool depthFirst(Sol start, Sol goal) { Stack<Sol> nextStack = new Stack<Sol>(); Stack<Sol> traversed = new Stack<Sol>(); bool found = false; start.depth = 0; nextStack.Push(start); while (nextStack.Count != 0) { //Dequeue next State for comparison //And add it 2 list of traversed States Sol sol = nextStack.Pop(); DFS.AddFirst("poped State:" + sol.ToString() + "level " + sol.depth); traversed.Push(sol); if (sol.Equals(goal)) { found = true; DFS.AddFirst("Goal:" + sol.ToString()); break; } else { sol.getneighbors(); foreach (Sol neighbor in sol.neighbors) { if (!neighbor.containedin(traversed) && !neighbor.containedin(nextStack)) { neighbor.depth = sol.depth + 1; nextStack.Push(neighbor); } } } } return found; } public virtual bool iterativedeepening(Sol start, Sol goal) { bool found = false; for (int level = 0; ; level++) { Stack<Sol> nextStack = new Stack<Sol>(); Stack<Sol> traversed = new Stack<Sol>(); start.depth = 0; nextStack.Push(start); while (nextStack.Count != 0) { Sol sol = nextStack.Pop(); IDS.AddFirst("poped State:" + sol.ToString() + "Level" + sol.depth); traversed.Push(sol); if (sol.Equals(goal)) { found = true; IDS.AddFirst("Goal:" + sol.ToString()); break; } else if (sol.depth < level) { sol.getneighbors(); foreach (Sol neighbor in sol.neighbors) { if (!neighbor.containedin(traversed) && !neighbor.containedin(nextStack)) { neighbor.depth = sol.depth + 1; nextStack.Push(neighbor); } //end if } //end for each } //end else if } // end while if (found == true) break; } // end for return found; } } } Just wondering if I may be doing something wrong somewhere or something.

    Read the article

1 2 3 4 5 6 7  | Next Page >