Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Using Lua implement various operations list     - Android gets the global process information and the memory used by the process (Programming)

- How to disable IPv6 on Ubuntu, Linux Mint, Debian (Linux)

- Oracle Client Dedicated and Shared connection mode (Database)

- Java garbage collection and heap memory layout (Programming)

- Intel Graphics Installer installation on Ubuntu 15.10 (Linux)

- Thunderbird 24.4.2 has been released for use with PPA updates (Linux)

- Linux, Oracle listen address modification (Database)

- printf PHP string operations () built-in function usage (Programming)

- Experts teach you safety precautions Linux Sniffer (Linux)

- Use IP address spoofing Intrusion Prevention Firewall (Linux)

- Revive Adserver ad server installation on Ubuntu 15.04 / CentOS7 (Server)

- To install PostgreSQL 9.4 (Database)

- MySQL and Oracle time zone settings compare (Database)

- CentOS set up FTP server (Server)

- Monitor traffic Linux Shell Edition (Programming)

- Firewall settings oracle remote connection in Linux systems (Linux)

- HDFS Distributed File System Resource Manager Developer summary (Server)

- IronPython and C # to interact (Programming)

- Yii PHP Framework Getting Started tutorial (Linux)

- Hackers is how the invasion and control of Things devices? (Linux)

 
         
  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:      
 
- MySQL optimization resulting order by using filesort (Database)
- Understand the profound discussion of some of the options for wireless encryption (Linux)
- RedHat6.4 installation tutorial --- Minimal Edition (Linux)
- C # Future: Method Contract (Programming)
- Setting grep Highlight Matches (Linux)
- Linux desktop system using the remote server in clear text ssh password (Server)
- Linux initialization init system - UpStart (Linux)
- How to make GRub instead of the default Ubuntu software center (Linux)
- SpringMVC garbage processing (Programming)
- Using LLVM Clang and Blocks under Linux (Programming)
- XtraBackup achieve non-stop use of master-slave synchronization service (Database)
- Ceph single / multi-node installation summary Powered by CentOS 6.x (Server)
- PostgreSQL Select source code analysis (Database)
- Open remote MySQL database connection managed under CentOS (Database)
- You need to know 12 Git High Command (Linux)
- Tree Traversals Again (Programming)
- Node.js developers must know four JavaScript concepts (Programming)
- HBase table data processing tab (Database)
- Linux iptables firewall settings (Linux)
- shellinabox: one uses AJAX Web-based terminal emulator (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.