linked-list

Swap items in doubly-linked list by their indices in the backing array

三世轮回 提交于 2019-12-17 21:12:37
问题 I have an array of objects of the following type: struct Node { Node *_pPrev, *_pNext; double *_pData; }; Some of the nodes participate in a doubly-linked list, with _pData!=nullptr for such nodes. There is also a dummy head node with _pNext pointing to the beginning of the list, and _pPrev pointing to the end. The list starts with containing only this head node, and it should be never removed from the list. The doubly-linked list is backed by an array, with initial size equal to the maximum

Coding a function to copy a linked-list in C++

£可爱£侵袭症+ 提交于 2019-12-17 20:43:52
问题 I need to implement an auxilliary function, named copyList, having one parameter, a pointer to a ListNode. This function needs to return a pointer to the first node of a copy of original linked list. So, in other words, I need to code a function in C++ that takes a header node of a linked list and copies that entire linked list, returning a pointer to the new header node. I need help implementing this function and this is what I have right now. Listnode *SortedList::copyList(Listnode *L) {

Next struct item,incomplete type [duplicate]

℡╲_俬逩灬. 提交于 2019-12-17 20:37:04
问题 This question already has answers here : self referential struct definition? (9 answers) Closed 2 years ago . struct node{ struct node next; int id; } gives "field next has incomplete type error ". what is wrong with this struct ? 回答1: When creating a self-referential data type, you need to use pointers to get around problems of circularity: struct node; struct node { struct node * next; int id; } ...should work, but take care to allocate memory correctly when using it. Why a pointer?

bubble sort implementation on linked lists

ぐ巨炮叔叔 提交于 2019-12-17 20:33:38
问题 I have to implement a BubbleSort algorithm on a Linked List instead of an array. I'm new to java so I don't really know how to put it in code. But I gave it a try and here's what I got: SinglyNode.java public class SinglyNode { public Object names; public SinglyNode next; public SinglyNode (Object name1) { names = name1; } public SinglyNode (Object name2, SinglyNode next1) { names = name2; next = next1; } Object getObject() { return names; } SinglyNode getNext() { return next; } void

How would you pick a uniform random element in linked list with unknown length?

ⅰ亾dé卋堺 提交于 2019-12-17 20:03:20
问题 How would you pick a uniform random element in linked list with unknown length in one pass or if not two passes? 回答1: Use reservoir sampling http://en.wikipedia.org/wiki/Reservoir_sampling . You need only one pass of the data. For picking one element: Pick first element (probability 1) Later, for kth element pick it with probability 1/k (i.e. replace the existing selection with kth element) I will let you prove that this results in uniform selection of elements. 来源: https://stackoverflow.com

Printing out a linked list using toString

Deadly 提交于 2019-12-17 19:50:54
问题 Ok guys, so I am trying to learn how to print out a linked list. I have all the methods that I would need to use for the list, but I can't figure out how to display the values of the nodes. Right now there is nothing in my main method because I kept getting errors trying to call non static methods in the main. I have a toString method that displays the contents of the list. How would I go about calling this toString to display the value of each node? Any advice will be greatly appreciated.

Sorting a linked list in C

夙愿已清 提交于 2019-12-17 19:00:17
问题 I'm trying to sort a linked list by finding the largest value, deleting it from its position, and then inserting it at the top of the list. The difficulty I'm running into is the actual deleting and inserting at the top. The issue seems to be in the if condition in the while loop contained within the sortList function, but I'm not sure how to fix it. Any help would be appreciated. #include <stdio.h> #include <stdlib.h> typedef struct node{ int num; struct node *next; } Node, *NodePtr; void

Reversing a linked list in python

狂风中的少年 提交于 2019-12-17 18:12:10
问题 I am asked to reverse a which takes head as parameter where as head is a linked list e.g.: 1 -> 2 -> 3 which was returned from a function already defined I tried to implement the function reverse_linked_list in this way: def reverse_linked_list(head): temp = head head = None temp1 = temp.next temp2 = temp1.next temp1.next = None temp2.next = temp1 temp1.next = temp return temp2 pass class Node(object): def __init__(self,value=None): self.value = value self.next = None def to_linked_list(plist

Create Balanced Binary Search Tree from Sorted linked list

≡放荡痞女 提交于 2019-12-17 17:31:41
问题 What's the best way to create a balanced binary search tree from a sorted singly linked list? 回答1: How about creating nodes bottom-up? This solution's time complexity is O(N). Detailed explanation in my blog post: http://www.leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html Two traversal of the linked list is all we need. First traversal to get the length of the list (which is then passed in as the parameter n into the function), then create nodes by the list's order.

Java Linked List search and delete method

浪子不回头ぞ 提交于 2019-12-17 16:54:15
问题 I have a project for computer science class and have everything done except for one method. The delete method. Basically I am making a linked list from user input and I need to be able to delete all nodes (which is done) and delete a single specified node. So I need to search through the list of nodes find the one to delete and delete it. Anything that can help is appreciated. If you have a solution please offer an explanation as I am trying to learn and just solve the problem. I'm not going