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 :: guzzle login example 
Php :: multiline string php 
Php :: laravel eloquent join two models 
Php :: polymorphism in php 
Php :: Get All dates of a month 
Php :: country list laravel 
Php :: create middleware laravel 
Php :: magento Fatal error: Allowed memory size of 134217728 bytes exhausted 
Php :: mail function php not working 
Php :: cakephp 3 make migration 
Php :: php arrays 
Php :: create factory in laravel 8 
Php :: laraodck imagick 
Php :: what is route namespace in laravel 
Php :: pusher 
Php :: laravel with select 
Php :: run cron job in seconds 
Php :: string operator in php 
Php :: cURL error 6: Could not resolve host: api.themoviedb.org (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://api.themoviedb.org/3/movie/popular?api_key=5cb73b68870b70a436b10ea06298de07 
Php :: laravel email verification laravel 8 tutorial 
Php :: split functions.php 
Php :: laravel Call to a member function validate() on array 
Php :: Eloquent orWhere clousure 
Php :: elvis operator php 
Php :: php integer to js integer 
Php :: Include Custom Post Types Categories to the main query 
Php :: cidblike ci3 
Php :: nwidart/laravel-modules seed 
Php :: Validate checkboxes laravel 
Php :: php ffi get load average 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =