99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實例講解)
python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實例講解)
2018-04-08
收藏

python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實例講解)

單向鏈表

單向鏈表也叫單鏈表,是鏈表中最簡單的一種形式,它的每個節(jié)點包含兩個域,一個信息域(元素域)和一個鏈接域。這個鏈接指向鏈表中的下一個節(jié)點,而最后一個節(jié)點的鏈接域則指向一個空值。

表元素域elem用來存放具體的數(shù)據(jù)。

鏈接域next用來存放下一個節(jié)點的位置(python中的標(biāo)識)

變量p指向鏈表的頭節(jié)點(首節(jié)點)的位置,從p出發(fā)能找到表中的任意節(jié)點。

節(jié)點實現(xiàn)

class Node(object):
 """單鏈表的結(jié)點"""
 def __init__(self,item):
  # item存放數(shù)據(jù)元素
  self.item = item
  # next是下一個節(jié)點的標(biāo)識
  self.next = None

單鏈表的操作

is_empty() 鏈表是否為空

length() 鏈表長度

travel() 遍歷整個鏈表

add(item) 鏈表頭部添加元素

append(item) 鏈表尾部添加元素

insert(pos, item) 指定位置添加元素

remove(item) 刪除節(jié)點

search(item) 查找節(jié)點是否存在

單鏈表的實現(xiàn)

class Singlepnkpst(object):
 """單鏈表"""
 def __init__(self):
  self.__head = None
 
 def is_empty(self):
  """判斷鏈表是否為空"""
  return self.__head == None
 
 def length(self):
  """鏈表長度"""
  # cur初始時指向頭節(jié)點
  cur = self.__head
  count = 0
  # 尾節(jié)點指向None,當(dāng)未到達(dá)尾部時
  while cur != None:
   count += 1
   # 將cur后移一個節(jié)點
   cur = cur.next
  return count
 
 def travel(self):
  """遍歷鏈表"""
  cur = self.__head
  while cur != None:
   print(cur.item,end = ' ')
   cur = cur.next
  print("")

頭部添加元素

def add(self, item):
  """頭部添加元素"""
  # 先創(chuàng)建一個保存item值的節(jié)點
  node = Node(item)
  # 將新節(jié)點的鏈接域next指向頭節(jié)點,即_head指向的位置
  node.next = self.__head
  # 將鏈表的頭_head指向新節(jié)點
  self.__head = nod

尾部添加元素    
def append(self, item):
  """尾部添加元素"""
  node = Node(item)
  # 先判斷鏈表是否為空,若是空鏈表,則將_head指向新節(jié)點
  if self.is_empty():
    self.__head = node
  # 若不為空,則找到尾部,將尾節(jié)點的next指向新節(jié)點
  else:
    cur = self.__head
    while cur.next != None:
      cur = cur.next
    cur.next = node

指定位置添加元素

def insert(self, pos, item):
  """指定位置添加元素"""
  # 若指定位置pos為第一個元素之前,則執(zhí)行頭部插入
  if pos <= 0:
   self.add(item)
  # 若指定位置超過鏈表尾部,則執(zhí)行尾部插入
  epf pos > (self.length()-1):
   self.append(item)
  # 找到指定位置
  else:
    node = Node(item)
    count = 0
    # pre用來指向指定位置pos的前一個位置pos-1,初始從頭節(jié)點開始移動到指定位置
    pre = self.__head
    while count < (pos-1):
      count += 1
      pre = pre.next
    # 先將新節(jié)點node的next指向插入位置的節(jié)點
    node.next = pre.next
    # 將插入位置的前一個節(jié)點的next指向新節(jié)點
    pre.next = node

刪除節(jié)點

def remove(self,item):
  """刪除節(jié)點"""
  cur = self.__head
  pre = None
  while cur != None:
  # 找到了指定元素
  if cur.item == item:
    # 如果第一個就是刪除的節(jié)點
    if not pre:
      # 將頭指針指向頭節(jié)點的后一個節(jié)點
      self.__head = cur.next
    else:
      # 將刪除位置前一個節(jié)點的next指向刪除位置的后一個節(jié)點
      pre.next = cur.next
      break
    else:
      # 繼續(xù)按鏈表后移節(jié)點
      pre = cur
      cur = cur.next

查找節(jié)點是否存在    
def search(self,item):
  """鏈表查找節(jié)點是否存在,并返回True或者False"""
  cur = self.__head
  while cur != None:
    if cur.item == item:
      return True
      cur = cur.next
    return False
以上這篇python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實例講解)就是小編分享給大家的全部內(nèi)容了



數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }