LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2017, 11:26 AM   #1
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,638

Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
I'm back: how to loop through a link list to infinity


what I am trying to do is get my program to get a different filename at X=time intervals.

using a while loop off the command line for now. Being this.

Code:
~ sixth-run >  while true ; do ./disfile  -C -q  /media/data/test  ; sleep 2s ; done
count 0 load_image 1
in cycle_images /media/data/test/sub1/DispicableMe-1-24724.png
count 0 load_image 1
in cycle_images /media/data/test/sub1/DispicableMe-1-24724.png
count 0 load_image 1
in cycle_images /media/data/test/sub1/DispicableMe-1-24724.png
count 0 load_image 1
in cycle_images /media/data/test/sub1/DispicableMe-1-24724.png

this is my function
Code:
char *cycle_images(mylist *list)
{
 mylist* head = list;

// place keeper for top of list (first image)

   head->prev = head; 

// because head->prev is not being used for anything else at the moment. 

 while(head)
 {
   // test to be sure it is being called and what it is doing
  
		
    head = head->next;
 
    printf("in cycle_images %s\n", head->filename);
    
    return(head->filename);
     
     // set it back to the beginning of the list
     if(head->next == NULL)
        head = head->prev;
 }
// it should never reach this
 return(head->filename);
}
the place where it is called code:
Code:
 char *filename ;
 int c = 0;

 if(opts.cycle_images == 1 )
 {
   printf("count %d load_image %d\n",c, opts.cycle_images);
   filename = cycle_images(filelist);
    c++;
   }
   else if(get_im_name())
     filename = get_im_name();

   if (!filename)
   {
     printf("forgot file yea?!\n");
     exit(1);
    }
For now the question is:
is that char *cycle_images(mylist *list) function set up properly to actually reset the list to the front so it just keeps cycling over and over again?

looking at it as if it is, right now as I write this, I see that even if is keeps getting called it will always reset to the front and never actually traverse down the list then repeat due to it being called again and again, that just starts it from the top again.

nevertheless, I still need to know if that is a proper means of causing it to loop continuously so I can figure out how to get it to stay in that function and keep cycling and send the next filename at a given interval continuously looping.
 
Old 08-12-2017, 12:13 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,462

Rep: Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778
Quote:
Originally Posted by BW-userx View Post
is that char *cycle_images(mylist *list) function set up properly to actually reset the list to the front so it just keeps cycling over and over again?
This is not the best approach. If you want to loop around, you need to set last->next=first in the list and your function will automatically cycle.

From the other hand first->prev and last->next are usually set to NULL and you can also use: if item->next is null, use first.
 
1 members found this post helpful.
Old 08-12-2017, 01:01 PM   #3
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,638

Original Poster
Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
Quote:
Originally Posted by pan64 View Post
This is not the best approach. If you want to loop around, you need to set last->next=first in the list and your function will automatically cycle.

From the other hand first->prev and last->next are usually set to NULL and you can also use: if item->next is null, use first.
I tell you this is completely frustrating. Before checking my email for this. I've been experimenting with that function. TO me logically it is just like a print list function, start at beginning then cycle through it, when it gets to the last one just have it start over.

so printing it out I get a directory path with no file in it stuck in the filename member, blows up my program.

I check for what is being put into it, and it is nothing but filenames - but out put I get a directory name ...

so I did a for loop

Code:
int longth = list_length(list);
int i = 0;

for ( i = 0; i < longth -1; i++)
{
  printf("%d [%s\n]-->",longth, head->filename);
  head = head->next;
}
run it and have it load an image and I still get a dir name without a filename. Do not know where it is getting that dir entry as a file and not DIrectory.

Code:
> ./disfile -C  -q /media/data/test
7 [/media/data/test/sub1/DispicableMe-1-24724.png
]-->7 [/media/data/test/sub1/DispicableMe-1-11824.png
]-->7 [/media/data/test/sub1/DispicableMe-1-10424.png
]-->7 [/media/data/test/DispicableMe-1-27124.png
]-->7 [/media/data/test/DispicableMe-1-25824.png
]-->7 [/media/data/test/DispicableMe-1-10824.png
]-->count 0 load_image 1
//here is the filename returned. 
loadimage /media/data/test
in main
plus when I put a return (head->filename) it will not even loop. it only spits out one then quits.

-- so now that I've ranted my frustrations let me try what you suggested.
Code:
This is not the best approach. If you want to loop around, 
you need to set last->next=first in the list and your function
 will automatically cycle.
I got to figure out a why to know what the last one is first so I can assign it to.

Code:
last->next = first
or like check for null then go back one then set that to the head?

Last edited by BW-userx; 08-12-2017 at 01:13 PM.
 
Old 08-12-2017, 01:31 PM   #4
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,638

Original Poster
Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
Quote:
Originally Posted by pan64 View Post
This is not the best approach. If you want to loop around, you need to set last->next=first in the list and your function will automatically cycle.

From the other hand first->prev and last->next are usually set to NULL and you can also use: if item->next is null, use first.
WELL I still now have to find out why I am getting a dir path without a filename,
Code:
char *cycle_images(mylist *list)
{
 mylist* current, *temp;
    current = temp = list;

    while(current->next != NULL)
    {
        temp = current;
        current = current -> next;
        printf("%s\n",current->filename);
    }
    temp->next = list;
    current->next = list->next;
    list->next = NULL;
    list = current;

return(current->filename);

}
I call it in main to print out as if it is a print list
Code:
int main (int argc, char **argv){
init_imglib_x();
init_system_options(argc,argv);
//check_options();
set_images(opts.mode);
printf("in main\n");
printf("\n");

cycle_images(filelist);

return 0;
} // end main
and I end up getting this in the end:
Code:
userx%slackwhere š sixth-run š> ./disfile -C  -q /media/data/test
/media/data/test/sub1/DispicableMe-1-11824.png
/media/data/test/sub1/DispicableMe-1-10424.png
/media/data/test/DispicableMe-1-27124.png
/media/data/test/DispicableMe-1-25824.png
/media/data/test/DispicableMe-1-10824.png
/media/data/test
count 0 load_image 1
loadimage /media/data/test <--- that one!
in main
the "loadimage /media/data/test" is what gets returned to the load_image function as a filename and I get a black desktop instead of an image filename to load and set instead.

Last edited by BW-userx; 08-12-2017 at 01:32 PM.
 
Old 08-12-2017, 01:38 PM   #5
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,638

Original Poster
Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
BUT when I add that return in the loop I only get one then it quits, but I guess that is the nature of the beast (return) that whence it returns it stops. So I need to figure out how to get it to keep sending then next file to load in a continuous loop.
Code:
char *cycle_images(mylist *list)
{
 mylist* current, *temp;
    current = temp = list;

    while(current->next != NULL)
    {
        temp = current;
        current = current -> next;
        printf("%s\n",current->filename);
        return(current->filename);
    }
    temp->next = list;
    current->next = list->next;
    list->next = NULL;
    list = current;
printf("justbefore last return\n");
return(current->filename);

}
Code:
> ./disfile -C  -q /media/data/test
/media/data/test/sub1/DispicableMe-1-11824.png
count 0 load_image 1
loadimage /media/data/test/sub1/DispicableMe-1-11824.png
in main

/media/data/test/sub1/DispicableMe-1-11824.png <- gets me that one image.
 
Old 08-12-2017, 03:33 PM   #6
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,638

Original Poster
Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
I'm going to finish my program and deal with this little tid bit later.
It's not a necessary function.
 
Old 08-13-2017, 01:00 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,417
Blog Entries: 6

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
I have had to defer thinking about this today while dealing with a hardware failure and restoring from backups (the day they tell you about...). In the mean time I see you have marked it solved, but will add my collected thoughts, hopefully they will be of use.

Quote:
Originally Posted by BW-userx View Post
I still need to know if that is a proper means of causing it to loop continuously so I can figure out how to get it to stay in that function and keep cycling and send the next filename at a given interval continuously looping.
No, once you return from that function the loop has ended. Think about your own description, it contains a key part of the problem description...

Quote:
...keep cycling and send the next filename...
To send the next of something within a loop, the loop needs to be the caller (i.e. sender), and it must have knowledge of both the sequence and the current state so it can determine the next state.

Your loop is the called function and returns only once, as you have seen.

You should not change the prev, next pointers of list elements within the loop. That changes the list and is likely responsible for the corrupted list data you mentioned in your other thread.

As pan64 has pointed out, the prev pointer at the head of the list, and the next pointer at the tail are usually NULL - that is how you know when you are at the end, and also makes it easy to prepend or append more elements to the list. When you make them non-null you make a list with no free end(s).

Quote:
Originally Posted by pan64 View Post
From the other hand first->prev and last->next are usually set to NULL and you can also use: if item->next is null, use first.
The part in red is the key to making it loop continuously, per your original question. So the logic would be something like this...

Code:
mylist *list;

  /* Initialize list by appending elements to tail */
...
  /* Set current state to head of list */
mylist *current = list;

while(1){
   send_somewhere(current->filename);
   if(current->next)
      current = current->next
   else
      current = list
}
 
Old 08-13-2017, 01:13 PM   #8
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,638

Original Poster
Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
Quote:
Originally Posted by astrogeek View Post
I have had to defer thinking about this today while dealing with a hardware failure and restoring from backups (the day they tell you about...). In the mean time I see you have marked it solved, but will add my collected thoughts, hopefully they will be of use.



No, once you return from that function the loop has ended. Think about your own description, it contains a key part of the problem description...



To send the next of something within a loop, the loop needs to be the caller (i.e. sender), and it must have knowledge of both the sequence and the current state so it can determine the next state.

Your loop is the called function and returns only once, as you have seen.

You should not change the prev, next pointers of list elements within the loop. That changes the list and is likely responsible for the corrupted list data you mentioned in your other thread.

As pan64 has pointed out, the prev pointer at the head of the list, and the next pointer at the tail are usually NULL - that is how you know when you are at the end, and also makes it easy to prepend or append more elements to the list. When you make them non-null you make a list with no free end(s).



The part in red is the key to making it loop continuously, per your original question. So the logic would be something like this...

Code:
mylist *list;

  /* Initialize list by appending elements to tail */
...
  /* Set current state to head of list */
mylist *current = list;

while(1){
   send_somewhere(current->filename);
   if(current->next)
      current = current->next
   else
      current = list
}
I'll chew on this, but I marked it solved because I believe I got enough to start with when I get back around to that timer thing that is what I am going to use to try ad figure out how to use it for a trigger to fire it off to send the next file in a sequential list.

like inside of that function that way it'd hold its place then when it runs again it will already be set up for the next file in the list. But this is all just in theory within my head right now.

As I stripped out that link list code I was "using" that person has it set up to create list put that list into a bigger list, and I felt that was more than I nee. So I took what I learned from that and created one of my own that is not so complex.

as all I need is a list of filenames to cycle through them in order to make a option to cycle through then on ones desktop.

Now I am back to figuring out and testing every way posable I can think of to blow it up or make it not work off the command line so I can catch the errors and handle them to prevent it from blowing up or not working.

But I might stop and give that a few minutes of my time and book mark this page so I will have it to come back to. either way.

thanks again, and I hope you have recovered from your catastrophe well enough.

Last edited by BW-userx; 08-13-2017 at 01:16 PM.
 
Old 08-16-2017, 11:56 AM   #9
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 678

Rep: Reputation: 105Reputation: 105
Just for laughs and giggles, here's how you could do this with a linked list without loose ends.

First you setup your list and then you can tie the beginning to the end and the end to the beginning, so you have a seamless double-linked list.

Code:
mylist *list;

  /* Initialize list by appending elements to tail (you may want to keep track of your first and last element)*/
...
  /* tie the end to the beginning and the beginning to the end */
  if(!list->previous)
     list->previous=last;
  if(!list->next)
     list->next=first;

  /* Set current state to head of list */
mylist *current = list;

while(1){
   send_somewhere(current->filename);
   current = current->next
}
One thing leads to another: you'd like to add/remove elements at the correct positions, right?
Code:
/* this is not tested code, just a bit of thoughts from the top of my head, might be I forget a thing or two */
mylist *add_element_after(mylist *position, mylist *element)
{
  mylist *temp_next, *temp_prev;

  temp_next=position->next;

  position->next=element;
  element->next=temp_next;
  element->previous=position;
  temp_next->previous=element;

  return element;
}
I hope I did that right ;-) I do love double-linked lists, so you can go forward and backward...
 
Old 08-16-2017, 07:07 PM   #10
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,638

Original Poster
Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
I've settled with this one I figured out
Code:
mylist *list_time_jump(mylist *l, int num)
{
    int i;
    mylist *ret = NULL;

    if(l)
      ret = l;
    else
     ret = filelist;
    
//limits it movement
for (i = 0; i < num; i++)
{
  if (ret->next)
  {
     ret = ret->next;
  }
  else
 {
   ret = filelist;
 }
}
return (ret);
}
for the use with my timer.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Symbolic infinity loop on Chromium install aquaboot Linux Mint 8 05-21-2012 04:49 PM
how to make link list and add and delete list node in shared memory golden_boy615 Programming 1 12-18-2011 06:52 AM
Loop back address sulekha Linux - Networking 2 08-01-2008 07:08 AM
What is a loop back device? kd7edh Linux - General 1 01-17-2006 07:19 PM
Loop back Interface siddiqu Linux - Networking 2 11-17-2001 08:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration