linked-list

How would you get the nth node from the tail in a singly linked list (in one traverse)?

社会主义新天地 提交于 2020-07-05 10:27:06
问题 So I got this question from an exam. How would you get the nth node from the tail in a singly linked list? Each Node has a value and a next (which is a pointer to the next value). We are given this: getNodeFromTail(Node head, int x) { } So the way I did it is to find the length of the list by traversing it once. Then going again to get the (length - x) node. So in total, 2 traversals. getNodeFromTail(Node head, int x) { int length = 0; Node headdupe = head; while (headdupe.next != NULL) {

Sort a text file with a String and Integer

ⅰ亾dé卋堺 提交于 2020-06-28 04:02:33
问题 So currently I'm working on a project to just help me practice java. I have a game that I followed a tutorial on and I've modified it a bit, now I'm trying to figure out a way to sort a text file with a String followed by an Integer. My previous attempts have all failed and I have yet to find an online resource that has actually worked. I've successfully sorted it alphabetically using the String but I want to sort it by the Integer value. Text File example abc 31 adc 100 ccd 211 ddc 99 The

Java: LinkedList class as stack and queues

佐手、 提交于 2020-06-25 21:48:38
问题 I am new to LinkedList class, and facing difficulties as to how to use it in order to implement or instantiate stack and queues object. I am not looking for piece of self-implemented codes. I wanted to know how do we use this class as stack and queues and can use the already defined methods: pop,push,enqueue and dequeue or top (in case of stacks). 回答1: Queue A LinkedList is already a queue, since it implements the Queue interface (and check the Javadoc yourself). Hence it has the following

Time Complexity in singly link list

女生的网名这么多〃 提交于 2020-06-24 14:45:06
问题 I am studying data-structure: singly link list. The website says singly linked list has a insertion and deletion time complexity of O(1) . Am I missing something? website link I do this in C++, and I only have a root pointer . If I want to insert at the end, then I have to travel all the way to the back, which means O(n) . 回答1: The explanation for this is, that the big O notation in the linked table refers to the function implementation itself, not including the list traversal to find the

Time Complexity in singly link list

霸气de小男生 提交于 2020-06-24 14:44:49
问题 I am studying data-structure: singly link list. The website says singly linked list has a insertion and deletion time complexity of O(1) . Am I missing something? website link I do this in C++, and I only have a root pointer . If I want to insert at the end, then I have to travel all the way to the back, which means O(n) . 回答1: The explanation for this is, that the big O notation in the linked table refers to the function implementation itself, not including the list traversal to find the

singly and doubly linked list in java?

早过忘川 提交于 2020-06-23 11:54:46
问题 Which collection interface is efficient to implement the singly and doubly linked list in java? code sample please? 回答1: The right interface to implement a doubly-linked list is, unsurprisingly, a LinkedList. See the JavaDoc. I'm not going to give you a code sample here; they're all over the web and you could find one with two minutes' research. If you want a singly-linked list for some reason, you're probably going to have to roll your own using a custom Node class. Each Node should just

How can I have a function duplicate each element in a list in C? [closed]

谁说我不能喝 提交于 2020-06-17 09:41:46
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 days ago . Improve this question I want to write a subroutine with name duplicate C that duplicates each element in a list of integer elements. The list is stored as a linked list where the nodes are given by: struct nodeEl { int el; struct nodeEl * next; }; typedef struct nodeEl node; List header (of type node *

JavaScript variable being ignored

删除回忆录丶 提交于 2020-06-17 09:41:29
问题 So I'm trying to write a Data Structures Visualizer with JS (so that I can host it online). It seems as though my JS ignoring my variables (and claiming some functions don't exist) and I can't figure out why. I'd appreciate the help. var stack = new Stack(); var defaultValueCounter = 0; function push() { var value = document.getElementById("add").value; if (value === "") { defaultValueCounter++; value = defaultValueCounter; } //console.log(stack + ", " + value) stack.push(value); addCol(value

How can I have a function duplicate each element in a list in C? [closed]

送分小仙女□ 提交于 2020-06-17 09:41:13
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 days ago . Improve this question I want to write a subroutine with name duplicate C that duplicates each element in a list of integer elements. The list is stored as a linked list where the nodes are given by: struct nodeEl { int el; struct nodeEl * next; }; typedef struct nodeEl node; List header (of type node *

JavaScript variable being ignored

牧云@^-^@ 提交于 2020-06-17 09:40:05
问题 So I'm trying to write a Data Structures Visualizer with JS (so that I can host it online). It seems as though my JS ignoring my variables (and claiming some functions don't exist) and I can't figure out why. I'd appreciate the help. var stack = new Stack(); var defaultValueCounter = 0; function push() { var value = document.getElementById("add").value; if (value === "") { defaultValueCounter++; value = defaultValueCounter; } //console.log(stack + ", " + value) stack.push(value); addCol(value