linked-list

How to count the number of nodes in a linked list without traversing it?

*爱你&永不变心* 提交于 2019-12-12 07:59:24
问题 I have been asked in an interview how to count the number of nodes in a linked list without traversing the list? Is there any way to achieve this? 回答1: The only way I can think of is to add a counter of the number of nodes which is incremented each time the add or insert methods are invoked, and decremented when delete is invoked. You cannot make assumptions about memory occupied because, being a linked list, you cannot guarantee that all nodes will be in the same memory block (indeed, this

How to implement a Linked List in Java? [duplicate]

青春壹個敷衍的年華 提交于 2019-12-12 07:24:17
问题 This question already has answers here : How do I create a Linked List Data Structure in Java? [closed] (6 answers) Closed 3 years ago . I am trying to implement a simple HashTable in Java that uses a Linked List for collision resolution, which is pretty easy to do in C, but I don't know how to do it in Java, as you can't use pointers... First, I know that those structures are already implemented in Java, I'm not planning on using it, just training here... So I created an element, which is a

JTable - Selected Row click event

。_饼干妹妹 提交于 2019-12-12 07:12:13
问题 I have a Jtable that is populated with a linkedlist through an AbstractTableModel. What I want to do is when I click (left-mouse click) on a row in the JTable, the linkedlist is search (in this case it contains movie titles) and displays the values in the linked list in Jtextboxes How do I do this? Here is the code GUI_g: http://pastebin.com/J3qtjn8J ProgramTableModel: http://pastebin.com/Dwkc9Cz3 Processing: http://pastebin.com/qHnkvCbr Main: http://pastebin.com/K4yGYX9H My guess it retrieve

C programming Linked Lists delete node at position N

耗尽温柔 提交于 2019-12-12 07:06:57
问题 EDIT: Figured out the problem. Also if you found this through google or another search engine here is where I went wrong and how to fix it. My deleteNode() method was moving through the list properly with the correct temp and keeping the head untouched. Where I was going wrong was in what I was returning as the result of the method. I was returning either temp or newNode which is incorrect because it goes through the list until it finds defined position. Once it finds that defined position it

Initialize a linked list using ints in C

随声附和 提交于 2019-12-12 07:05:12
问题 I need to initialize a linked list using ints given from the main.c. #include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char ** argv) { int b = 128; int M = b * 11; // so we have space for 11 items char buf [1024]; memset (buf, 1, 1024); // set each byte to 1 char * msg = "a sample message"; Init (M,b); // initialize I know what I have isn't correct, but it's the best I could come up with. #include <stdio.h> #include "linked_list.h" struct node { int value; struct

Remove entry and Insert Entry in Linked Lists functions operating on the wrong entries

百般思念 提交于 2019-12-12 06:48:17
问题 Good afternoon. I'm learning the C language from a book called Programming in C Third Edition by Stephen G. Kochan. I wrote some code that is supposed to insert and remove certain entries from a list, which it does, the problem is, it does not remove the right entry, and it does not quite insert the entry in the right spot. The code is below. Any help would be greatly appreciated! //Insert and Remove entry functions using doubly linked lists #include <stdio.h> struct Entry { int Value; struct

How to remove duplicate Integer elements in array data structure in java

你离开我真会死。 提交于 2019-12-12 06:45:48
问题 Is there any simple way to remove duplicate elements in Java(will two loops work and how). Thank you:) IN: int[] arr = {1,3,4,2,3,1,6,7,7}; Output i want is: {1,3,4,2,6,7} the only i know is we can traverse it through loop. eg. for(int i = 0;i < arr.length;i++){ for(int j = 0;j<arr.length;j++){ if( ){ //what logic i can apply here. } } } 回答1: This should work.. final Integer[] noDuplicates = new LinkedHashSet<>(Arrays.asList(arr)).toArray(new Integer[0]); 回答2: Java 8 provides a nice way to do

Reverse Linked list Java memory [closed]

醉酒当歌 提交于 2019-12-12 06:37:19
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I was doing this leetcode problem and I don't understand why this solution doesn't work. It seems to only be returning the head element. Thanks /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public

C Programming Error , printing linked list, at run time executed code crashes

╄→尐↘猪︶ㄣ 提交于 2019-12-12 06:30:57
问题 I'm working on linked lists and pointers. Here is a simple code including a push function. After pushing my elements, and trying to print the first member, the executed code crashes at run time. However, when passing the same pointer to the print_list function and applying the printf function inside print_list it works fine. But when using it directly in the main function and applying the printf function it crashes. #include<stdio.h> #include<stdlib.h> typedef struct list{ int order; struct

Input from a file (Find the nth element for the Linked List)

此生再无相见时 提交于 2019-12-12 06:21:05
问题 The problem is to find nth element in a linked list, i have the problem figured out on finding the element. But i have to read an input from a file and output the nth element from the list For example Input would look like this a b c d 4 e f g h 2 Output would like a g It is stated "The first argument will be a path to a filename containing a series of space delimited characters followed by an integer representing a index into the list (1 based), one per line" I am not sure how i would go