Home Backend Development PHP Tutorial PHP implements sequential linear table

PHP implements sequential linear table

Jan 25, 2020 pm 09:58 PM
php order

PHP implements sequential linear table

What is a linear sequence table?

Linear sequence table refers to storage in memory in order, and is connected one by one except the beginning and end (usually expressed in the form of a one-dimensional array).

(Free learning video tutorial sharing: php video tutorial)

The example code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

<?php

/*

 * GetElem: 返回线性表中第$index个数据元素

 * ListLength: 返回线性表的长度

 * LocateElem: 返回给定的数据元素在线性表中的位置

 * PriorElem: 返回指定元素的前一个元素

 * NextElem: 返回指定元素的后一个元素

 * ListInsert: 在第index的位置插入元素elem

 * ListDelete: 删除第index位置的元素elem

 */

class Sequence {

  public $seqArr;

  public $length;

  public function __construct($arr) {

    $this->seqArr = $arr;

    $this->length = count($arr);

  }

  /*

   * 返回线性表中第$index个数据元素

   */

  public function GetElem($index) {

    if (($this->length) == 0 || $index < 0 || ($index > $this->length)) {

      return "Error";

    }

    return $this->seqArr[$index - 1];

  }

  /*

   * 返回线性表的长度

   *

   */

  public function ListLength() {

    return $this->length;

  }

  /*

   * 返回给定的数据元素在线性表中的位置

   */

  public function LocateElem($elem) {

    for ($i = 0; $i < ($this->length); $i++) {

      if (($this->seqArr[$i]) == $elem) {

        return $i + 1;

      }

    }

  }

  /*

   * PriorElem: 返回指定元素的前一个元素

   */

  public function PriorElem($elem) {

    for ($i = 0; $i < ($this->length); $i++) {

      if (($this->seqArr[$i]) == $elem) {

        if ($i == 0) {

          return "Error (is null) ";

        } else {

          return $this->seqArr[$i - 1];

        }

      }

    }

  }

  /*

   * NextElem: 返回指定元素的后一个元素

   */

  public function NextElem($elem) {

    for ($i = 0; $i < ($this->length); $i++) {

      if (($this->seqArr[$i]) == $elem) {

        return $this->seqArr[$i + 1];

      }

    }

  }

  /*

   * ListInsert: 在第index的位置插入元素elem

   */

  public function ListInsert($index, $elem) {

    if (($this->length) == 0 || $index < 0 || $index > ($this->length)) {

      return "Error";

    }

    for ($i = $index; $i < ($this->length); $i++) {

      $this->seqArr[$i + 1] = $this->seqArr[$i];

    }

    $this->seqArr[$index] = $elem;

    $this->length = $this->length + 1;

    return $this->seqArr;

  }

  /*

   * ListDelete: 删除第index位置的元素

   */

  public function ListDelete($index) {

    if (($this->length) == 0 || $index < 0 || $index > ($this->length - 1)) {

      return "Error";

    }

    unset($this->seqArr[$index]);

    $this->length--;

    return $this->seqArr;

  }

}

?>

Copy after login

Related article tutorial sharing: php tutorial

The above is the detailed content of PHP implements sequential linear table. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles