linked-list

which should be used: array vs linked list?

爷,独闯天下 提交于 2020-01-04 05:12:05
问题 I am planning to implement a bounded queue without using the Queue<T> class. After reading pros and cons of Arrays and LinkedList<T> , I am leaning more towards using Array to implement queue functionality. The collection will be fixed size. I just want to add and remove items from the queue. something like public class BoundedQueue<T> { private T[] queue; int queueSize; public BoundedQueue(int size) { this.queueSize = size; queue = new T[size + 1]; } } instead of public class BoundedQueue<T>

Unexpected error in double linked list in C

末鹿安然 提交于 2020-01-04 04:26:12
问题 I am trying to insert elements in my double linked list based on the number of nodes n . Like if n is 4 then number of elements entered are: 34 45 32 1 but getting segmentation fault . Can anybody tell me where I am going wrong? #include<stdio.h> #include<malloc.h> struct node{ struct node *prev; struct node *next; int info; }*start; create_list(int num) { printf("Hi I entered!\n"); struct node *q,*tmp; tmp= malloc(sizeof(struct node)); tmp->info=num; tmp->next=NULL; if(start==NULL) { printf(

given a node how can I find previous node in a singly linked list

左心房为你撑大大i 提交于 2020-01-03 15:58:18
问题 Given the current node, how can I find its previous node in a Singly Linked List. Thanks. Logic will do , code is appreciated. We all know given a root node one can do a sequential traverse , I want to know if there is a smarter way that avoids sequential access overhead. (assume there is no access to root node) Thanks. 回答1: You can't. Singly-linked lists by definition only link each node to its successor, not predecessor. There is no information about the predecessor; not even information

How to print address of a variable in Java

大憨熊 提交于 2020-01-03 15:35:43
问题 As an address of a variable (i.e. int a) in C can be obtained by &a . How can this be done in Java? For Eg. The elements of a linked list are non-contiguous. How can we print the address of the elements of a linked list created using the Collections Framework? If the above thing is not possible, how can we show that the elements of an array are contiguous whereas that of a linked list are not? 回答1: There is no element in the java language that allows you the get the address of anything and

LinkedList Get method

情到浓时终转凉″ 提交于 2020-01-03 06:37:56
问题 I have a get method for my singly linked list and they work fine but my instructor told me that he wants me to cut down on the code because I have too many special cases. Problem being is that when I try to cut out some parts of my code, the code no longer works as it's supposed to. The code written from me: public E get(int index) { Node<E> f = first; // If index is bigger / smaller than Linked List size throw IndexOutOfBounds if (index > size || index < 0){ throw new

LinkedList Get method

纵然是瞬间 提交于 2020-01-03 06:37:14
问题 I have a get method for my singly linked list and they work fine but my instructor told me that he wants me to cut down on the code because I have too many special cases. Problem being is that when I try to cut out some parts of my code, the code no longer works as it's supposed to. The code written from me: public E get(int index) { Node<E> f = first; // If index is bigger / smaller than Linked List size throw IndexOutOfBounds if (index > size || index < 0){ throw new

How do you remove the first Node in a Linked List?

北城以北 提交于 2020-01-03 04:16:24
问题 Sup guys so I'm going over a few of my methods in my Linked List class and I'm getting a logical error when removing a node from a linked list. I was working on my removeFirst() method when I then encountered a error in my removeLast() method as well. The problem is that both remove the last item in the list. not sure why but here is my code. Remove First Node public T removeFirst() throws EmptyCollectionException { // Checking to see if the List is empty or not if ( isEmpty() ) throw new

C++ Linked list destroy function

风格不统一 提交于 2020-01-03 01:41:34
问题 this is kinda a continue of my last question about linked list. I have worked a little more on it and I got stuck at some functions I need to implement. The one I have question on right now is the destroy() function. It is supposed to release memory of every list_ item . The approach is to remove every list_item recursivly from the front to the end until NULL is found. However for some reason it will only delete the key value from the struct. The node still there though. Here's the code The

Creating Linked List, not passing back to Main

a 夏天 提交于 2020-01-02 10:11:29
问题 So I am creating a linked list in a separate function, and when I print out the linked list within the function, it seems everything is fine. However; when I go to main and try to access the linked list with printf I get a segmentation fault and am confused exactly why. void createLL(struct node* head, struct node* curr, char ch, int number){ //lowest digit is the head while (((scanf(" %c",&ch)) >= 0)){ curr = (struct node*)malloc(sizeof(struct node*)); //allocate space number = ch - '0' ; /

Implementing linked list with iterator interface

大兔子大兔子 提交于 2020-01-02 09:26:34
问题 Here's the problem. Write a function for merging multiple (sorted) linked lists into one sorted linked list. This function should access the elements through Iterator interface (do not access elements through the linked list directly). The arguments to the merge procedure are an array of Iterators and the size of the array. The return value should be another Iterator with an underlying List implementation. Steps: (1) Implement linked list with iterator interface. Define the Element in the