Search
 
SCRIPT & CODE EXAMPLE
 

PHP

can we create linked list in php

<?php
//node structure
class Node {
public $data;
public $next;
}
class LinkedList {
public $head;
public function __construct(){
$this->head = null;
}

//Add new element at the end of the list
public function push_back($newElement) {
$newNode = new Node();
$newNode->data = $newElement;
$newNode->next = null;
if($this->head == null) {
$this->head = $newNode;
} else {
$temp = new Node();
$temp = $this->head;
while($temp->next != null) {
$temp = $temp->next;
}
$temp->next = $newNode;
}
}
//display the content of the list
public function PrintList() {
$temp = new Node();
$temp = $this->head;
if($temp != null) {
echo “
The list contains: “;
while($temp != null) {
echo $temp->data.” “;
$temp = $temp->next;
}
} else {
echo “
The list is empty.”;
}
}
};
// test the code
$MyList = new LinkedList();
//Add three elements at the end of the list.
$MyList->push_back(10);
$MyList->push_back(20);
$MyList->push_back(30);
$MyList->PrintList();
//The output of the above code will be:
//The list contains: 10 20 30
?>
Comment

SIMPLE linked list in php

<?php
 // clase de nodo
class Node {
         public $ data; // datos de nodo
         public $ next; // siguiente nodo

    public function __construct($data) {
        $this->data = $data;
        $this->next = NULL;
    }
}
 // Lista enlazada individual
class SingleLinkedList {
       private $ header; // nodo principal

   function __construct($data) {
      $this->header = new Node($data);
   }
       // Encontrar nodo
   public function find($item) {
      $current = $this->header;
      while ($current->data != $item) {
         $current = $current->next;
      }
      return $current;
   }
       // (después del nodo) inserte un nuevo nodo
   public function insert($item, $new) {
       $newNode = new Node($new);
       $current = $this->find($item);
        $newNode->next = $current->next;
      $current->next = $newNode;
      return true;
   }

         // Actualizar nodo
    public function update($old, $new) {
        $current = $this->header;
        if ($current->next == null) {
                         echo "¡La lista vinculada está vacía!";
            return;
        }
        while ($current->next != null) {
            if ($current->data == $old) {
                break;
            }
            $current = $current->next;
        }
        return $current->data = $new;
    }

         // Encuentra el nodo anterior del nodo que se va a eliminar
    public function findPrevious($item) {
        $current = $this->header;
        while ($current->next != null && $current->next->data != $item) {
            $current = $current->next;
        }
        return $current;
    }

         // Eliminar un nodo de la lista vinculada
    public function delete($item) {
        $previous = $this->findPrevious($item);
        if ($previous->next != null) {
            $previous->next = $previous->next->next;
        }
    }

         // Integración de findPrevious y delete
    public function remove($item) {
        $current = $this->header;
        while ($current->next != null && $current->next->data != $item) {
            $current = $current->next;
        }
        if ($current->next != null) {
            $current->next = $current->next->next;
        }
    }

         // Vaciar la lista vinculada
    public function clear() {
       $this->header = null;
    }

         // Mostrar los elementos en la lista vinculada
    public function display() {
        $current = $this->header;
        if ($current->next == null) {
                         echo "¡La lista vinculada está vacía!";
            return;
        }
        while ($current->next != null) {
            echo $current->next->data . "&nbsp;&nbsp;&nbsp";
            $current = $current->next;
        }
    }
}

$linkedList = new SingleLinkedList('header');
$linkedList->insert('header', 'China');
$linkedList->insert('China', 'USA');
$linkedList->insert('USA','England');
$linkedList->insert('England','Australia');
 echo'La lista vinculada es: ';
$linkedList->display();
echo "</br>";
 echo '----- Eliminar nodo USA -----';
echo "</br>";
$linkedList->delete('USA');
 echo'La lista vinculada es: ';
$linkedList->display();
echo "</br>";
 echo '----- Actualizar nodo Inglaterra a Japón -----';
echo "</br>";
    $linkedList->update('England', 'Japan');
 echo'La lista vinculada es: ';
$linkedList->display();
//echo "</br>";
 // echo "----- Lista enlazada vacía -----";
//echo "</br>";
//$linkedList->clear();
//$linkedList->display();

 // salida:
 La lista vinculada es: China Estados Unidos Inglaterra Australia   
 ----- eliminar nodo USA -----
 La lista vinculada es: China Inglaterra Australia   
 ----- Actualizar nodo Inglaterra a Japón -----
 La lista vinculada es: China Japón Australia
Comment

PREVIOUS NEXT
Code Example
Php :: laravel check if item is in collection 
Php :: css not working in live laravel project 
Php :: __dir__ in php 
Php :: transient wordpress 
Php :: php check if a url exists 
Php :: php formData curl 
Php :: php json decode not working on array 
Php :: Group by not working - Laravel 
Php :: ERROR: Could not enable dependency mpm_prefork for php7.4, aborting 
Php :: get unique array from multidimentional array by value in php 
Php :: guzzle http client 
Php :: laravel blade @selected 
Php :: user location using php 
Php :: file exist php 
Php :: ?? ternary operator in php 
Php :: curl php loop 
Php :: run schedule laravel 
Php :: laravel soft delete example 
Php :: Proc file for laravel 
Php :: how to check where function defined in php 
Php :: php call class method dynamically 
Php :: wordpress admin url 
Php :: php redirect with query string 
Php :: wp php go back 
Php :: php password verify 
Php :: static php 
Php :: default language laravel 
Php :: how make a variable global php 
Php :: distinct laravel not working 
Php :: laravel echo html 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =