Matlab / Java Linked List Error
Hi
i'm working on my Final university Project , and my time is too short the project is about Wireless sensor network and i have to simulate one of the recent papers about it. most of the coding is done but i have a simple problem about linked list(i use matlab and import java libraries into it for using linked list)
okay here is some explanation i have a queue/list that every time head of the list is checked that is it the goal or not if its not a goal it deletes the first item and gets the second one which is actualy the first one in the list problem occurs when my list has only 1 item and its not goal so code delete the first item , AFTER that code tries to get the first item which is null and empty thats when Error happens ,
my goal is after checking/searching all the list if it was successful turn flagg=1 if not flagg=0
while temp ~= goal;
for K=1:size(G,1)
if temp == G(K,1)
q.add(G(K,2))
end
end
if (~q.isEmpty())
q.removeFirst();
temp=q.getFirst();
else
flagg=1
end
end
Thanks. M.Dadmand
Re: Matlab / Java Linked List Error
What happens when the list only has a single element? Trace through this with a piece of paper and a pencil if you have to.
Re: Matlab / Java Linked List Error
thank you so much for reply
let me explain more
first my search is Breadth first search
if the algorithm find the goal it will break the While and everything is fine
but at the moment no result has found (that means the queue is empty)
i have a matrix like this :
[1,2; 1,3 ; 1,5 ; 3,8 ; 5,10]
i input 1 by hand for the first number in queue
it searches the 1 in my matrix (only the first column) we have 3 ones in our first column
so it add 2 , 3 , 5 to the queue
after finish searching in my matrix it deletes the first one (q.removeFirst();)
and gets the first one so now is 2
loop repeats until we are at 10 and no goal found yet
so at this line :
q.removeFirst();
temp=q.getFirst();
it removes 10 and tries to get first item which is nothing/null and that's when error happens
so what i want to do after that is turn a flag 1 and come out of my while loop
if i use poll method instead of these
q.removeFirst();
temp=q.getFirst();
lines everything could work fine , saddly matlab gives me this error for it
Undefined function 'pollFirst' for input arguments of type 'double'.
Error in TestFInal2 (line 102)
pollFirst(temp)
EDIT : problem solved , i just added a break in while
so it breaks the while NOT the whole code
btw i was stuck at in in weeks :D
i have another problem its about graph should i make another thread ?