linked-list

Why exactly do we need a “Circular Linked List” (singly or doubly) data structure?

六眼飞鱼酱① 提交于 2019-12-04 07:42:10
问题 Why exactly do we need a "Circular Linked List" (singly or doubly) data structure? What problem does it solve that is evident with simple Linked Lists (singly or doubly)? 回答1: A simple example is keeping track of whose turn it is in a multi-player board game. Put all the players in a circular linked list. After a player takes his turn, advance to the next player in the list. This will cause the program to cycle indefinitely among the players. To traverse a circular linked list, store a

Add to front of linked list

本秂侑毒 提交于 2019-12-04 07:25:21
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 would be appreciated. Thank you. Jeff Don't you have access to "Next" node ? In that case public void

Difference between LinkedList<?> and LinkedList<Object> [duplicate]

女生的网名这么多〃 提交于 2019-12-04 07:11:12
问题 This question already has answers here : What is the difference between ? and Object in Java generics? (6 answers) What does the question mark in Java generics' type parameter mean? (6 answers) Closed 2 years ago . Is there any difference between LinkedList< ? > and LinkedList< Object > in Java? 回答1: This passes compilation: LinkedList<?> list1 = new LinkedList<String> (); This doesn't: LinkedList<Object> list2 = new LinkedList<String> (); i.e. a LinkedList<?> variable can be assigned any

Why do I get a ConcurrentModificationException?

喜夏-厌秋 提交于 2019-12-04 07:11:06
问题 Why do I get a ConcurrentModificationException at the specified location in my code? I cannot figure out what I am doing wrong... The removeMin() method is being used to locate the min in the list pq , remove it, and return its value import java.util.Iterator; import java.util.LinkedList; public class test1 { static LinkedList<Integer> list = new LinkedList<Integer>(); public static void main(String[] args) { list.add(10); list.add(4); list.add(12); list.add(3); list.add(7); System.out

Synchronizing access to a doubly-linked list

匆匆过客 提交于 2019-12-04 06:18:25
I'm trying to implement a (special kind of) doubly-linked list in C, in a pthreads environment but using only C-wrapped synchronization instructions like atomic CAS, etc. rather than pthread primitives. (The elements of the list are fixed-size chunks of memory and almost surely cannot fit pthread_mutex_t etc. inside them.) I don't actually need full arbitrary doubly-linked list methods, only: insertion at the end of the list deletion from the beginning of the list deletion at arbitrary points in the list based on a pointer to the member to be removed, which was obtained from a source other

How does indirection work in this code?

 ̄綄美尐妖づ 提交于 2019-12-04 06:14:47
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . I was reading the answer to Merging two sorted linked list. The code: #define SWAP_PTRS(a, b) do { void *t = (a); (a) = (b); (b) = t; } while (0) Node* MergeLists(Node* list1, Node* list2) { Node *list = NULL, **pnext = &list; if (list2 == NULL) return list1; while (list1 != NULL) { if (list1->data > list2->data) SWAP_PTRS(list1, list2); // What does the

Linked lists, arrays, and hardware memory caches

元气小坏坏 提交于 2019-12-04 05:17:13
While questions have been asked before about linked lists versus arrays, the answers mostly boil down to what most of us probably have already learned at some point: Lists are good at inserting and deleting Arrays are good at random access Now respectable people like Bjarne Stroustrup have argued that arrays practically always outperform linked lists because they make much better use of the caching architecture implemented in modern hardware. He also states that the performance advantage of arrays increases with their size. While I basically understand his arguments and agree with him, I

C++ Segmentation when using erase on std::list

青春壹個敷衍的年華 提交于 2019-12-04 04:07:43
I'm trying to remove items from a C++ linked list using erase and a list iterator: #include <iostream> #include <string> #include <list> class Item { public: Item() {} ~Item() {} }; typedef std::list<Item> list_item_t; int main(int argc, const char *argv[]) { // create a list and add items list_item_t newlist; for ( int i = 0 ; i < 10 ; ++i ) { Item temp; newlist.push_back(temp); std::cout << "added item #" << i << std::endl; } // delete some items int count = 0; list_item_t::iterator it; for ( it = newlist.begin(); count < 5 ; ++it ) { std::cout << "round #" << count << std::endl; newlist

Different Types of Linked Lists!

本小妞迷上赌 提交于 2019-12-04 03:53:59
What are the different types of Linked Lists which are commonly used? I know and have used the following: Singly Linked List Doubly Linked List Circular List What are the other kinds of lists that have been used by you or known to you? Unrolled Linked List In computer programming, an unrolled linked list is a variation on the linked list which stores multiple elements in each node. It can drastically increase cache performance, while decreasing the memory overhead associated with storing list metadata such as references. It is related to the B-tree. - Wikipedia XOR Linked List XOR linked lists

Generic Linked List for Delphi 2009

时光总嘲笑我的痴心妄想 提交于 2019-12-04 03:43:49
问题 I was looking in Generics.Collections and noticed there was no linked list. Sure they are simple to make, but I thought it was odd there was not one (or I just missed it). Are linked lists just outdated when compared to new modern data structures, or is there a need for a general generic linked list? Does anyone know of one? 回答1: Do you know the DeHL? I think the TLinkedList<T> from the DeHL.Collections.LinkedList.pas unit is exactly what you are looking for. 回答2: In the old days, almost any