linked-list

Why is a LinkedList Generally Slower than a List?

巧了我就是萌 提交于 2019-12-20 09:26:32
问题 I started using some LinkedList’s instead of Lists in some of my C# algorithms hoping to speed them up. However, I noticed that they just felt slower. Like any good developer, I figured that I should do due diligence and verify my feelings. So I decided to benchmark some simple loops. I thought that populating the collections with some random integers should be sufficient. I ran this code in Debug mode to avoid any compiler optimizations. Here is the code that I used: var rand = new Random

C++ Generic Linked List

て烟熏妆下的殇ゞ 提交于 2019-12-20 07:33:21
问题 For the code below: #include <iostream> #include <string> using namespace std; class Foo2; class Foo3; template <class T> class Foo1 { public: Foo1(); void print() { cout << "My name is: " << name << endl; } T getNext(){ return nextLink; } string name; T nextLink; }; class Foo2 : public Foo1 { public: Foo2(){ name = "Foo2"; } }; class Foo3 : public Foo1 { public: Foo3(){ name = "Foo3"; } }; template <class T> class LinkedList { public: T curr; T first; void add(T node){ if(first == NULL){

JAVA - Items not being entered into the list properly?

一世执手 提交于 2019-12-20 06:19:41
问题 This is sort of a continuation from this question. I managed to figure out the whole JTextField not letting me enter anything problem. Now I have a separate problem that the item isn't being entered properly in the list. I still think you guys should look at the GitHub repository since I'm only posting a small portion of the code here, but here's at least the relevant portions. Here's the GUI program (or at least the relevant portions): import java.awt.event.*; import javax.swing.*; public

Linked list sorting in C

前提是你 提交于 2019-12-20 05:31:34
问题 I'm writing a simple file for one of my classes that is a simple linked list activity and I need to sort a linked list. This is my source code so far: /* * Simple list manipulation exercise. * 1. Create a list of integers. * 2. Print the list. * 3. Sort the list. * 4. Print the list * 5. Free the list nodes. */ #include <stdlib.h> #include <stdio.h> struct node { int value ; struct node *next ; } ; extern struct node *mk_node(int v) ; extern void print_list(struct node *head) ; extern struct

Issues with reversing objects in a LinkedList

泪湿孤枕 提交于 2019-12-20 05:23:11
问题 I'm writing code for an assignment that requires a method which reverses elements in a LinkedList, given the portion of the list to reverse. For example, if the user enters 3 the method would reverse the first 3 elements in the array. I've written code for it but instead of reversing the code it simply replaces the 2nd element with the element present in the first index. My only issue seems to be the reverseFirstSome method. I'm not asking you to write the code for me, but any pointer in the

Counting all the nodes in a Linked List

微笑、不失礼 提交于 2019-12-20 04:20:03
问题 I'm trying to write a simple method to count all the nodes in the linked list. I know there are 7 items in the linked list, but it is returning just 6 of them. Here is my method public int count() { int count = 0; for (ListNode n = head; n.next != null; n = n.next) { count++; } return count; } And here is my ListNode.java public class ListNode { String name; // a name in the list ListNode next; // the next node in the list ListNode prev; // the previous node in the list /** * Constructor with

How would I make my custom generic type linked list in Java sorted?

纵然是瞬间 提交于 2019-12-20 04:11:06
问题 I am writing my own linked list in java that is of generic type instead of using the java collections linked list. The add method for the linked list is made up of the following code: public void add(T item, int position) { Node<T> addThis = new Node<T>(item); Node<T> prev = head; int i; if(position <= 0) { System.out.println("Error: Cannot add element before position 1."); } else if(position == 1) { addThis.setNext(head); head = addThis; } else { for(i = 1; i < position-1; i++) { prev = prev

Deleting node from linked list by index

老子叫甜甜 提交于 2019-12-20 03:54:59
问题 This is my code for deleting a node from a linked list. vec_store holds seq and size . Variable seq holds the vectors and a pointer. For some reason, the else if(i<s->size-1) doesn't work which is the last condition. Can anyone solve the problem? By the way this is C code. void delete_vec(vec_store s, int i) { if (i<0 || s->size-1<i) { printf("Cannot delete vector because index %d is out of bounds\n",i); } else if (i==0) { node temp; temp = s->seq; s->seq = s->seq->next; s->size--; free(temp)

std::forward_list — erasing with a stored iterator

蹲街弑〆低调 提交于 2019-12-20 02:45:30
问题 I'm trying to keep a global list of a particular (base) class's instances so that I can track them down by iterating through this global list at any time. I believe the most proper way to address this is with an intrusive list. I have heard that one can encounter these creatures by digging into the Linux kernel, for example. In the situation where I'm in, I don't really need such guarantees of performance, and using intrusive lists will complicate matters somewhat for me. Here's what I've got

Insert new node at the beginning of Linked-List

天涯浪子 提交于 2019-12-19 10:12:30
问题 In a simple Linked List implementation on C, I couldn’t figure out a line of function named insert(). It takes a char and add to the linked list in alphabetical order. The line is about creating a new node when the list is empty. And since there will be only one node on the list, the line should be like I’ve commented, am I wrong? /****************************************************/ void insert( ListNodePtr *sPtr, char value ){ ListNodePtr newPtr; ListNodePtr previousPtr; ListNodePtr