Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Linked List Alternative


[SOLVED] Linked List Alternative

Reply
  #1 (permalink)  
Old 01-28-09, 09:37 PM
sushi4664's Avatar
sushi4664 sushi4664 is offline
Aspiring Coder
 
Join Date: Apr 2007
Location: USA
Posts: 411
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Linked List Alternative

I have a structural question.

I come from a Java background. In Java, there was a premade class called LinkedList. Essentially I want something like that.

The idea is, I have an array, example:

Code:
$exampleArr=array(1,2,3,4);
When I call the following snippet of code:

Code:
for($i=0;$i<7;$i++)
    echo $exampleArr[$i];
I want the following output:

1
2
3
4
1
2
3

So essentially I want an array type structure that goes back to its first element after its last one.

Is there a simple way with php?
__________________
- sushi
Visit http://napkinz.com/index.php - web comic that is update weekly

-ps: got through the archive...there are really funny comics in there....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-28-09, 09:58 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Like this:
PHP Code:

<?php
$exampleArr
=array(1,2,3,4);
for(
$count=0;$count<2;$count++)
{
 for(
$i=0;$i<count($exampleArr);$i++)
 {
  echo 
$exampleArr[$i]."<br />";
  }
 }
?>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 01-28-09, 10:29 PM
sushi4664's Avatar
sushi4664 sushi4664 is offline
Aspiring Coder
 
Join Date: Apr 2007
Location: USA
Posts: 411
Thanks: 0
Thanked 0 Times in 0 Posts
But for the code I am writing, I don't know how many elements the array has, nor do I know how many iterations it is going to be used for,
__________________
- sushi
Visit http://napkinz.com/index.php - web comic that is update weekly

-ps: got through the archive...there are really funny comics in there....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 01-28-09, 10:34 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I guess this would be a representation of a
Singly-linked list


This one shifts from top to bottom:
PHP Code:

<?php
$exampleArr 
= array(1,2,3,4);
 for(
$i=0;$i<7;$i++)
 {
  
$value array_shift($exampleArr);
  echo 
$value."<br />";
  
array_push($exampleArr,$value);
  }
?>
This one shifts from top to bottom and has a pointer to start anywhere in the array:
PHP Code:

<?php
$exampleArr 
= array(1,2,3,4);
$pointer 1// Use values 1 thru count($exampleArr)
for($i=0;$i<7;$i++)
{
 
$value array_shift($exampleArr);
 
array_push($exampleArr,$value);
 echo 
$exampleArr[count($exampleArr)-$pointer]."<br />";
 }
?>
This one shifts from bottom to top:
PHP Code:

<?php
$exampleArr 
= array(1,2,3,4);
 for(
$i=0;$i<7;$i++)
 {
  
$value array_pop($exampleArr);
  echo 
$value."<br />";
  
array_unshift($exampleArr,$value);
  }
?>
This one shifts from bottom to top and has a pointer to start anywhere in the array:
PHP Code:

<?php
$exampleArr 
= array(1,2,3,4);
$pointer 1// Use values 1 thru count($exampleArr)
for($i=0;$i<7;$i++)
{
 
$value array_pop($exampleArr);
 
array_unshift($exampleArr,$value);
 echo 
$exampleArr[count($exampleArr)-$pointer]."<br />";
 }
?>
__________________
Jerry Broughton

Last edited by job0107; 01-28-09 at 11:04 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 01-29-09, 04:47 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Here is where the modulus operator comes handy:
PHP Code:

<?php


$array 
= array(1234);
$count sizeof($array);

for (
$i 0$i 7$i++)
{
    
$key = ($i $count);
    
    echo 
$array[$key], "<br />\n";
}

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] CLL - Circular Linked List sushi4664 PHP 6 06-26-08 09:11 PM
how to write linked list darksystem Everything Java 2 04-19-07 10:04 AM
how is the program of the INFIX to POSTFIX in stack using linked list? venice C/C++ 1 08-10-04 12:59 AM


All times are GMT -5. The time now is 07:12 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.