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