Created
April 16, 2013 02:11
-
-
Save kruvas/5392812 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'linklist.class.php'; | |
class obJect { | |
public $nameOfOb; | |
public $typeOfOb; | |
function __construct($nameOfOb,$typeOfOb) | |
{ | |
$this->nameOfOb = $nameOfOb; | |
$this->typeOfOb = $typeOfOb; | |
} | |
} | |
$sdir = "/home/rajat/qBT_dir"; //Source Directory | |
$ddir = "/home/rajat/ajindertest"; // Destination Directory | |
$len = strlen($sdir); | |
$lnl = new LinkList(); | |
$lnl->insertFirst(new obJect($sdir,"DIR")); | |
dirToList($sdir,$lnl); | |
foreach($lnl->readList() as $key => $value) | |
{ | |
if($value->typeOfOb == "DIR") | |
{ | |
echo "Creating Directory".refmt($value->nameOfOb,$ddir,$len)."......\n"; | |
mkdir(refmt($value->nameOfOb,$ddir,$len),0777); | |
echo "Directory Successfully Created\n"; | |
} | |
elseif($value->typeOfOb == "File") { | |
echo "Copying File ". $value->nameOfOb. " to ".refmt($value->nameOfOb,$ddir,$len)."......\n"; | |
copy($value->nameOfOb,refmt($value->nameOfOb,$ddir,$len)); | |
echo "File Successfully Copied \n"; | |
} | |
} | |
function dirToList($dir,$lnl) | |
{ | |
$cdir = scandir($dir); | |
foreach($cdir as $key => $value) | |
{ | |
if(!in_array($value,array(".",".."))) | |
{ | |
if(is_dir($dir."/".$value)) | |
{ | |
$lnl->insertAfter(new obJect($dir,"DIR"), new obJect($dir."/".$value,"DIR")); | |
dirToList($dir."/".$value,$lnl); | |
} | |
else | |
{ | |
$lnl->insertAfter(new obJect($dir,"DIR"),new obJect($dir."/".$value,"File")); | |
} | |
} | |
} | |
} | |
function refmt($value,$ddir,$len) | |
{ | |
return substr_replace($value, $ddir , 0 , $len); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Title: Single linked list | |
* Description: Implementation of a single linked list in PHP | |
* @author Sameer Borate | codediesel.com | |
* @version 1.0.1 16th August 2012 | |
*/ | |
class ListNode | |
{ | |
/* Data to hold */ | |
public $data; | |
/* Link to next node */ | |
public $next; | |
/* Node constructor */ | |
function __construct($data) | |
{ | |
$this->data = $data; | |
$this->next = NULL; | |
} | |
function readNode() | |
{ | |
return $this->data; | |
} | |
} | |
class LinkList | |
{ | |
/* Link to the first node in the list */ | |
private $firstNode; | |
/* Link to the last node in the list */ | |
private $lastNode; | |
/* Total nodes in the list */ | |
private $count; | |
/* List constructor */ | |
function __construct() | |
{ | |
$this->firstNode = NULL; | |
$this->lastNode = NULL; | |
$this->count = 0; | |
} | |
public function isEmpty() | |
{ | |
return ($this->firstNode == NULL); | |
} | |
public function insertFirst($data) | |
{ | |
$link = new ListNode($data); | |
$link->next = $this->firstNode; | |
$this->firstNode = &$link; | |
/* If this is the first node inserted in the list | |
then set the lastNode pointer to it. | |
*/ | |
if($this->lastNode == NULL) | |
$this->lastNode = &$link; | |
$this->count++; | |
} | |
public function insertLast($data) | |
{ | |
if($this->firstNode != NULL) | |
{ | |
$link = new ListNode($data); | |
$this->lastNode->next = $link; | |
$link->next = NULL; | |
$this->lastNode = &$link; | |
$this->count++; | |
} | |
else | |
{ | |
$this->insertFirst($data); | |
} | |
} | |
public function deleteFirstNode() | |
{ | |
$temp = $this->firstNode; | |
$this->firstNode = $this->firstNode->next; | |
if($this->firstNode != NULL) | |
$this->count--; | |
return $temp; | |
} | |
public function deleteLastNode() | |
{ | |
if($this->firstNode != NULL) | |
{ | |
if($this->firstNode->next == NULL) | |
{ | |
$this->firstNode = NULL; | |
$this->count--; | |
} | |
else | |
{ | |
$previousNode = $this->firstNode; | |
$currentNode = $this->firstNode->next; | |
while($currentNode->next != NULL) | |
{ | |
$previousNode = $currentNode; | |
$currentNode = $currentNode->next; | |
} | |
$previousNode->next = NULL; | |
$this->count--; | |
} | |
} | |
} | |
public function deleteNode($key) | |
{ | |
$current = $this->firstNode; | |
$previous = $this->firstNode; | |
while($current->data != $key) | |
{ | |
if($current->next == NULL) | |
return NULL; | |
else | |
{ | |
$previous = $current; | |
$current = $current->next; | |
} | |
} | |
if($current == $this->firstNode) | |
{ | |
if($this->count == 1) | |
{ | |
$this->lastNode = $this->firstNode; | |
} | |
$this->firstNode = $this->firstNode->next; | |
} | |
else | |
{ | |
if($this->lastNode == $current) | |
{ | |
$this->lastNode = $previous; | |
} | |
$previous->next = $current->next; | |
} | |
$this->count--; | |
} | |
public function find($key) | |
{ | |
$current = $this->firstNode; | |
while($current->data != $key) | |
{ | |
if($current->next == NULL) | |
return null; | |
else | |
$current = $current->next; | |
} | |
return $current; | |
} | |
public function insertAfter($key,$key2) | |
{ | |
$prev = $this->find($key); | |
$curr = new ListNode($key2); | |
$curr->next=$prev->next; | |
$prev->next = $curr; | |
} | |
public function readNode($nodePos) | |
{ | |
if($nodePos <= $this->count) | |
{ | |
$current = $this->firstNode; | |
$pos = 1; | |
while($pos != $nodePos) | |
{ | |
if($current->next == NULL) | |
return null; | |
else | |
$current = $current->next; | |
$pos++; | |
} | |
return $current->data; | |
} | |
else | |
return NULL; | |
} | |
public function totalNodes() | |
{ | |
return $this->count; | |
} | |
public function readList() | |
{ | |
$listData = array(); | |
$current = $this->firstNode; | |
while($current != NULL) | |
{ | |
array_push($listData, $current->readNode()); | |
$current = $current->next; | |
} | |
return $listData; | |
} | |
public function reverseList() | |
{ | |
if($this->firstNode != NULL) | |
{ | |
if($this->firstNode->next != NULL) | |
{ | |
$current = $this->firstNode; | |
$new = NULL; | |
while ($current != NULL) | |
{ | |
$temp = $current->next; | |
$current->next = $new; | |
$new = $current; | |
$current = $temp; | |
} | |
$this->firstNode = $new; | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment