LinkedList = {}
LinkedList.__index = LinkedList

function LinkedList:new()
    return setmetatable({ head = nil, tail = nil }, self)
end

Node = {}
Node.__index = Node

function Node:new(data)
    return setmetatable({ data = data, next = nil, prev = nil }, self)
end

-- Insert at the end
function LinkedList:append(data)
    local newNode = Node:new(data)
    if not self.head then
        self.head = newNode
        self.tail = newNode
    else
        newNode.prev = self.tail
        self.tail.next = newNode
        self.tail = newNode
    end
end

-- Insert at the beginning
function LinkedList:prepend(data)
    local newNode = Node:new(data)
    if not self.head then
        self.head = newNode
        self.tail = newNode
    else
        newNode.next = self.head
        self.head.prev = newNode
        self.head = newNode
    end
end

-- Delete a node with the given value
function LinkedList:delete(data)
    local current = self.head
    while current do
        if current.data == data then
            if current.prev then
                current.prev.next = current.next
            else
                self.head = current.next
            end
            if current.next then
                current.next.prev = current.prev
            else
                self.tail = current.prev
            end
            return
        end
        current = current.next
    end
end

-- Print the linked list from head to tail
function LinkedList:display()
    local current = self.head
    while current do
        print(current.data)
        current = current.next
    end
end

-- Test
local list = LinkedList:new()
list:append(1)
list:append(2)
list:append(3)
list:prepend(0)
print("Original List:")
list:display()  -- Output: 0, 1, 2, 3
list:delete(2)
print("After Deleting 2:")
list:display()  -- Output: 0, 1, 3

C相关代码片段

reddit photoshop will be deactivated popup macos

uppercase to lowercase in c

show available value

lowercase to uppercase in C

c switch return

how to change hte middle node in linked list C

openGL create shape

script to check program running and restart

printf("")

Rewind to a commit Git

mouse click for colab

how to remove newline in the end of fgets string

fit text to conatiner flutter

gorm string array

js observe url change

clock skew detected makefile

time() function in c

worst fit code in c

first fit code in c

roem evaluation pyspark

calculator c

safet algorith im c

c copy char*

jfug function

soil moisture sensor interfacing with ESP32

flutter sidebar menu example

access images in c panel htaccess file

c include header file in the folder aboce

c include libray

gcc name output file

C data link escape char

How to type DLE in C

vscode Ctrl+d undo

C[strtol] func

pointer c++

power in c

sort three numbers

strip in c

c time duration

overleaf itemize a b c

route groups

itoa c code

queue in c

'wsgirequest' object has no attribute 'get'

c copy file

how to print array data in html using javascript

hello world program in c

how to compare a variable with a string in c

c print array

c rand range

exponents without pow c

how to reversed number in c

c random number with range

pthread mutex destroy while lock

pthread init mutex twice

insert c or p

woocommerce align add to cart buttons

vscode fold all code when open file

commit github non funziona

makefile both c and c++

scanf with space

c macro variable arguments

c printf format

check if prime c

read file line by line c

c use define in string

pattern program in c

Write the code in C to implement selection sort

c program of Goldbach conjecture

otput only one decimal point in c

trim string c

tokenize string c

firebase message in app doesn't work

implementing stack using linked list

esercizi semplici con le funzioni in c

array to linked list in c

sorted array to bst implementation in c

doubly linked list insertion in the middle

arch distro

distance vector routing algorithm