Questions tagged [linked-list]

1

votes
1

answer
99

Views

Don't understand how method “delete” work in linkedlist

please help me to understand how do method delete(with specify value) work? I understood everything except this point. I wrote method delete but don't understand how changing field next in Link "previous" weigh with Link first, I know that the next Link will be missed, but this Link also will be mis...
1

votes
4

answer
6.6k

Views

insert at end of linked list

I am writing a simple function to insert at the end of a linked list on C++, but finally it only shows the first data. I can't figure what's wrong. This is the function: node* Insert(node* head, int data) { if (head == NULL) { head = new node(); head->data = data; head->link = NULL; return head; } e...
mistletoe
0

votes
0

answer
4

Views

AddressSanitizer errors when testing the performance of singly linked list

I am working on a creating a singly linked list. To create that I have wrote structures such as struct node and operation like list_free_node. Now this is my first time using malloc and pointers, and I get a few errors (I think related to those two mentioned concepts) that I have never seen before....
mroWsymaS
0

votes
2

answer
17

Views

Deleting the middle node of a dynamic linked-list, IF it exists

For a programming challenge, I need to write a method that will delete the middle node of a linked list, only IF the list has an odd number of nodes. It should return the info in the middle node if it exists; and null otherwise. For example, if the list is a->b->c->d->e, c is deleted. And if the li...
K. Xiang
0

votes
2

answer
24

Views

how do I recursively find an element in a linked list but the last element is not equal to None

Im trying to find the last value on a linkedlist but the last node does not does not point to none. node 4 is not pointing to none so how do I find it recursively. node1 = Node(44) node2 = Node(220) node3 = Node(320) node4 = Node(402) node2.setNext(node1) node3.setNext(node2) node4.setNext(node3) s...
123WEB
-2

votes
0

answer
22

Views

Cannot figure out why my code doesn't put data in my node

Main function: { string newName = ""; string newAuthor = ""; int newYear = 0; Book book; cout > newName; b.setTitle(newName); b.print(); cout > newAuthor; b.setAuthor(newAuthor); b.print(); cout > newYear; b.setYear(newYear); bookList.addFront(book); b.print(); pressAnyKey(); Book.cpp: #include "Boo...
Hank Logan
0

votes
0

answer
23

Views

Struct pointer strange behavior

I'm trying to make a BrainFuck (BF) interpreter using a two-way linked list. However, the way I'm moving through this list seems to have a rather strange impact on cells. So first here's the part of my program that gives me a hard time int main(int argc, char **argv) { Cell c = {NULL, NULL, '\0', NU...
Tix'at
-1

votes
0

answer
24

Views

My pointer is still NULL even though I am assigning a non-null pointer to it

I don't understand why my code doesn't work. As you can see in the picture below, Xcode displays what has an address and what doesn't. It works on my friend's laptop perfectly, but it doesn't for me. I am trying to split the linked list into 2. MyList1 contains the first half the list while MyList2...
SirCobalt
0

votes
1

answer
16

Views

How do you create a LinkedList, which contains loops, in Python?

I'm trying to come up with a test case for the solution given by hackerrank for detecting cycles in a linkedlist with python. The solution given by hackerrank is: class Node(object): def __init__(self, data = None, next_node = None): self.data = data self.next = next_node def has_cycle(head): fast =...
zcahgg1
0

votes
2

answer
23

Views

How to print the entries made in my LinkedList?

I need help concerning my coursework in Java Programming. I am creating a console Flower (shop) application which stores the flower's name, colour, age and price. However, I am having problems printing the LinkedList I created to store records of the Flowers added. I have tried tweaking with the sho...
Audu Ibrahim
0

votes
0

answer
5

Views

Creating a second inverted singly-linked list based on another list

So I need to create a function that takes a normal singly-linked list and creates another list based on it, except the first element in the new list will be the last element in the original list and so on. It doesn't sound hard at all but my function for some reason only ever returns the same numbe...
Matt17BR
1

votes
2

answer
89

Views

C Doubly Linked List Program

I have been writing a C program to work with Doubly Linked Lists. But it does not work on Dev Cpp. Can anybody point out any error? It works until the part where I add a new record and then it crashes. I'm guessing there is a problem with the scanf() function. //C double-linked list program struct s...
1

votes
4

answer
1k

Views

Inserting into sorted linked list - Java

I need to maintain a sorted data structure from which items can be deleted and added. For that I decided to choose a linked list. Each data item contains a letter and some numbers such as these: A1480, A1488, B1297, C3119 These need to be maintained in order. I have written code for it which first f...
Rosie
1

votes
3

answer
859

Views

Dummy Node in Linked List in C++

I am trying to develop a way in which I can solve Linked list problems without having to care about the head node in any special way i.e. In linked list problems we usually deal with the head pointer separately before we start with the next nodes. I found a way: Use a dummy node so that the actual l...
adrian008
0

votes
1

answer
25

Views

How to allocate space for the head of linked list in C?

struct node { int data; struct node *next; }; int main() { struct node *head = malloc(sizeof(struct node)); struct node *current = head; ... }; Though this piece of code can run without any warning or error, Valgrind will give some messages saying Conditional jump or move depends on uninitialised va...
user8314628
1

votes
3

answer
3.6k

Views

LinkedList implementation in Typescript

I am currently learning data structures and I am facing several problems when trying to implement LinkedList in TS. I added several methods but though it seems to work, the output is really weird. My questions are in comments. My code: function LinkedList () { //why doesn't fat arrow syntax wor...
kazanDipi
0

votes
2

answer
27

Views

How is unrolled linked list better than linked list?

I was studying the book "Data Structure and algorithms made easy" and but I got confused while learning "Comparing Linked Lists and Unrolled Linked list"... what is overhead? Why he is only stating 8 bytes of overhead for 100 elements array?
Saumya Pandey
1

votes
2

answer
2.4k

Views

What is the difference between linkedlist and queue?

I'm new to data structures and it seems like both data structures have more similarities. In this answer it says that there is a difference in interface. Please explain it.
dilusha_dasanayaka
1

votes
2

answer
151

Views

Counting occurrence in singly linked list by nodes

I am writing a simple app that gets a list and saves the objects as nodes in a singly linked list and we can add(), remove(), copy(), etc. each node depending on the given data set. each node has a char value which is our data and an int count which counts the occurrence of the related char. e.g. fo...
Nitwit
1

votes
3

answer
54

Views

Assigning NULL to the head node in a linked list in C

Please see the full code below. I have an initial array named arr. I'm using a linked list to store some indices via the append function. After I got the indices, I store them in linked list and use clearList to change the corresponding values to 0 (In this example arr[2] and arr[4]). Finally, I f...
volkang
1

votes
1

answer
718

Views

How to point one structure to another

I need help pointing *user_playlist to a playlist node. How can I do this? struct playlist_ { int album; int track_num; struct playlist_ *next; }; typedef struct playlist_ playlists; struct users_ { int user_ID; playlists *user_playlist; struct users_ *next; }; typedef struct users_ users; p_users...
Learning C
0

votes
0

answer
6

Views

How to change sinlgy linked list to doubly linked list?

I have a program that I am supposed to change it from a singly linked list to a doubly linked list. This means that I use pointer that points to the next node and a pointer that points to previous node. How do I do this while recycling my previous code. Is there a way to do this with minimum steps i...
Samuel Iorga
1

votes
7

answer
8.1k

Views

Recursive MergeSort on a Linked List using Java

I searched on the net for a good clean and simple implentation of a merge sort algorithm in Java for a Linked List that uses recursion. I couldn't find a nice one so Im trying to implement it here. but Im stuck. Here is what I have so far: public List mergeSortList(Node head, Node tail) { if ((head...
banditKing
4

votes
4

answer
123

Views

Doubly Linked List in Prolog

I have been learning Prolog in my spare time for about 8 months to a year and now I am moving on to tackle implementing some of the classic data structures and algorithms . I am interested in achieving a doubly linked list in Prolog, but quite baffled as to how to proceed . I was attracted to Prolog...
S. Selfial
1

votes
4

answer
518

Views

java infinite loop while using collections [closed]

Please suggest me to solve the below infinite loop . Class object contains the collection of same type objects. While converting to String , The object calls the toString of each object in the collection. Hence it leads to infinite loop. Please dont use any static variables. import java.util.LinkedL...
ranganath111
1

votes
3

answer
6.9k

Views

Add linkedlist to linkedlist, JAVA

I want to design a linkedlist to another linkedlist, so far I have this method in MyLinkedList class: public void addList(int index, E e){ if(index == 0){ addFirst(e); } else if (index >= size){ addLast(e); } else{ Node current = head; for(int i = 1; i < index; i++){ current = current.next; } Node t...
user1657294
1

votes
3

answer
85

Views

How can I refer to what called a function in C++?

I was wondering if there is a way in C++ to know what called a function? Like the this keyword in Java or JavaScript. For example, I have a function called insert, which inserts an item into a linked list, I want the linked-list that called those the function insert to call two other functions. How...
IrfanM
1

votes
1

answer
209

Views

Why is this merge sort function returning linked list with zeroes (c++)?

I've got this merge sort function namespace sorted{ template class list { /* other stuff */ list* slice(int from, int to){ from = (from < 0) ? 0 : from; to = (to > this->len) ? this->len : to; list* result = new list(); node *n = this->head; int idx = 0; while (n && (idx < this->len)){ if ((from val...
bitcycle
1

votes
3

answer
391

Views

ToString for templated linked list?

Here's my code: template class list { private: node* head; node* tail; int len; public:...
bitcycle
1

votes
3

answer
676

Views

Way of implementing LinkedList, Java

I'm reading this book and there is this chapter on liked list and it starts with the implementation of a single linked list, it goes like this: Creating a Linked List: class Node { Node next = null; int data; public Node(int d) { data = d; } void appendToTail(int d) { Node end = new Node(d); Node n...
Hoto
1

votes
2

answer
5k

Views

Creating a linked list from a text file

I'm teaching myself about linked lists and have proposed a basic problem to solve. I want to line by line read a text file that will have names and add each name to my linked list. An example of the text file would be: John Jacob Jingleheimer Smith I am having trouble figuring out how to dynamicall...
sdsd
1

votes
3

answer
6.4k

Views

Destructor for a doubly-linked list that points to its value

Suppose I have a doubly-linked list defined by the class class list { /*...*/ private: struct node { node* prev; node* next; int* value; } node* first; //NULL if none node* last; //NULL if none /*...*/ } If I wanted to make a destructor for this list do I have to explicitly delete the value? list::~...
Bob John
1

votes
4

answer
1.9k

Views

How to dynamically allocate array of structures in c?

I want to implement a simple word counting program which will open a text file, which is supplied as a command line argument, and then count the number of words, number of non-alphabetical word (ex : 1998, 2-3, and so on) sentences and paragraphs in that text file. Assuming a character array between...
Ege
1

votes
4

answer
777

Views

Removing a node from a linked list in c++

I'm trying to learn C++ and there is a small confusion I have. The text which I am learning from tells me that if I want to delete a node of type const T& I should first create a new pointer of that node type, then delete it using the inbuilt C++ delete[]. However, what happens if I just set the lin...
Evil Washing Machine
1

votes
3

answer
1.3k

Views

linked list insertion sort in c

the program should do the insertion ascending sort for the nodes,first it should check the names and if the names are equal it should sort the ids,i do not know what is the issue that does not sort properly. # include # include # include typedef struct nd{ int id; char name[20]; float gpa; struct...
maziar parsaeian
1

votes
3

answer
1.6k

Views

A recursive function that appends the end of a linked list to the beginning of another and returns a pointer to the beginning of the new list

I am having trouble figuring out how to have the function return the "Head" of the new list recursively. They append just fine, but recursively I cannot figure out how to "save my place" so to speak. struct node { int value; node *next; }; node* append(node *&L1, node *L2) { if(L1->next == NULL) { L...
Craig Smith
1

votes
2

answer
1.1k

Views

How to clear a linked list using double pointer?

Hi I'm trying to make a function that clears a linked list that *first will point to, then the node **first should be freed and the pointer *first set to NULL. I'm having trouble grasping double pointers and can't get this to work correctly.
Jonathan Swiecki
1

votes
3

answer
994

Views

adding nodes into singly-linked list

I am trying to learn linked lists.I just wrote a program illustrating a linked list but it doesn't seem to work.Here's the code: #include using namespace std; struct node { int data; node* next; }; int main(int argc, const char * argv[]) { node* trav; node* root; root = new node; trav = root; trav...
parth
1

votes
1

answer
406

Views

Bus Error: 10 in C while inputing text from .txt file into nodes

The code I am trying to implement is a method that reads a .txt file and converts the strings into nodes. Essentially, as I am reading the .txt file, I first check for non-letters (the word cannot start with a number, nor can the word have a non-alphanumerical in any index of the word). Once it find...
user2074228

View additional questions