linked-list

Linked list and copy constructor

I'm trying to write a basic, singly-linked list class in C++. I did it in my data structures class years back, but I can't remember the details. Should my Node class have a copy constructor? It has a Node* as a member variable, and as far as I know you're always supposed to write a copy constructor, destructor, and assignment operator f...

traversing a singly linked list in C++

Hey everyone, I was wondering if it is possible to traverse a linked list like this: currentNode = randomNode;//where randomNode may or may not = firstNode prevNode = firstNode; while(prevNode != currentNode } Is it possible to do this in C++ when I am trying to find the node before currentNode in a singly linked list? I trying to...

What's the fastest algorithm for sorting a linked list?

I'm curious if n log n is the best a linked list can do. Thanks ...

Swapping Nodes on a single linked list

Trying to make a swapNode function that can take any two nodes and swap them. I've made an algorithm that works if they're at least 2 nodes away, but I can't seem to come up with an algorithm that will work if they are closer to each other. Here's what I wrote so far: void swapNode(call * &head, call * &first, call * &second){ call...

detecting the start of a loop in a singly linked link list?

Is there any way of finding out the start of a loop in a link list using not more than two pointers? I do not want to visit every node and mark it seen and reporting the first node already been seen.Is there any other way to do this? ...

Java How to find a value in a linked list iteratively and recursively

Hi I have a method that has a reference to a linked list and a int value. So, this method would count and return how often the value happens in the linked list. So, I decided to make a class, public class ListNode{ public ListNode (int v, ListNode n) {value = v; next = n;) public int value; public ListNode next; } Then, the metho...

Why is this string changed?

I have the following code, so far, I want to check if a file name is already in the linked list fileList (or flist). according to the output, the string saved in the first Node was changed somewhere in Node* getFileName(Node *&flist) How did this happen? Also, is there anything else that I'm doing is wrong or not safe regarding pointers ...

Java: Trouble with TreeSet and LinkedList

I have an unsorted linked list. To sort it, I thought I'd put the values into a TreeSet with a comparator supplied, then return those values as a new linked list. Yet, it fails. Comparator: public class SortSpeciesByCommonName implements Comparator<Species> { /** * a negative integer, zero, or a positive integer as the first ...

Why can't the Nodes be linked together?

EDIT: Is it possible to NOT use new? (do not dynamically allocating memory) I think it is push that is wrong, but I don't know where, how, and why. here is the code: struct Node { string fileName; Node *link; }; int size(Node *&flist) { int count = 0; Node *tempPtr = flist; while (tempPtr != 0) { count += 1; ...

Is it possible to create a linked list on the stack in C++?

Hi i just started learning C++ couple of weeks ago. So now I have this school assignment problem that asks me to implement a linked-list without using "new" or anything to do with dynamically allocating memory (and cannot use any ADT from STL). The prof says that everything can be done on the stack, but how? I have been working on this s...

Operator Overload << in Linked List

How can I overload the operator <<. The purpose of the overloaded operator is to do: cout << ptr->info and not receive the memory address but Display the maker year and model of that node's info section. Example: template <class DataType> struct Node { DataType info; Node<DataType> *next; }; In each info section of the Node there wil...

Paint component doesn't print polygon according to intended coordinate

I have problem drawing the triangle polygons based on the coordinates which are stored in linked list. When I checked the linked list elements using System.out.println in paint component method Public void paintComponent (Graphics g) { ... for (Polygon p : triangles) { g2.drawPolygon(triangle); ... // print the elements...

Test Suite for a Linked List Implementation? (and/or other data structures)

For fun, I'm going to write my own implementations of common data structures like Linked List or Binary Sorted Tree. Are there any good test suites that already exist that I can use to make sure they are fully living up to their contracts? (It probably wouldn't be that hard to test them myself, but if it's already been done I don't want...

Clearing a double-linked list.

Hi. I have a double linked list (queue) I have made on my own. I am wondering, to clear the linked list, is it enough to simply remove the head and tail references? E.g public void Clear() { Head = null; Tail = null; } I am imaging a domino effect, but I am having a hard time testing it. It WILL make the whole object appea...

Counting memory blocks

This is a homework question from compiler design course. I just need an explanation of certain parts of the question. It is claimed that returning blocks to the standard memory manager would require much administration. Why is it not enough to have a single counter per block, which holds the number of busy records for that bl...

Most optimal way to find the sum of 2 numbers represented as linked lists

Hello, I was trying to write a program for the problem I mentioned above, the numbers (i.e the lists) can be of unequal length, I was not able to figure out a way to do this other than the most commonly thought of approach i.e reverse list-1 reverse list-2 find the sum and store it in a new list represented by list-3 reverse the li...

What is test best sorting algorithm for a Doubly LinkedList

Hi, Can someone recommend me a sorting algorithm for my custom link list + example, basically it is similar to the Generic LinkedList so if you could give examples for the Generic LinkedList it will be great. I am planning to implement the merge sort, please advice on the pros/cons as I am not so familiar with any algorithm, just taken...

Which Java collection's Linked list adds in O(1) when given an element?

hi, i need to insert into a very large LinkedList, whose elements i hold in a fast-access HashMap. it's important to keep the list in order (which is not the natural order of the keys). i thought it would be possible to hash the linked list nodes, then insert directly on the node (getting the node from the map + insert in a linked list...

Linked list interview question

This may be the old, but I couldn't think of an answer. Say, there are two lists of different lengths merge at one point; how do we know where the merging point is? We don't know the length and we should parse each list only once. ...

How can I sort a linked list in C ?

Hi, Need to be able to either sort or insert entries to a linkedlist in alphabetical order. Is there a good way to do that? ...