前端工程師應該學習的數據結構

這段時間惡補了數據結構。雖然工作中用處不大,但是這是計算機基礎。畢竟,掌握原理,才能獲得真正的自由

數據結構彙總
這次學習了以下數據結構,並使用JavaScript對其一一封裝

  • 隊列
  • 鏈表

棧和隊列

首先棧和隊列都是線性儲存數據的一種方式,要理解它們都是存儲數據的,再者是線性存儲的。

兩者有什麼不同?

棧的儲存數據規則是先進後出

隊列的儲存數據規則是先進先出

注意:怎麼進怎麼出,只是在數據存儲時的一種規則,這樣符合此規則的場景下使用該數據類型會顯得更高效。

因為JavaScript沒有對應的數據類型,所以如果我們要使用,需要自己封裝,我們基於數組來簡單的封裝下:

<code>

class

Z

{

constructor

() {

this

.items = []; } /<code>

隊列

<code>

class

D

{

constructor

() {

this

.items = []; } push(item) {

this

.items.push(item); } pop() {

this

.items.shift(); } isEmpty() {

return

this

.items.length ===

0

; } len() {

return

this

.items.length; } }/<code>

鏈表

什麼是鏈表?

鏈表是一種將數據存儲到“結點”中的數據結構,需要存儲多少個數據,就生成多少個“結點”,把這些“結點”用指針掛接起來


鏈表的分類

常見的鏈表有單項列表和雙向鏈表兩種

單項鍊表

每個結點有兩部分組成,分別是next(指向下一個結點的data)和自身的data,如圖所示:

前端工程師應該學習的數據結構

雙向鏈表每個結點有三部分組成,分別是next(指向下一個結點的data)prev(指向上一個結點的data)及自身結點的data。如圖所示:

前端工程師應該學習的數據結構

其中:head結點指針指向第一個結點,tail指針指向最後一個結點

單項鍊表封裝

<code>function linkedList() {
     
    function Node(

data

) {

this

.

data

=

data

;

this

.next =

null

; }

this

.head =

null

;

this

.length =

0

; linkedList.prototype.append = function(

data

) {

var

newNode = new Node(

data

);

if

(

this

.length ==

0

) {

this

.head = newNode }

else

{

var

current =

this

.head;

while

(current.next) { current = current.next; } current.next = newNode; }

this

.length++; } linkedList.prototype.insert = function(position,

data

) {

if

(position

0

|| position >

this

.length) {

return

false

; }

var

newNode = new Node(

data

);

if

(position ==

0

) { newNode.next =

this

.head;

this

.head = newNode; }

else

{

var

index =

0

;

var

current =

this

.head;

var

previous =

null

;

while

(index++ < position) { previous = current; current = current.next; } newNode.next = current; previous.next = newNode; }

this

.length++;

return

true

; } linkedList.prototype.

get

= function(position) {

if

(position

0

|| position >=

this

.length) {

return

null

; }

var

current =

this

.head;

var

index =

0

;

while

(index++ < position) { current = current.next; }

return

current.next; } linkedList.prototype.indexOf = function(

data

) {

var

current =

this

.head;

var

index =

0

;

while

(current) {

if

(current.

data

==

data

) {

return

index; } current = current.next; index++; }

return

-

1

; } linkedList.prototype.updata = function(position,

data

) {

if

(position

0

|| position >=

this

.length) {

return

false

; }

var

current =

this

.head;

var

index =

0

;

while

(index++ < position) { current = current.next; } current.

data

=

data

;

return

true

; } linkedList.prototype.removeAt = function(position) {

if

(position

0

|| position >=

this

.length) {

return

false

; }

if

(position ==

0

) {

this

.head =

this

.head.next; }

else

{

var

current =

this

.head;

var

previous =

null

;

var

index =

0

;

while

(index++ < position) { position = current; current = current.next; } previous.next = current.next; }

this

.length -=

1

;

return

true

; } linkedList.prototype.toString = function() {

var

current =

this

.head;

var

listString =

''

;

while

(current) { listString += current.

data

+

' '

current = current.next; }

return

listString; } }/<code>

雙向鏈表封裝

<code>function DoublyList(){
  

this

.head =

null

;

this

.tail =

null

;

this

.length =

0

; function Node(

data

){

this

.

data

=

data

;

this

.prev =

null

;

this

.next =

null

; } DoublyList.prototype.append = function(

data

){

var

newNode = new Node(

data

);

if

(

this

.length ==

0

){

this

.head = newNode;

this

.tail = newNode; }

else

{ newNode.prev =

this

.tail;

this

.tail.next = newNode;

this

.tail = newNode; }

this

.length +=

1

; } DoublyList.prototype.insert = function(position,

data

){

if

(position<

0

|| position>

this

.length)

return

false

;

var

newNode = new Node(

data

);

if

(

this

.length ==

0

){

this

.head = newNode;

this

.tail = newNode; }

if

(position ==

0

){

this

.head.prev = newNode; newNode.next =

this

.head;

this

.head = newNode; }

else

if

(position ==

this

.length){ newNode.prev =

this

.tail;

this

.tail.next = newNode;

this

.tail = newNode; }

else

{

var

current =

this

.head;

var

index =

0

;

while

(index++ /<code>


分享到:


相關文章: