linked-list

Warnings when creating a singly linked list with arrays

瘦欲@ 提交于 2019-12-05 22:25:54
#include <stdio.h> typedef struct { int data; struct node *next; }node; void print(node *head) { node *tmp = head; while (tmp) { printf ("%d ", tmp->data); tmp = tmp->next; } } int main() { node arr[5] = { {1, &arr[1]}, {2, &arr[2]}, {3, &arr[3]}, {4, &arr[4]}, {5, NULL} }; print(arr); return 0; } Why do i get these warnings while compiling with gcc -Wall ? (even without -Wall, gcc produces the same warnings) list.c: In function ‘print’: list.c:15:7: warning: assignment from incompatible pointer type [enabled by default] list.c: In function ‘main’: list.c:22:18: warning: initialization from

Different Types of Linked Lists!

牧云@^-^@ 提交于 2019-12-05 20:50:00
问题 What are the different types of Linked Lists which are commonly used? I know and have used the following: Singly Linked List Doubly Linked List Circular List What are the other kinds of lists that have been used by you or known to you? 回答1: Unrolled Linked List In computer programming, an unrolled linked list is a variation on the linked list which stores multiple elements in each node. It can drastically increase cache performance, while decreasing the memory overhead associated with storing

Why LinkedList and arraylist extends AbstractList in java?

拜拜、爱过 提交于 2019-12-05 17:51:33
问题 Why LinkedList and ArrayList extends AbstractList in Java ? Abstract classes are used when we want to specify a common behaviour in implementation classes. But all the methods which are in AbstractList are overridden by ArrayList and LinkedList . So what is the use of extending this class? 回答1: subList(int,int) method is not overriden by both ArrayList and LinkedList , and for this AbstractList provides a common implementation From Java source public List<E> subList(int fromIndex, int toIndex

How to Merge sort a Linked List with O(nlogn) time and O(1) space complexity

余生长醉 提交于 2019-12-05 17:05:34
(disclaimer: for school) As far as I know, recursively splitting a linked list, then sending it off to another function to merge is O(nlogn) time and O(n) space. Is it possible to do mergesort on linked list with O(nlogn) time and O(1) space complexity? How would you go about doing this? ANY HELP APPRECIATED PS: to make sure that the traditional mergesort is space complexity 0(n), this is an example of 0(n), right? How would it be changed for O(1) space? void sortTrack() { Node merge = this.head; this.head = Node (merge); } public Node mergeSort(Node head){ if ((head == null)||(head.next ==

Declaring a LinkedList in Java

你。 提交于 2019-12-05 16:56:33
问题 I always learn when we declare a collection we should do, Interface ob = new Class() , if i want to use for example a LinkedList i'll do List ob = new LinkedList() , but then i can't have access to all methods from LinkedList.. Isn't LinkedList ob = new LinkedList() 100% correct? 回答1: Isn't LinkedList ob = new LinkedList() 100% correct? Well I'd suggest using the generic form, but sure - if you want to use functionality which is specific to LinkedList , you need to declare the variable

Implementation of LinkedList in python __getitem__() method

雨燕双飞 提交于 2019-12-05 16:51:47
I am implementing a LinkedList in python(3.7.4) and the code of the module is below :- LinkedList.py class Node: def __init__(self,value): self.value = value self.ref = None class LinkedList(Node): def __init__(self): self.__head = None self.__cur = None self.__count = 0 def add(self,value): if self.__head is None: self.__cur = Node(value) self.__head = self.__cur else: self.__cur.ref = Node(value) self.__cur = self.__cur.ref self.__count += 1 def getList(self): temp = self.__head while temp!=None: yield temp.value temp = temp.ref def delete(self,value): temp = self.__head while temp!=None: if

Why are Scala's `Lists` implemented as linked lists

大兔子大兔子 提交于 2019-12-05 13:38:49
问题 I always thought that the benefit of linked lists was that you could add or remove items (especially not from the end) without having to copy lots of elements thanks to the beauty of pointers. However, Scala's List is immutable (at least by default). What is the benefit of having an immutable linked list (because there are definite downsides, e.g. not O(1) element access.) Thanks! 回答1: I think the main reason is that one of the most powerful uses of linked lists is head/tail splitting. There

Deleting a node in Linked List in python

旧城冷巷雨未停 提交于 2019-12-05 12:40:26
To delete a node in a linked list, what is wrong with this implementation?: def delete(self, val): tmp = self.head prev = None while tmp: if val == tmp.data: self.size -= 1 if prev==None: self.head = self.head.next else: prev.next = tmp.next else: prev = tmp tmp = tmp.next All the guides I've looked at say it should be: def delete(self, data): tmp = self.head prev = None found = False while tmp and not found: if data == tmp.data: found = True else: prev = tmp tmp = tmp.next if found: self.size -= 1 if prev == None: self.head = self.head.next else: prev.next = tmp.next but I can't figure out

Reversing a singly linked list when a block size is given

旧街凉风 提交于 2019-12-05 12:36:41
There is a singly connected linked list and a block size is given.For eg if my linked list is 1->2->3->4->5->6->7->8-NULL and my block size is 4 then reverse the first 4 elements and then the second 4 elements.The output of the problem should be 4->3->2->1->8->7->6->5-NULL I was thinking of dividing the linked list into segments of size 4 and then reversing it. But that way I am forced to use a lot of extra nodes which is not desired at all. The space complexity should be kept to a minimum. It will be highly appreciable if someone can come with a better solution where the usage of extra nodes

C++ Linked list behavior

為{幸葍}努か 提交于 2019-12-05 10:32:18
I have some C code, where in there are two linked lists(say A and B) and A is inserted at a particular position into B and A still has elements. How do I simulate the same behavior effectively using the C++ STL? If I try splice, it makes the second one empty. Thanks, Gokul. You need to copy the elements. Consider something like this: std::copy(a.begin(), a.end(), std::inserter(b, b_iterator)); If you want the same nodes shared by two lists, this is simply not supported by std::list (STL containers always have exclusive ownership). You can avoid duplicating the elements by storing pointers in