linked-list

Creating a linked list without declaring node as a pointer

和自甴很熟 提交于 2020-04-16 02:50:33
问题 I've been searching around for a good while now on google and a few text books and I can't seem to understand why it is, when building a linked list, that the nodes need to be pointers. Eg. If i have a node defined as: typedef struct Node{ int value; struct Node *next; } Node; why is it that in order to create a linked list, I would say: Node *a = malloc(sizeof(Node)); Node *b = malloc(sizeof(Node)); a->value = 1; b->value = 2; a->next = b; b->next = NULL; rather than: Node a, b; a.value = 1;

descendingIterator for java.util.List

五迷三道 提交于 2020-04-10 03:37:10
问题 LinkedList can be iterated using ascending or descending iterator like this: LinkedList<Object> list = new LinkedList<Object>(); ... StringJoiner sJ1 = new StringJoiner(" "); list.iterator().forEachRemaining(a -> sJ1.add(a.toString())); System.out.println("averse: \n" + sJ1.toString()); StringJoiner sJ2 = new StringJoiner(" "); list.descendingIterator().forEachRemaining(a -> sJ2.add(a.toString())); System.out.println("reverse: \n" + sJ2.toString()); averse: Hello 2 Chocolate 10 reverse: 10

Reverse a linkedList from a array

北城以北 提交于 2020-03-23 12:04:22
问题 I am trying to add a reverse linked list back to a linked list from an array. public void reverse(){ //Calling function to take a linked list and put it inside an array int[] myArray = this.toArray(); //Populate list using array for (int i= 0; i < myArray.length; i++){ this.addHead(myArray[i]); } System.out.println(this.toString()); } Here is my method this works fine but it only set the linked list to the last index and stops. EX. [1,7,7,6] lS.reverse() => [6] Here is the to array function /

Reverse a linkedList from a array

痞子三分冷 提交于 2020-03-23 12:04:13
问题 I am trying to add a reverse linked list back to a linked list from an array. public void reverse(){ //Calling function to take a linked list and put it inside an array int[] myArray = this.toArray(); //Populate list using array for (int i= 0; i < myArray.length; i++){ this.addHead(myArray[i]); } System.out.println(this.toString()); } Here is my method this works fine but it only set the linked list to the last index and stops. EX. [1,7,7,6] lS.reverse() => [6] Here is the to array function /

Merge Sort for Singly Linked List seems to remove any numbers larger than the final number I input into the list

北城余情 提交于 2020-03-23 08:01:51
问题 I am currently trying to formulate a mergeSort mechanism for a singly linked list. Through research and finding consistent ideas about A) a merge sort being the best way to sort a singly linked list, and B) that these are the key components for performing such an operation, I have arrived at this following code. It almost works exactly as intended, but will only return all of the integers larger than the last inputted number. For example, inputting 7, 6, 5, 4, 3, 2, 1 will return 1, 2, 3, 4,

Adding element twice into Linux kernel double linked list

流过昼夜 提交于 2020-03-05 03:09:46
问题 I am trying to use linux kernel doubly linked-list implementation mentioned in https://github.com/torvalds/linux/blob/master/include/linux/list.h in user-space which its user-space implementation can be found in https://gist.github.com/roychen/1710968 following is the code which I used at first and it works fine :) #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include "list.h" struct Node { int data; char name[10]; struct list_head mylist; }; int main() {

Undesirable Zero on the Output of Single Linked-list

泪湿孤枕 提交于 2020-03-04 23:06:21
问题 I am trying to do a simple linked-list in order to print the numbers inserted as arguments on the call of the program. However, it prints an undesirable zero on the final of the output. I guess it is a NULL that is printed, but I don't know how to get rid of it. I am still understanding the basics of linked-lists. Thank you. /* */ #include <stdio.h> #include <stdlib.h> /* */ #define NUMERO_DE_ARGUMENTOS_MINIMO 3 #define EOS '\0' /* */ #define OK 0 #define ARGUMENTO_NULO 1 #define ARGUMENTO

Undesirable Zero on the Output of Single Linked-list

萝らか妹 提交于 2020-03-04 23:04:51
问题 I am trying to do a simple linked-list in order to print the numbers inserted as arguments on the call of the program. However, it prints an undesirable zero on the final of the output. I guess it is a NULL that is printed, but I don't know how to get rid of it. I am still understanding the basics of linked-lists. Thank you. /* */ #include <stdio.h> #include <stdlib.h> /* */ #define NUMERO_DE_ARGUMENTOS_MINIMO 3 #define EOS '\0' /* */ #define OK 0 #define ARGUMENTO_NULO 1 #define ARGUMENTO

QuickSort on a doubly linked list not working as it should

匆匆过客 提交于 2020-03-03 14:00:13
问题 I used an algorithm that I've used in the past for arrays. This always picks the first element as the pivot. Here's the code: void quickSort(int a[],int l,int r,int *count) { if(l<r-1) { *count=*count+r-l-1; int q=partition(a,l,r); //finding the pivot position in sorted array quickSort(a,l,q-1,count); //recursive calling before pivot sub array quickSort(a,q+1,r,count); //recursive calling after pivot sub array } } //partition function definition int partition(int a[],int l,int r) { int j,temp

invalid use of template name without an argument list

落花浮王杯 提交于 2020-02-26 05:52:28
问题 I am facing a problem with my linked list class, I have created the interface and implementation files of the class, but when I build it, this error occurs: "invalid use of template name 'LinkedList' without an argument list". here's my interface file: #ifndef LINKEDLIST_H #define LINKEDLIST_H template <typename T> struct Node{ T info; Node<T> *next; }; template <typename T> class LinkedList { Node<T> *start; Node<T> *current; public: LinkedList(); ~LinkedList(); }; #endif // LINKEDLIST_H and