linked-list

Deleting node in linked list - segmentation fault

ⅰ亾dé卋堺 提交于 2021-01-28 11:42:40
问题 Problem requires to delete node from linked list given head pointer of list and the position of node in list to be deleted. More details of question can be found at: https://practice.geeksforgeeks.org/problems/delete-a-node-in-single-linked-list/1 Code returns segmentation fault but not exactly sure where I went wrong. My code is as follows: Node* deleteNode(Node *head,int x) { //Your code here struct Node* temp = head; if(x==0){ //change head head = temp->next; free(temp); } //find previous

Copy an array to linked List in each data field

冷暖自知 提交于 2021-01-28 09:16:36
问题 First, sorry if my question has already been answered. I found some threads that are (in a way) similiar but I was not able to solve my problem. Second, I am new to single-linked-list in C so I would be happy if you could answer my question as easy as possible. I made a simple linke-list, that has characters in it: #include <stdio.h> #include <stdlib.h> // declaration of node struct _Node_ { char data_string; struct _Node_ *next; }; int main() { //a simple linked list with 3 Nodes, Create

Reversing a LinkedList in with multiple assignment

风流意气都作罢 提交于 2021-01-28 08:02:16
问题 I have this code right down here: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def reverseList(self, head: ListNode) -> ListNode: if head == None: return pre, node = None, head while node: pre, node.next, node = node, pre, node.next return pre I am trying to vizualize how this works. If it starts on a list, the pre becomes the head , since node was assigned to head . then node.next is assigned to pre , so it

Delete node from Linked List

人走茶凉 提交于 2021-01-28 05:49:43
问题 I am working on iterative delete function that deletes node from a linked list, I think that the code is supposed to work fine, it traverses through the list, finds the needed node, points the head to the next node and deletes the current, but when I test run it I get infinite loop, could you please help me to pinpoint the error, here is the function: typedef struct E_Type * List; struct E_Type { int data; struct E_Type* next; }; the function: bool erase(List & l, int data){ List head = l; if

Linked List Delete Node

非 Y 不嫁゛ 提交于 2021-01-28 04:52:09
问题 So I'm trying to implement a function that deletes nodes from a linked list. Here's my main: int main(void) { NODE* first = generateNodes(5); NODE* jank = getNode(first, 2); deleteNode(first,2); printf("Length of Node List: %d\n",getNodeListLength(first)); printf("First Node: %d\n",first -> pos); printf("Jank Node: %d\n",jank -> pos); return 0; } This is my output: Length of Node List: 2 First Node: 0 Jank Node: 2 The output should be (because in main() I removed jank , and decreased the size

C Split String and Use It For Struct

十年热恋 提交于 2021-01-28 02:33:29
问题 I need to create a phonebook in C, with structs and linked-list. Data will be on a file. I'll store data like Name1#Surname1#Date1#Number1 Name2#Surname2#Date1#Number2 Name3#Surname3#Date1#Number3 But I couldn't split by delimiter "#" and use datas in my struct. Here is my code: #include <stdio.h> #include <locale.h> //Veri Yapısı: typedef struct kisi { char isim[50]; char soyisim[50]; int dYili; char telefon[16]; struct kisi *sonraki; }Kisi; int main() { //Türkçe çıktılarda sorun yaşamamak

How much memory is used when allocating an array vs allocating an linked list in Java?

若如初见. 提交于 2021-01-28 00:02:58
问题 My guess is its a 32-bit/64-bit word (depending on the CPU) per value stored in the array. So it would be array size X 32-bit/64-bit. In the case of linked lists, it would be twice this to store the reference which points to the next element. So it would be 2 * array size X 32-bit/64-bit. Is this correct, am I missing anything? 回答1: Much more. Each element in a linked list has: Pointer to next element, pointer to previous element, pointer to item value (12 bytes) + object overhead (around

Why can't you create a linked lists without creating nodes as pointers?

倖福魔咒の 提交于 2021-01-27 19:14:19
问题 There is an answer in Creating a linked list without declaring node as a pointer. But I wanted to know if there were any other reasons due to which you can't create nodes as pointers, just so, to be clear. One of the reasons is that the scope of the new nodes will die outside the function-Is there no way you can solve this problem?, and Is there any other reason? 回答1: I've been using a lot of linked lists (and even more complex structures) in which no node was allocated separately on the heap

moving a rdf ordered list from a graph to another with sparql

只愿长相守 提交于 2021-01-27 19:01:38
问题 I have a list in a rdf knowledge graph in a Fuseki dataset. I can get the elements of the list with something like select ?webpage where { graph <http://datamusee.givingsense.eu/graph/webtracks> { ?track <http://erlangen-crm.org/current/P16_used_specific_object> ?content. ?content rdf:rest*/rdf:first ?webpage . } } but I would like to copy the list from the webtracks graph to another graph I doen't find how to do it either with an insert or by exporting the data with a construct and then

Borrowed RefCell does not last long enough when iterating over a list

折月煮酒 提交于 2021-01-27 12:30:13
问题 I'm trying to implement a linked list to understand smart pointers in Rust. I defined a Node : use std::{cell::RefCell, rc::Rc}; struct Node { val: i32, next: Option<Rc<RefCell<Node>>>, } and iterate like fn iterate(node: Option<&Rc<RefCell<Node>>>) -> Vec<i32> { let mut p = node; let mut result = vec![]; loop { if p.is_none() { break; } result.push(p.as_ref().unwrap().borrow().val); p = p.as_ref().unwrap().borrow().next.as_ref(); } result } the compiler reports an error: error[E0716]: