Questions tagged [singly-linked-list]

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
-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
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
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
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
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
68

Views

Linked List strange behavior

I don't know how to ask this question, so I present it with an example. I hope you understand it. Here's the structure node: typedef struct NODE { struct NODE *link; int data; } SList; Suppose I use the appendList(Node *list, int data); [you can use your own also] function to insert the node into a...
Ashish Rawat
1

votes
2

answer
131

Views

Linked List inserting trouble

I am having trouble with inserting items in linked list. All the elements end up having the same *data content that is inserted in the last. The program compiles successfully. I use gcc and gdb. I am new to coding so please mention troubles if any that you see in my programming style. typedef struct...
new-kid
1

votes
3

answer
251

Views

Why can't I delete the first node (head node) directly?

I have created a simple linked list using c where we can insert and delete elements at any place in the list. The code worked properly until i tried to delete the first node using the following code. typedef struct l_list { int data,index; struct l_list *next_node; }node; static int total_node=0; no...
APan
1

votes
4

answer
7.4k

Views

Sorting a Singly Linked List With Pointers

I am trying to sort a singly linked list using bubble sort by manipulating ONLY the pointers, no keys. The following gets stuck in the for loop and loops infinitely. I don't understand why this is. Can anybody explain to me why the end of the list is not being found? Node* sort_list(Node* head) { No...
Mark S.
1

votes
1

answer
224

Views

Cannot Fix Abstract Method Error (Java)

I'm working on learning Java and I'm trying out single linked lists. I am implementing an interface I've made for it too, but I keep getting an error saying that my main class LinkedList "is not abstract and therefore does not override method search(java.lang.Object) in LinkedListInterface". What am...
Maggie S.
1

votes
1

answer
169

Views

linkedlist when It reach end of list

