gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
使用typescript写一个双向链表
- User
使用typescript写一个双向链表
- Assistant
可以使用如下的typescript代码实现一个双向链表:
typescript - User
添加push_front函数
- Assistant
可以在
DoublyLinkedList类中添加一个pushFront()函数来实现在链表开头插入新元素的功能。typescriptclass ListNode<T> { public prev: ListNode<T> | null; public next: ListNode<T> | null; constructor(public value: T) { this.prev = null; this.next = null; } } class DoublyLinkedList<T> { private head: ListNode<T> | null; private tail: ListNode<T> | null; private length: number; constructor() { this.head = null; this.tail = null; this.length = 0; } // 省略 get、push、pop、shift 方法的实现 public pushFront(value: T): number { const node = new ListNode(value); if (this.length === 0) { this.head = node; this.tail = node; } else { node.next = this.head; this.head!.prev = node; this.head = node; } this.length++; return this.length; } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.