찾다
php教程PHP源码储存字符串的PHP单链表

跳至 [1] [全屏预览]
<meta charset="utf-8" />
<?
class Node{
    public $data;
    public $next;

    function __construct($data, $next = null){
        $this->data = $data;
        $this->next = $next;
    }    
}

class LinkedList{
    private $head; //单链表的头结点,不存储数据

 function __construct(){//单链表的构造方法
 	//头结点的数据为"傀儡", 不代表 任何数据
    $this->head = new Node("dummy 傀儡"); 
    $this->first = null;
    }

 function isEmpty(){ 
        return ($this->head->next == null);
    }
	
/*	orderInsert($data) 方法, 
 *  按给定字符串 $data 的大小, 将其安插到适当的位置,	
 *  以保证单链表中字符串的存储,始终是有序的。 
 */
 function orderInsert($data){
	$p = new Node($data);	
    if($this->isEmpty()){
        $this->head->next = $p;
    }
    else{
    $q = $this->head;
    while($q->next != NULL && strcmp($data, $q->next->data)> 0 ){
    $q = $q->next;
		}
        $p->next = $q->next; 
		$q->next = $p;
    }     	
 }
 
 function insertLast($data){//将字符串插到单链表的尾部
    $p = new Node($data);
	
    if($this->isEmpty()){
        $this->head->next = $p;
    }
    else{
        $q = $this->head->next;
        while($q->next != NULL)
            $q = $q->next;
        $q->next = $p;  
    }     
}

    function find($value){//查询是否有给定的字符串
        $q = $this->head->next;
        while($q->next != null){
        if(strcmp($q->data,$value)==0){
				break;
			}
            $q = $q->next;    
        }
		if ($q->data == $value)
        return $q; 
		else
		return new Node($value." 没有找到!");
    }

    function traversal(){//遍历单链表
		if(!$this->isEmpty()){
        $p=$this->head->next;
        echo $p->data." ";
        while($p->next != null){
			$p=$p->next;
			echo $p->data." ";
			}			
    	}else
		echo "链表为空!";
	}
} 

$ll = new LinkedList();
$city =array("Wuhan", "Beijing", "Shanghai", "Tianjin", "Changsha", "Kunming", "Gongyi","Tokyo","New York","Ottawa","Moskow","Edmonton","Glasgow","Thunder Bay","New Delhi"); 

for ($i=0;$i<count($city);$i++)
$ll->orderInsert($city[$i]);
$ll->traversal();
echo "<br>".$ll->find("Gongyi")->data."<br>";
echo $ll->find("Baoding")->data."<br>";
?>

2. [图片] output.png    

储存字符串的PHP单链表
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.