public class LList2 implements ListInterface { private Node firstNode; // head reference to first node private Node lastNode; // tail reference to last node private int numberOfEntries; public LList2() { clear(); } // end default constructor public final void clear() // NOTICE clear is not final i...
David Hang
1

votes
2

answer
352

Views

char& operator[] overload reference return? (linked list)

So, I've been getting this error when it comes to returning the proper type of variable. It says something to the effect of "initial value of non-const must be lvalue" Can anyone help me revise my code to properly return the character at the given index? This is a singly linked list project BTW. ch...
Marksman46
1

votes
4

answer
4.2k

Views

Singly linked list - push_back

I have to create method push_back which will add an item to the end of my list. But I have one constraint - I can't check if head is empty (if head is null) I don't have idea how I can do this. Here is my code: #include #include struct node { int value; struct node* next; }; void print(struct node...
Kaki
1

votes
1

answer
371

Views

Linked list for stack, head->next keeps becoming null

//Colin James P. Naranjo //CMSC123 CD-1L //This program demonstrates postfix evaluation through pop and push functions #include #include #include typedef struct node{ //Uses a combination of typedef and tagged structure for the singly linked list char value; struct...
James Liam
1

votes
2

answer
561

Views

Singly linked list C, printing

I'm a beginner in developing, so my sensei gave me a task to complete in which I need to enter a couple of strings in linked lists and after I enter print, they need to be printed in the correct order, from the first to last. Here is what I got: #include #include #include typedef struct Node...
Mirakurun
-1

votes
0

answer
9

Views

C: Memory leak implementing a simple linked list

I created a linked list that stores integers. The program appears to run fine but Valgrind informs me that there is a memory leak. I am not sure how this is possible. The code is provided below along with the output and Valgrinds assesment. Thank you. main.c #include #include #include "linked_l...
user9477548
1

votes
2

answer
39

Views

How is this singly linked list destructor causing an infinite loop?

I wrote a singly linked list implementation using a struct. It is not part of an outer class that manages the operations on the list. Instead all of the operations are handled directly with the Nodes. I understand that if the struct definition was part of a class, say ListManager, calling the destr...
user3502362
2

votes
1

answer
35

Views

singly linked list c++ constructor, destructor and printing out

I am a beginner learning c++, and currently making a singly linked list. I have faced some problems and I thought for a very long time, searched a lot but still do not have an answer for this code so I am begging for some help.. So this is my linked.h template class Node { public: T data; Node* nex...
Erica
1

votes
5

answer
105

Views

singly linked linked list C reverse data reading

I have a linked list which I am trying to read in reverse, however I'm having problems with doing so. This is my struct. typedef struct node{ int x; struct node *next; }node; The problem is with my reading function. This is the function: void read(){ node *iter; while(r->next!=NULL){ iter=r; while(...
Seçkin Güleviz
1

votes
2

answer
905

Views

Singly linked list assignment operator overload in C++

I've the following singly linked list implementation. template struct node{ node(T data):data(data),next(nullptr){} T data; node * next; }; template< typename T> class slist{ node* head; int size; public: slist(node* head):head(head), size(0){} slist(const slist& rhs){ node* temp = rhs.getHead();...
Antithesis
2

votes
4

answer
6.6k

Views

quicksort on singly-linked list

My quickSort doesn't work. I'm particularly unsure about what to pass through to the partition algorithm and how to manage the pivot as in one case it becomes a header node and in the other case a last node. I based my approach on the solution for arrays. Here's my attempt. Any ideas? Please note th...
raoulbia
2

votes
2

answer
959

Views

C++ Linkedlist simple question

I'm trying to check if an entity exists in a given linkedlist. This is my code: bool LinkedList::existByID(int ID) { //create node to search through the list Node * helpNode; //start it at the top of the list helpNode = head; if (head == NULL) { return false; } //while the item has not yet been...
BIU
1

votes
3

answer
62

Views

Search and delete from linked list

I'm having difficulties with this function. The function is supposed to search a student in a linked list and delete this node or print an error message if the student wasn't found. The following code does not work properly, it deletes the next node after the node which is being searched for. void v...
Babs91
2

votes
2

answer
710

Views

Traversing a custom linked list

I'm writing a program to simulate memory fragmentation. The input file tells what segments need to be input at what time. A sample file is: N C 200 P 1 2 3 P 2 3 4 P 2 3 1 R E where C is the memory size, P is the segment in the order (size, start time, and life time), and R (should) prin...
Bre
2

votes
3

answer
2.5k

Views

Removing the lastNode in a singly Linked List

Can I check how do we remove a last node from a single linked list? is it the same as how we remove the first node? remove first Node def deleteAtHead(self): temp = self.head self.head = self.head.next delete temp remove last Node def deleteAtTail(self): prev = None temp = self.tail self.tail= self....
overflowhow
2

votes
1

answer
32

Views

When creating global struct variable for linked list it says illegal initialization otherwise it works fine

I have created this structure and I am trying to perform the basic operations with the help of functions which I created. My program works but I have to declare the temp variable inside every function. I tried making it global but it says "illegal initialization". struct node { int data; struct nod...
Gaurav Bahadur
3

votes
1

answer
70

Views

Adding an element at the head of a linked list, if the element isn't in the list

I have the following data structure: typedef struct Word { char *word; int occur; struct Word *next_word; } * WordList; I'm trying to implement a function that adds a string (word) to a WordList. If it's already present in the list, then increment its occurrences, otherwise, add it to the head. This...
throwaway
1

votes
2

answer
63

Views

I don't know why my linked list pointer isnt moving

I'm trying to write a piece of code that adds elements to a list. typedef struct things { int value; struct things *next; } something; int main() { int input = 0; something *head = NULL; something *current = NULL; current = head; //current points to head address while(input != -1) { scanf("%d", &inp...
Kevin
17

votes
8

answer
11.2k

Views

How to reverse a linked list?

Node reverse(Node head) { Node previous = null; Node current = head; Node forward; while (current != null) { forward = current.next; current.next = previous; previous = current; current = forward; } return previous; } How exactly is it reversing the list? I get that it first sets the second node to...
user1176235
1

votes
2

answer
2.2k

Views

Reversing a singly linked list in C [duplicate]

Possible Duplicate: How to reverse a singly linked list using only two pointers? This is C code to reverse a linked list. But this isn't producing the desired output. struct node *temp,*prev; while(head->next!=NULL) { temp=prev=head; while(temp->next->next!=NULL) { temp=temp->next; prev=prev->next;...
Srikanth R
1

votes
1

answer
1.4k

Views

Adding custom structure to GSList with Glib

I'm trying to add a structure to a singly linked list with the function g_slist_append(list, &structure). This seems to work (it's adding the pointer), however I can't seem to find a way to view the elements in the structure when reading the linked list. My structure looks like this: struct customst...
Evert
1

votes
2

answer
2.5k

Views

Getting the current value and position of a node within a list

I'm trying to create code that will insert anywhere in a list. I will also convert this to replace the value of the node in the given position. So far my code is: #include #include struct node* createNode(int,int); struct node { int data, posi; struct node *next; }; struct node *head = NULL; struct...
magicianiam
1

votes
1

answer
245

Views

Implementing Popback in constant time in a singly linked list

I wanted to know if it was possible(It should be as std::list seems to do it) to implement a PopBack() operation on a singly linked list in constant time and how? I am assuming that we store head and tail pointers. In such a case, PushBack(), PushFront(), PopFront() can be easily implemented in con...
Arun Valakottial
4

votes
4

answer
127

Views

C: Removing a node from singly-LinkedList1 and inserting it at the head of singly-LinkedList2

I'm doing a computer science project that simulates an operating system's process scheduler. I have multiple singly-linked lists, and need to move nodes between them. I am trying to write a general function to accomplish this, but I think my weakness using pointers is holding me back. How do I imple...
python_newbie
1

votes
2

answer
1.8k

Views

Linked list shows only first node element on printing

I am trying to create a linked list in order to enhance my concepts of pointers and address. I have to create linked list in following way: (1) Read all the nodes together at once at terminal. (2) Then show the final linked list so formed at last. How i try to do so ? I am reading first the size of...
user252990
2

votes
2

answer
99

Views

About a simple linked list to set struct pointer correctly

This code creates a dynamic linked list which also memory allocate and free correctly, but there is a small step I'm quite puzzled. It seems it didn't set the first struct pointer start->next to the next, which I think it should. But when I try to run it, it works well. Then I try to add start->next...
Lee William
2

votes
3

answer
461

Views

How to implement a Boolean expression to find duplicates in a linked list?

I'm having trouble with my function that is suppose to return true if the linked list has duplicates of a certain value passed into the function Note: This is a singly linked list.
Crowning
5

votes
3

answer
3.5k

Views

What is the difference between SGI slist and C++11 forward_list?

Both SGI slist and C++11 std::forward_list appear identical to me unless I have missed something; both implement a singly-linked list. I assume there is a difference though as the C++ Standard Commitee didn't adopt the name slist and instead chose a new name, forward_list, when they added the contai...
Ricky65
4

votes
1

answer
86

Views

Sorting names and storing them to an Array cell with LinkedList

So the idea is to get an input which is a String(a name to be specific), then store it in an Array with the size of 26 to its respective cell. The sorting goes this way: names that start with 'A' goes to cell 0, names that start with 'B' goes to cell 1 and so on. Now, a cell contains a LinkedList wh...
Helquin

View additional questions