linked-list

push operation on stack using linked list fails

自古美人都是妖i 提交于 2019-12-10 18:39:03
问题 I am trying to create a stack using single linked list, my push operation doesn't insert data into the linked list This is what I have tried so far, typedef struct element { int data; struct element *next; }node; The push method void push(node *root, int data) { if(root == NULL) { root = (node *) malloc (sizeof(struct element)); root->data = data; root->next = NULL; } else { node *temp = (node *) malloc (sizeof(struct element)); temp->data = data; temp->next = root; root = temp; } } In my

Python implementation of Mergesort for Linked list doesn't work

泪湿孤枕 提交于 2019-12-10 17:49:32
问题 I couldn't find a simple implementation of Merge Sort in Python for Linked Lists anywhere. Here's what I tried: Definition for singly-linked list: class ListNode: def __init__(self, x): self.val = x self.next = None Merge Sort Implementation: def mergeSortLinkedList(A): # Base case length of 0 or 1: if A == None or A.next == None: return A leftHalf, rightHalf = splitTheList(A) mergeSortLinkedList(leftHalf) mergeSortLinkedList(rightHalf) # The above two lines should be modified to the

Java Two-Way Linked Set data-structure [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-10 17:34:39
问题 This question already has answers here : Bidirectional multi-valued map in Java (6 answers) Closed 6 years ago . Is there an implementation of the following data-structure already implemented in Java: Say I want the set for '2' ('A', 'C', 'D') but I also want the set for 'A' ('1', '2') 回答1: You have no such data structure in the Java Collections Framework. I suggest you the guava library you may find something useful there. Note that what you have here is essentially a undirected graph so

Efficiently searching a common node in 2 singly linked lists with a memory constraint?

两盒软妹~` 提交于 2019-12-10 17:29:12
问题 Suppose you're given two signly-linked lists which ganging up at some point. design a O(n+m) algorithm with use of no more than O(1) memory which finding the FIRST common node, where m and n are the distance from the head of the list, to the gang point, respectively. I thought of marking visited nodes, but then realized that it takes more than O(1) memory. Problem is easy when you can run on the entire list, which is not allowed here because of runtime limitation. help =D 回答1: I assume there

How to write a function within a function (list_map)

这一生的挚爱 提交于 2019-12-10 17:10:01
问题 Hello I recently asked some questions on linked lists in C. The link was found here First I want to thank everyone for helping me with this. But I have one issue I cannot understand. I even asked the professor but he emailed me back with little information. Basically I am writing a linked list in C (see above link). One of the things the professor gives us in the header file is this: void list_map( INTLIST *list, void (*f)(void *) ); /*Applies a function to each element of the list */ So I

traversing task_struct->children in linux kernel

我与影子孤独终老i 提交于 2019-12-10 16:45:45
问题 I am trying to traverse a task_struct's children in the linux kernel and get information from the children. I'm having problems with all the information, so let's just keep it at the getting the pid for simplicity. This is the relavant part of my code. struct list_head * p; struct task_struct ts, *tsk; pid_t tmp_pid; INIT_LIST_HEAD(&ts.children); current = tsk; list_for_each(p, &(tsk->children)){ ts = *list_entry(p, struct task_struct, children); tmp_pid = ts.pid; printk("the pid is %d\n",

Using mergesort to sort linked lists

我们两清 提交于 2019-12-10 16:37:26
问题 I am currently working on the mergesort algorithm. I have three functions. list_sort_merge, mergelist and splitlist. list_sort_merge calls the other two to split and merge the list. I am having trouble getting this to work correctly. So what happens when I run GDB is that I get through the split function and get each number by itself such as the following example: 427 42.7 4.2.7 Then the mergesort comes along and segfaults me. What happens is the right_list and left_list are not being passed

Editing a node in a Linked list

*爱你&永不变心* 提交于 2019-12-10 16:17:33
问题 I am creating a student list (linked list) that can add, view and edit student information. I have two fields namely Student Name and Student Grade and I add new students in the list in a way that it is sorted according to the student's grades in descending order. I have finished doing the add and view portion. The problem is on the edit part because I need to edit the information, then I need to sort it again so that it would be on the proper location of the list. For example, I have 3

Fastest way to prove linked list is circular ? in python [duplicate]

筅森魡賤 提交于 2019-12-10 15:56:26
问题 This question already has answers here : Best algorithm to test if a linked list has a cycle (13 answers) Closed 5 years ago . Could someone please let me know the best way to prove a linked list contains a loop? I am using an algorithm with two pointer, one is moving slow with one steps and one is moving faster with two steps. class Node(object): def __init__(self, value, next=None): self.next=next self.value=value def create_list(): last = Node(8) head = Node(7, last) head = Node(6, head)

Adding and Subtracting Bigints Using Linked Lists

天大地大妈咪最大 提交于 2019-12-10 14:55:42
问题 I'm almost done with this assignment, and it's killing me. This is my THIRD post about three different sections of this, and I'm honestly embarrassed that I'm struggling this much with the assignment. The assignment itself is to make a program that performs addition and subtraction of big integers using linked lists (and I'm slowly starting to hate linked lists, outside of Lisp). Everything seems to be working now, save for the actual addition and subtraction. I'm not sure if it is the