Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Using Lua implement various operations list     - Glibc support encryption by modifying the DNS (Programming)

- Zabbix monitoring disk IO status (Server)

- IIS virtual host of safety knowledge (Linux)

- MySQL stored procedures execute dynamic sql statement (Database)

- How Mutt mail client to use cipher text password (Linux)

- To build a private Docker registry (Server)

- ORA-12547: TNS: lost contact error Solution (Database)

- ImageMagick Tutorial: How to cut images in Linux command line (Linux)

- How to install Nginx on FreeBSD 10.2 as an Apache reverse proxy (Server)

- Linux system find command Detailed (Linux)

- Upgrading KDE Plasma 5.3 in Ubuntu 15.04 (Linux)

- Linux rights management (Linux)

- Shell array: Define Shell array, the array length (Programming)

- cp: omitting directory error solutions under Linux (Linux)

- CentOS 6.4 under PXE + Kickstart unattended operating system installation (Programming)

- Cacti installation deployment under CentOS 6.6 (Server)

- Multipath configuration under Linux (Linux)

- Dell R710 server disk recovery database one case (record) (Server)

- Linux system with a firewall to prevent the DOS attack (Linux)

- Java Virtual Machine Basics (Programming)

 
         
  Using Lua implement various operations list
     
  Add Date : 2018-11-21      
         
         
         
  Using Lua implement various operations list

#! / Usr / local / bin / lua

--Lua Achieve list
node = {}
list = node

- Initialization construct an empty table
function init ()
 list.data = 0
 list.next = nil
end

- Adding data to the end of the list
function addRear (d)
 node.next = {} - the establishment of a node, a node is equivalent to malloc
 node = node.next
 node.next = nil
 node.data = d
 list.data = list.data + 1
end

- Adding data to the head of the list
function addHead (d)
 newNode = {} - the establishment of a node, a node is equivalent to malloc
 newNode.data = d
 newNode.next = list.next
 list.next = newNode
 list.data = list.data + 1
end

- I-th position of the insertion data d i> = 1
function insert (i, d)
 if i <1 then
  print ( 'legal position is not inserted')
  return
 end
 
 local j, k, l = i-1,0, list - find the i-th position
 while k ~ = j do
  k = k + 1
  l = l.next
  if not l.next then break end
 end
 --if k ~ = j then print ( "insert position is not lawful") return end
 
 - Start insert
 newNode = {}
 newNode.next = l.next
 newNode.data = d
 l.next = newNode
 list.data = list.data + 1
end
 
- Every element of the list to print
function display ()
 local l = list.next
 while l do
  io.write (l.data .. "")
  l = l.next
 end
 print ( ' n-- display ok -')
end

- To determine whether the list is empty
function is_empty ()
 return list.data == 0
end

- Remove the i-th position data i> = 1 returns the contents of deleted data
function delete (i)
 if i <1 then
  print ( 'legal position is not deleted')
  return
 end
 
 local j, k, l = i-1,0, list
 while k ~ = j do
  k = k + 1
  l = l.next
  if not l.next then break end
 end
 
 - Start deleting
 d = l.next.data
 t = l.next.next
 l.next = nil
 l.next = t
 list.data = list.data-1
 return d
end

- Clean the list, after the operation is completed, the list also, but is empty, the initialization state fairly the beginning
function clear ()
 if not list then
  print ( 'list does not exist')
 end
 
 while true do
  firstNode = list.next
  if not firstNode then - indicates the list is already empty table
   break
  end
  t = firstNode.next
  list.next = nil
  list.next = t
 end
 list.data = 0
 print ( '- clear ok -')
end

- Destruction list
function destory ()
 clear () - to clear the list
 list = nil
end

- Get the i-th element of i> 1 value
function getData (i)
 if not list then
  print ( 'list does not exist')
  return
 end
 if i <1 then
  print ( 'not legal position')
  return
 end
 
 local l = list.next - points to the first element
 local k = 1
 while l do
  if k == i then
   return l.data
  end
  l = l.next
  k = k + 1
 end
 
 print ( 'not legal position')
end
  
- Get the length of the list
function getLen ()
 if not list then
  print ( 'list does not exist')
  return
 end
 return list.data
end

- Main Method
function main ()
 init ()
 addRear (5)
 addRear (7)
 addRear (10)
 addHead (1)
 addHead (2)
 insert (2,4)
 display ()
 
 
 print ( 'Enter the location you want to delete the elements:')
 pos = io.read ( '* number')
 ret = delete (pos)
 if not ret then
  print ( 'deletion failed')
 else
  print ( 'the element you want to delete is:' .. ret)
 end
 print ( 'the contents of the list is deleted:')
 display ()
 
 print ( 'Enter the location you want to get elements:')
 pos = io.read ( '* number')
 print ( 'first' ..pos .. 'element content is:' .. getData (pos))
 print ( 'the length of the list is:' .. getLen ())
 
 destory () - Destruction list
 print ( '- main end -')
end
 
- Entrance Program
main ()
     
         
         
         
  More:      
 
- Linux ln command - create a file / directory link (Linux)
- MySQL Parameter Tuning Best Practices (Database)
- CentOS boot image to achieve semi-automatic installation (Linux)
- OpenGL Superb Learning Notes - Vertex Shader example (Programming)
- How Glances monitoring system on Ubuntu (Linux)
- Linux in order to make NMAP hide and seek with the firewall (Linux)
- redis configuration in detail (English) (Database)
- Linux Basics Tutorial: Combining awk delete data before the specified date hdfs (Linux)
- Linux boot the system does not display a progress bar setting method (Linux)
- Linux disk partition, format, mount the directory (Linux)
- Oracle archive log summary (Database)
- Hibernate4 The Hello World (basic environmental structures) (Programming)
- Intrusion analysis and prevention tools Knark under Linux platform (Linux)
- Linux ldconfig command (Linux)
- Binary tree traversal algorithm summary (recursive and non-recursive) (Programming)
- Dockerfile use to build a mirror-based CentOS 7 (Linux)
- HomeKit Human Interface Guidelines (Linux)
- Vmstat command Linux Performance Monitoring (Linux)
- Linux, modify the hostname (Linux)
- C ++ 11 feature: decltype keywords (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.