linked-list

Writing a merge method for lists

南笙酒味 提交于 2019-12-10 10:43:36
问题 I'm trying to write a method that merges a doubly linked list in an alternating fashion. So if we have two int lists (0,1,2,3) and (4,5,6), we'd just have one final list of (0,4,1,5,2,6,3). Each list has a head, a tail, a next, and a prev pointer. Its hurting my mind to try to figure out where to start or how this would work. I've tried to trace it on paper, but no progress. Can anyone guide me in the right direction? Whats a good way to 'picture' this or plan for it, as I dont even know

C# - LinkedList - How to remove all nodes after specified node?

假装没事ソ 提交于 2019-12-10 09:40:31
问题 I am implementing an undo/redo buffer with generic LinkedList. In this state: [Top] state4 (undone) state3 (undone) state2 <-- current state state1 [bottom] When I do a Push, I would like to remove all states after the current one, and push the new one. My current bypass is to do while (currentState != list.last), list.removeLast(); but it sucks LinkedList just support Remove, RemoveFirst & removeLast... I would like something like RemoveAllNodesAfter(LinkedListNode ...) ? How can I code that

What is a data structure that has O(1) for append, prepend, and retrieve element at any location?

China☆狼群 提交于 2019-12-10 02:38:20
问题 I'm looking for Java solution but any general answer is also OK. Vector/ArrayList is O(1) for append and retrieve, but O(n) for prepend. LinkedList (in Java implemented as doubly-linked-list) is O(1) for append and prepend, but O(n) for retrieval. Deque (ArrayDeque) is O(1) for everything above but cannot retrieve element at arbitrary index. In my mind a data structure that satisfy the requirement above has 2 growable list inside (one for prepend and one for append) and also stores an offset

Queue using linked list implementation in Java

佐手、 提交于 2019-12-10 00:21:41
问题 I quickly wrote a linked list class in Java. I want to write another queue class which uses the linked list. How would I achieve this in Java? I don't fully understand the implements / extends keywords... this is what my queue looks like ( for example): public class Queue<T> implements LinkedList { protected LinkedList<T> list; public Queue() { list = new LinkedList<T>(); } public void add( T element) { list.add( element); } public T removeLast() { return list.removeLast(); } } Also note that

Linked List, insert at the end C++

喜欢而已 提交于 2019-12-09 21:10:03
问题 I was writing a simple function to insert at the end of a linked list on C++, but finally it only shows the first data. I can't figure what's wrong. This is the function: void InsertAtEnd (node* &firstNode, string name){ node* temp=firstNode; while(temp!=NULL) temp=temp->next; temp = new node; temp->data=name; temp->next=NULL; if(firstNode==NULL) firstNode=temp; } 回答1: What you wrote is: if firstNode is null, it's replaced with the single node temp which has no next node (and nobody's next is

Quick Sort on a Linked List with a random pivot in C

依然范特西╮ 提交于 2019-12-09 19:32:52
问题 I have spent a lot of time trying to work on this problem for a class and am at ends. I have found lots of resources regarding arrays and other ways of selecting a pivot but I am just at ends and am really going crazy here, any help would be so much appreciated you can not possibly imagine. #include <stdlib.h> /*and, malloc*/ #include <stdio.h> /*printf*/ struct listnode { struct listnode *next; long value; }; /*Finds length of list, which is usefull in selecting a random pivot*/ int

Measure size/length of singly linked list in Java?

亡梦爱人 提交于 2019-12-09 19:21:40
问题 I need help making the int size(); method for a singly linked list in Java. This is what I have so far, but it does not return the correct size of the list. public int size() { int size = 0; Node CurrNode = head; while(CurrNode.next != null) { CurrNode = CurrNode.next; size++; } return size; } Can someone help me implement this method in Java? 回答1: The biggest improvement you can make is to use Java Coding Convension and use camelCase local variables. You can write it like this. public int

Add to front of linked list

让人想犯罪 __ 提交于 2019-12-09 18:18:47
问题 I am confused as to how to add to the front of the linked list. /** * data is added to the front of the list * @modifies this * @ffects 2-->4-->6 becomes data-->2-->4-->6 */ public void insert(E data) { if (front == null) front = new Node(data, null); else { Node temp = new Node(data, front); front = temp; } } This creates a cycle. How do I avoid that? I have a LinkedList class which holds the front Node, in a variable called front. I have a Node class within this LinkedList class. Any help

Difference between arraylist and linkedList [duplicate]

元气小坏坏 提交于 2019-12-09 14:57:53
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: When to use LinkedList<> over ArrayList<>? When to use a linked list over an array/array list? When should I use arrayList and when should I go for LinkedList? When should I use TreeSet , LinkedHashSet and HashSet ? 回答1: When should i use arrayList and when should I go for LinkedList? Arraylist maintain indices like arrays. So if want more frequent get operations than put then arraylist is best to go. LinkedList

Using ListIterator to move back and forth over a LinkedList in Java

笑着哭i 提交于 2019-12-09 14:14:21
问题 I have a LinkedList over which I need to iterate back and forth multiple times. I am using it to keep track of a series of pages in a workflow that will be created dynamically. This does not behave as I would expect. Given this example: LinkedList<String> navigationCases; navigationCases.add("page1"); navigationCases.add("page2"); navigationCases.add("page3"); navigationCases.add("page4"); ListIterator navigationItr = navigationCases.listIterator(); navigationItr.next(); // Returns page1