Questions tagged [linked-list]

-2

votes
1

answer
20

Views

How to code add method in double linked list using interface?

I have to code the add method in List using interface. i tried but its not even working. can anyone help me. interface and list class are given below: /** * Inserts the specified element at the specified position in this list. * Shifts the element currently at that position (if any) and any subseq...
jhalock bhandari
1

votes
1

answer
39

Views

C-Linked-List: How do I save the 'Head' in a 'Temp' var so I don't have to traverse backwards each time

If I traverse my linked list for any reason, print, sort, etc... I have to re traverse it backwards to reset the head. Is there a better way to do this? struct list_node { struct list_node *next; struct list_node *prev; char word[30]; int word_count; }; typedef struct list_node list_node; struct li...
seamus
1

votes
2

answer
37

Views

Linked list with memory leak issues

This program is acting as a template for what will be a linked list of bunny objects with unique names and colors. int Nodes::insert() { Node* new_node = (Node*) malloc(sizeof(Node)); new_node->name = getName(); new_node->prev = NULL; new_node->next = head; if (head != NULL) { head->prev = new_node...
MChess404
-1

votes
0

answer
31

Views

Why doesnt my code work twice? it executes fine the first time, but not the second

I need to make a function that moves the nth element in a singly linked list to the end of the list. I created some code that does that but it only works once if I try to do it again it moves the selected element to the end but the one that was moved previously gets deleted/dissapears. My theory is...
Matt
0

votes
2

answer
22

Views

Getting segmentation fault when trying to print linked list data reversely using recursion

I have created linked list of nodes. And wanted to print the data of each node reversely using recursion technique. I got the working as well as segfaulted code. I am trying to understand whats actually problem in segfaulted code. Actually I tried debugging using GDB but I dont know exactly how to t...
renga_in_stack
1

votes
2

answer
7.1k

Views

Deleting a specific node from a linked list - python

As an exercise I am trying to create my own linked list. I have created a node class and a linked list class. I have been able to create a constructor and a node adder method. I am having a lot of problems with my delete method though. Here is my code first: class Node: def __init__(self,value,next=...
InsigMath
1

votes
3

answer
36

Views

how can I remove objects by attribute within a Linked List?

How could I remove an object inside a Linkedlist. I have a class account with studentId and studentName. I enter the objects inside the list, but when I try to remove I do not know how to do it. Because every time you remove an element from the middle of the list it gets organized, meaning the index...
DViga
1

votes
2

answer
52

Views

Confused by double pointer traversal of linked list

So I was watching this Ted Talk w/ Linus Torvalds wherein he outlines this example: https://www.youtube.com/watch?v=o8NPllzkFhE&t=14m26s I was intrigued by it, so I wanted to code it up for myself: void remove_node(node* entry) { node** indirect = &head; while (*indirect != entry) { indirect = &(*in...
Jackson Lenhart
1

votes
3

answer
43

Views

Is it okay to connect all links of a node in a function create_new_node?

I am learning linked list and I am just wondering if it is good/ bad practice to connect all links of a new node with the function *create_new. From what i read online, they only the next link, but i want to make it connect the previous link as well. Here is my code: node *create_new (int new_data,...
Thomas D
1

votes
2

answer
64

Views

Iterating LinkedList using streams is altering result list due to RaceCondtion

We are iterating the LinkedList using Streams in java8, and creating another list. But due the race condition the result List size got alatered. List InfoList = new LinkedList(); documentList.stream() .parallel() .forEach(document -> { Info descriptiveInfo = objectFactory.createDescriptiveInfo(); Li...
Raghavan
1

votes
1

answer
28

Views

Compile error with C++ Linked List creation. Undefined symbols for architecture x86_64:

I am attempting to create a linked list RecipeBook. I need to create a method add, which takes a Recipe object parameter and adds it to the end of the linked list. When compile on my mac I receive the error: Undefined symbols for architecture x86_64: 'Recipe::Recipe()', referenced from: RecipeBook::...
Dylan Brinkley
0

votes
0

answer
11

Views

Failing to remove the first node in a LinkedList

I am practicing using Java's LinkedList. The purpose of the main method here is to remove the given index's node. The given node can be removed rightly, except the first node. If I changed removeLastKthNode(head, lastKth); into head = removeLastKthNode(head, lastKth), it works. But I don't know wh...
tao4free
1

votes
1

answer
112

Views

C++ Swapping adjacent nodes in a linked list

I'm having some trouble swapping adjacent nodes in a singly linked list. Here's my swap function: void swap(std::shared_ptr root, int indexA, int indexB) { if (indexA == 0) { std::shared_ptr A = root; std::shared_ptr B = A->next; A->next = B->next; B->next = A; root = B; } else if (indexB == 0) { st...
Alexander Granell
1

votes
0

answer
124

Views

Large number division with linked lists in C

I am trying to represent a string that holds any number to infinity. This number must be an integer and return its value as a result. Right now, my goal is to do this representation but for division only. So lets say we have 125 as our numerator, and 5 as our denominator. The answer should be 25 rig...
Zeid Tisnes
1

votes
2

answer
53

Views

How to display in one line that have many behavior in LinkedList

Our prof gave us an activity about creating a linked list with another class that has attributes or behavior of last name, first name, age, course, etc. So my question is if I set a cumulative setter with the following given attributes. if I add it to the linked list will the linked list itself will...
genkisan
1

votes
0

answer
16

Views

addGrowConstant ArrayList?

So I keep getting OutOfBoundsExceptions from my code because the initial capacity (2) is too low. The Growth Constant is 10. What is there to do to fix this problem public void addGrowConstant(int i, E e) throws IndexOutOfBoundsException { //TODO Complete ArrayList addGrowConstant method while(s...
mcnuggets24
1

votes
1

answer
282

Views

Why doesn't my linked list remove multiple duplicates?

I have the following code: class LinkedList: def __init__(self): self.head = None I added the remove_duplicate() function to this LinkedList class which removes any duplicates except for the first instance in the list. def remove_duplicate(self, value): prev = None curr = self.head count = 0 while c...
G.Lee159
1

votes
3

answer
302

Views

How to keep track of all nodes in a linkedlist using only one variable?

The program will read values that are typed in by the user iteratively in a while loop. Every time the value is read, a new node will be created with this integer value. Now I'll make a LinkedList using these created nodes. And then print out every value stored in the LinkedList. I expect the order...
OptatootatpO
1

votes
2

answer
1.1k

Views

How to merge and sort two doubly linked lists in C

I am currently working on a project that takes in two text files with data and sorts them into two separate linked lists. My next step is to create a function that takes in those two lists to merge and sort them by ID in increasing order. I have started the implementation but I am stuck and need som...
MGLL414
1

votes
1

answer
71

Views

C# custom linked list node deletion - Garbage collection

Following is code to create a custom C# list and delete a node in the list: static void Main(string[] args) { ListNode rootNode = CreateList(); DeleteNode(rootNode.ChildNode); .... } private static ListNode CreateList() { ListNode node1 = new ListNode() { Value = 1, ParentNode = null; } ListNode no...
Romonov
1

votes
2

answer
303

Views

Adding nodes to a list in ascending order with Java

Is there a way to use the compareTo function when comparing objects, I'm not sure if it's just for Strings. I am trying add an node into its correct position in ascending order. heres where I declare my attributes/constructor private Node head; //refers to the head of the node private int size; // k...
Steven Matos
1

votes
1

answer
59

Views

Best algorithm for sequential access of nodes

I want to know the best algorithm where I can create a 'sorted' list based on the key (ranging from 0 to 2 power 32) and traverse them in sorted order when needed in an embedded device. I am aware of possible options namely sorted linklist As number of nodes in the linked list increases searching f...
codingfreak
1

votes
1

answer
82

Views

Swap Nodes in Pairs

I am currently brushing up and reviewing my datastructures as I prepare for job interviews. I am currently working on this problem where I have to swap every 2 nodes in a Linked List. My code is as follows: public ListNode swapPairs(ListNode head) { ListNode dummy = new ListNode(0); dummy.next =...
Haki Dehari
1

votes
1

answer
802

Views

Understanding a Linked List implementation in Python

I have found an implementation of a Linked List in Python, online, but it doesn't have any explanation or comments. I understand the underlying concepts of a Linked List, but there is one key part of the code I don't understand: class Node: def __init__(self, data): self.data = data self.next = None...
user9352220
1

votes
2

answer
52

Views

Linked List - How a single object is storing so many addresses one after other?

I learn about data structures and Linked Lists and I don't understand something. How can a single object store so many addresses and datas. When I learned about object I saw that you need to create new objects if you want to store many datas for a single variable, but now at Linked List, the newNode...
elvis
1

votes
1

answer
51

Views

How to implement function without argument that return size of Doubly Linked list? int size() const

I need to implement following function: int size() const; function returns number of pieces of data stored in the list Time complexity - O(1). Basically I have a class called DList contained in DList.h which has the following structure: class DList { struct Node { T data_; Node *next_; Node *prev_;...
1

votes
0

answer
36

Views

Implementing linked list in php - is it safe construction?

Here is an implementation of linked list. It's OK, it's working. But there are few lines that bother me (comments inside method are mine): //insertion at the start of linklist public function insertFirst($data) { $link = new ListNode($data); // create local variable $link->next = $this->firstNod...
DavidL
1

votes
0

answer
57

Views

Alternate Solution to Python Linked List method 'Get Element From Position' Is not Working

I'm having an issue with the code that I'm testing on a Udacity compiler for a course. I'm trying to test a solution that is similar to the answer provided by the course but it gives me an error. The challenge is to provide a method that returns an element in a certain position within a linked list....
JMags1632
1

votes
1

answer
133

Views

inserting a node in the nth position of a linked list

This is a hackerrank problem 'to insert a node in the nth position of a linked list'. From the problem's description, we are supposed to insert 10 at position 1. 3 > 5 > 4 > 2 > Null is an existing linked list and we have to insert 10 at index one. The output should look like this 3 >10 > 5 > 4 > 2...
1

votes
2

answer
48

Views

Doubly linked list no longer linked in both directions

I'm having some issues with a section of code I'm writing and was hoping someone may be able to help me see where I've gone wrong and explain why so I can better understand it. I'm being told that my doubly linked list is no longer linked in both directions but I'm failing to see where I've gone wro...
Jess-Tayler
1

votes
1

answer
745

Views

Cannot access memory at address at address “” (gdb)

#include #include #include struct Node { /* Data fields with appropraiate types */ char fname[64]; char lname[64]; char puid[16]; int Age; struct Node *next; }; struct List { struct Node *start; int numNodes; }; struct List * initialize_list() { struct List *list = (struct List *) malloc(sizeof(s...
Kenan Dominic
1

votes
3

answer
454

Views

Add Two Numbers Linked List

I have a question about add two numbers using a linked list from LeetCode. Below is the question itself,the problem I am facing, and my code. You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contains a sin...
oumoum
1

votes
1

answer
106

Views

Struggling with implementing Set with linked list

My task is to implement a set with a linked list. I created the linked list pretty quickly and then started to build set. I struggled a little with the set operations (union/intersection/difference) but After implementation i tried to make a driver method and now i am getting a seg fault with contai...
Ayleids
1

votes
1

answer
46

Views

Traverse through a linked list to find string

I need to loop through a vector of linked list and check if a node with the string(s) exists. Obviously this only checks the beginning to see if it exists. I want to know how to go through the entire linked list. vector table; //Dlist is the linked list int DL_Hash::Present(string &s) { int index...
user9573040
1

votes
0

answer
28

Views

Code to locate a specific item in a linked list and remove it

For my Computer Science course we are needing to create 2 functions. One function needs to locate the serial position of the item being searched for (an int in this case) and the index of the previous item. The locateNode function (the function described above) needs to be used within the Remove fun...
DemonSlayer730
1

votes
0

answer
59

Views

ArrayIndexOutOfBoundx on LinkedList.toArray()

I'm working with a android game and I get an ArrayIndexOutOfBoundsException when I try to run this code: public RemoteVariable[] getRemoteVariable(){ RemoteVariable[] variables = remoteQueue.toArray(new RemoteVariable[0]); remoteQueue.clear(); return variables; } From what I have read on the LinkedL...
Bartors
1

votes
0

answer
56

Views

Filtering a specific value from a multiple entry listbased on particular value

I have an excel file that contains a large set of value namely: ID, fullname, Date/Time and state and based on values of Date/Time and state there are multiple entries for a user with same ID and fullname. But I only need those entries where the Date/Time is minimum and maximum stored into a list su...
NitKrish
1

votes
1

answer
88

Views

Adding structs to a linked list doesn't seem to work

So I'm doing an exercise in C, learning to manipulate data structures. I am struggling a little with structs/pointers in functions. I've tried to step through, and I can see that I'm creating new structures, but I'm either creating them in the same memory location OR I'm creating a Linked List with...
Link
1

votes
1

answer
84

Views

swap nodes in a linked list overflows java

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode swapPairs(ListNode head) { if (head == null|| head.next == null) return head; ListNode curr = head;//current node in list...
Omar Elhosseni
1

votes
0

answer
59

Views

How to avoid self referential linked list in C

I'm writing this program for my OS class and I'm stuck on what seems like a simple issue. I have a Lock struct and a lockList struct that holds a pointer to a Lock as its head. When I need to add a lock to the list, I need to add it to the end of the linked list and be able to print them. Problem...
cpgreen2

View additional questions