Data structure linear table code
A linear table is a finite sequence of n elements with the same data characteristics. It is the most basic and commonly used linear structure (linear lists, stacks, queues, strings and arrays are all linear structures), and it is also the basis of other data structures.
Characteristics of non-empty linear tables or linear structures:
(1) There is only one data element called "first";
( 2) There is only one data element called the "last";
(3) Except for the first one, each data element in the structure has only one predecessor;
(4) Except for the last one, each data element in the structure has only one successor;
Linear table structure sequence representation (sequential table)
Concept: use A group of storage units with consecutive addresses stores the data elements of a linear table in sequence. The linear table of this storage structure is called a sequential table.
Features: Logically adjacent data elements are also adjacent in physical order.
As long as the starting position of the linear table is determined, any data element in the linear table can be randomly accessed, so the sequential storage structure of the linear table is a random access storage structure, because the advanced The array type in the language also has random access characteristics, so we usually use arrays to describe the sequential storage structure in the data structure, and use dynamically allocated one-dimensional arrays to represent linear tables.
The following is the code to use PHP to implement the data structure linear table (sequential table):
<?php class ArrayList{ private $list; private $size; public function __construct() { $this->list=array(); $this->size=0; } //初始化链表 public function InitList(){ $this->list=array(); $this->size=0; } //删除链表 public function destoryList(){ if (isset($this->list)){ unset($this->list); $this->size=0; } } //清空链表 public function clearList(){ if (isset($this->list)){ unset($this->list); } $this->list=array(); $this->size=0; } //判断链表是否为空 public function emptyList(){ if (isset($this->list)){ if ($this->size==0){ return true; }else{ return false; } } } //链表长度 public function lengthList(){ if (isset($this->list)){ return $this->size; }else{ return false; } } //取元素 public function getElem($i){ if ($i<1||$i>$this->size){ die('failed'); } if (isset($this->list)&&is_array($this->list)){ return $this->list[$i-1]; } } //是否在链表中 public function locateElem($e){ if (isset($this->list)&&is_array($this->list)){ for ($i=0;$i<$this->size;$i++){ if ($this->list[$i]==$e){ return $i+1; } return 0; } } } //前驱 public function priorElem($i){ if ($i<1||$i>$this->size){ die('failed'); } if ($i==1){ die('no prior'); } if (isset($this->list)&&is_array($this->list)){ return $this->list[$i-2]; } } //后继 public function nextElem($i){ if ($i<1||$i>$this->size){ die('failed'); } if ($i==$this->size){ die('no next'); } if (isset($this->list)&&is_array($this->list)){ return $this->list[$i]; } } //插入元素 public function insertList($i,$e){ if ($i<1||$i>$this->size){ die('failed'); } if (isset($this->list)&&is_array($this->list)){ if ($this->size==0){ $this->list[0]=$e; $this->size++; }else{ for($j=$this->size-1;$j>=$i;$j--){ $this->list[$j]=$this->list[$j-1]; } $this->list[$i-1]=$e; $this->size++; } } } //删除元素 public function deleteList($i){ if ($i<1||$i>$this->size){ die('failed'); } if (isset($this->list)&&is_array($this->list)){ if ($i==$this->size){ unset($this->list[$i-1]); }else{ unset($this->list[$i-1]); for ($j=$i;$j<$this->size;$j++){ $this->list[$j-1]=$this->list[$j]; } } $this->size--; } } //遍历 public function printList(){ if (isset($this->list)&&is_array($this->list)){ foreach ($this->list as $value) { echo $value.' '; } } } }
For more PHP-related knowledge, please visit PHP Chinese website!
The above is the detailed content of Data structure linear table code. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment