linked-list

Reverse doubly-link list in C++

南楼画角 提交于 2019-12-07 13:27:29
问题 I've been trying to figure out how to reverse the order of a doubly-linked list, but for some reason, in my function void reverse() runs while loop once and then crashes for some reason. To answer some questions ahead, I'm self-teaching myself with my brothers help. This isn't all of the code, but I have a display() function which prints all nodes chronologically from start_ptr and a switch which activates certain functions like case 1 : add_end(); break; case 2 : add_begin(); break; case 3 :

Sorting a linked list in ascending order in C

耗尽温柔 提交于 2019-12-07 12:03:49
问题 I am working on a program for my C programming course that is supposed to give us experience using linked lists. One of the last parts of the assignment asks for us to take a linked list and sort it in ascending order using prepend or append functions that we wrote earlier in our program. struct lnode { int datum; struct lnode *next; }; struct lnode* prepend(struct lnode *list, int x) { struct lnode *node = (struct lnode *)malloc(sizeof(struct lnode)); node -> datum = x; node -> next = list;

Efficiently generating all possible permutations of a linked list?

白昼怎懂夜的黑 提交于 2019-12-07 11:39:02
问题 There are many algorithms for generating all possible permutations of a given set of values. Typically, those values are represented as an array, which has O(1) random access. Suppose, however, that the elements to permute are represented as a doubly-linked list. In this case, you cannot randomly access elements in the list in O(1) time, so many permutation algorithms will experience an unnecessary slowdown. Is there an algorithm for generating all possible permutations of a linked list with

linked list implementation java

≡放荡痞女 提交于 2019-12-07 11:36:30
问题 I have implemented a Linked List into Java. I have created everything, but I am having difficulty removing a specific node with specific data. It is throwing a NullPointerException . I believe, I am getting a NullPointerException because the next node is null. If someone could please point me in the right direction that would be great. Input anything one two three exception: Exception in thread "main" java.lang.NullPointerException at LinkedList.remove(LinkedList.java:28) at Main.main(Main

Purpose of Curly Brace Usage of C Code found in Linux (include/linux/list.h)?

廉价感情. 提交于 2019-12-07 09:20:05
问题 I came across the following code within Linux (include/linux/list.h). I'm confused about line 713. In particular, I don't understand ({ n = pos->member.next; 1; }). What is the curly braces doing? Why is there a '1' in this statement? If someone could explain this particular line it would be much appreciated. Note, I don't need an explanation of how link lists and #defines work, etc. 704 /** 705 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry

Deleting a node in Linked List in python

让人想犯罪 __ 提交于 2019-12-07 08:31:51
问题 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

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

旧城冷巷雨未停 提交于 2019-12-07 06:59:02
问题 (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

Insertion sort on linked list in C?

安稳与你 提交于 2019-12-07 06:25:51
问题 I've tried searching for a problem similar to mine, but haven't found much help. I have a linked list of structs of this type: struct PCB { struct PCB *next; int reg1, reg2; }; I first create 10 PCB structs linked together in this way: for(i=20;i<=30;i++) { curr = (struct PCB *)malloc(sizeof(struct PCB)); curr->reg1 = i; curr->next = head; head = curr; } I then need to create 20 more PCB structs, but their reg1 values need to be generated using rand() . I'm currently doing that as so: for (j

Insertion in the middle of ArrayList vs LinkedList [duplicate]

☆樱花仙子☆ 提交于 2019-12-07 03:58:51
问题 This question already has answers here : When to use LinkedList over ArrayList in Java? (32 answers) Closed 6 years ago . Talking in Java's context. If I want to insert in the middle of either an ArrayList or a linkedList , I've been told that Arraylist will perform terribly. I understand that it is because, we need to shift all the elements and then do the insertion. This should be of the order n/2 i.e. O(n). But is not it the same for linkedList . For linked List, we need to traverse till

Linked List time complexity for its operations [duplicate]

社会主义新天地 提交于 2019-12-07 03:22:37
问题 This question already has answers here : What is the time complexity of LinkedList.getLast() in Java? (5 answers) Closed 6 years ago . I am implementing a Linked list in terms of stock market program. It has and operation - Buy For Buy the code is //Stocks is a linked List like so //LinkedList<Integer> stocks = new LinkedList<Integer>(); public void buy(int q, int p) { stocks.addLast(q); //add number of stocks stocks.addLast(p); //for i stocks i +1 = price of stock } This operation addLast is