Questions tagged [singly-linked-list]

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

votes
1

answer
49

Views

De allocating dynamically allocated memory of structure in C [closed]

I am new to C programming, I want to learn pointers and dynamic memory allocation, so decided to implement linked list in C. I am having trouble in function deleteFromMiddle() where I am get Sigtrap signal and program ends. Can you point out what i am doing wrong here. PS. Instead of next, i am usin...
1

votes
1

answer
143

Views

2 singly linked list intersection

I'm trying to create 2 single linked lists and find the intersection between them. I'm getting errors such as NameError: obj is not defined for the LinkedList line and would like a working solution. How do I make this work? What am I doing wrong? Am I even close? What is the meaning of life? T...
RyanWolfe9013
1

votes
1

answer
44

Views

Python Linked list removal of node at the end

I am implementing LinkedList in Python 3.6.5. I am stuck with deleting a node from the end: Here's the code: class Node(object): def __init__(self, data): self.data = data self.nextNode = None class LinkedList(object): def __init__(self): self.head = None self.counter = 0 def print_list(self): curre...
Ritwik Tiwari
1

votes
2

answer
123

Views

Adding a node to the head of the linked list

I would like to add a node in a sorted linked list based on the number. This is the struct: struct node { int number; struct node *next; } I am able to add to the sorted linked list correctly but can't get it to change head. Unfortunately I can't change the format of the function declaration so this...
Ibrahim Abouhashish
1

votes
0

answer
68

Views

In a reversely linked list, how to get a sublist of it efficiently?

Supposed I have a reversely linked list, i.e. a data structure where each node points to its predecessor: A
Golo Roden
1

votes
2

answer
101

Views

Linked List Python

class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def __len__(self): cur = self.head count = 0 while cur is not None: count += 1 cur = cur.next return count def append(self, item): cur = self.head while cur is not None: cur...
1

votes
2

answer
140

Views

Linked List - Command Line arguments

I am trying to implement a program that builds a Linked List from the command line. I have two functions, one that creates the list and one that prints it. My issue is that the list when outputting the data from the list is doing it in reverse order? Why is this happening? What can I do? Thanks in a...
J.user94
1

votes
1

answer
37

Views

Rpn calculator: How to free an element that was popped from the stack?

I will first add my code: typedef struct _stack stack; typedef struct _stack_element stack_element; struct _stack { stack_element* top; }; struct _stack_element { stack_element* next; float value; }; void stack_push(stack* astack, float value) { struct _stack_element *elem=calloc(1,sizeof(stack_elem...
Mulch
1

votes
0

answer
44

Views

Understand endless loop in Linked-list algorithm named “Partition”

I am trying to solve the problem '2.4 Partition' from the book 'Cracking the coding interview' : Problem: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. Somehow it goes into endless loop. The solution from the book is sl...
FlorianCiuculescu
1

votes
1

answer
26

Views

Problem printing the singly linked list after reversing the list in C

I have reversed a singly linked list and also swapped head and tail but after reverseList() the output shows only the head of the list. /* Program to create a linked list and read numbers into it until the user wants and print them using functions Author: Shekhar Hazari Created On: 20, January 2019...
Shekhar Hazari
1

votes
1

answer
25

Views

I want to delete a node using the deletea() function in my code but after that i enter an infinite loop in display() function?

i have tried to correct the code several times but in vain void deletea(int x) { int q=0,r; while(list[q].next!=-1 && list[q].info != x) q = list[q].next ; r = list[q].next ; list[q].next = list[r].next ; list[r].next = avail ; avail = r; } void display() { int p = 0; while(list[p].next != -1) { pri...
miachel santa
1

votes
0

answer
36

Views

Is this algorithm for searching for an item in a singly linked list correct?

Step 1: BEGIN Step 2: Accept the item to be searched Step 3: Declare PTR as a pointer pointing to the first node and position marker pos = 0 Step 4: repeat steps 5 - 7 till PTR becomes NULL Step 5: increment pos Step 6: Check whether PTR.INFO is equal to ITEM, if it is, display ITEM is present in th...
Sandy Ketchum
1

votes
0

answer
24

Views

How can we remove a node in a LinkedList given only a pointer to the node AFTER it?

This is similar to the classic interview question of removing a middle node from a LinkedList given only a pointer to that node. Now, what if you were only given a pointer to the node that comes AFTER the node you want to delete? Is this even possible with a Singly Linked List? Any input is apprecia...
Peter
0

votes
0

answer
2

Views

Swapping between two nodes using the given elements which are present in these nodes. Python-3.0 is used for writing code

I have written code but errors are there when I am compiling the code. One error is I am using push function to add new nodes in the list and last_pointer node is working as local node. So every time when I call function, last_pointer is considered as fresh or unnamed node. Another error is in swapn...
ritu agrawal
1

votes
1

answer
136

Views

How to check for common values between two Linked Lists?

public void setIntersection(LinkList list1, LinkList list2) { LinkList list4 = new LinkList(); Node a = list1.head; Node b = list2.head; while (a != null && b != null) { if (a.value < b.value) { a = a.next; } else if (a.value > b.value) { b = b.next; } else if (a.value == b.value){ list4.insert(a.va...
Naveen
1

votes
1

answer
2.8k

Views

Print linked list using operator overloading

I am working on a singly linked list. Several components of the list are not yet set up in main. However, I am working out the definitions in the class and would like to overload the cout to be used with object }; class Queue { private: struct Node { Game game; //data stored in the node stru...
Snerd
1

votes
3

answer
6.6k

Views

Custom Queue Class C++

So I'm trying to create a Singly-linked-list Queue. I'm trying to write a function to add elements, and everything adds fine, but the problem is that its FILO instead of FIFO. I'm not sure how to handle my front and rear pointers. #include #include using namespace std; class Queue{ public: Queue()...
Painguy
1

votes
1

answer
540

Views

Return Pointer of any selected index in circular singly linked list

I designed this code so that I can get a pointer of any position that user wants in a circular singly linked list, I am using cout to return pointer, I want such a mechanism that I can use it with my other function instead of re writing the whole code again, for that I need to do something with retu...
Asad Irfan
1

votes
2

answer
85

Views

overloaded + and - operators, trouble figuring out how to check if the “amount” being passed in is negative

I have overloaded these operators to help me traverse a doubly linked list, but have run into a small bug, and being still new to c++ I am stuck. I never accounted for it the 'amount' entered in would be a negative number. So I think I need to put a check in each operator for a negative number, be...
tattoo3d
1

votes
1

answer
3.3k

Views

Simple linear linked list in VB.NET using class

How can a linear linked list be implemented in VB.NET using class? How can this class be modified for linear linked list? Also the methods for traversing the list, deleting nodes, etc. Public Class clsHol Private dt As Date Private tp As String Private remark As String Public Function setValues(ByVa...
Shri
1

votes
1

answer
115

Views

Problems with setting pointers correctly while creating singly-linked list

struct Number_Node { int number; struct Number_Node* next_number; }; int counter=0; int main() { int input=0; Number_Node* n; Number_Node* h; Number_Node* t; do { cout > input; n = new Number_Node; n->number=input; t=n; if(counter==0) { h=n; counter++; } t->next_number=n; t=t->next_number; n->next_n...
1

votes
2

answer
591

Views

How to move Index element to end of Singly List while shifting all other elements java

Im trying to move the Index element to the end of the singly list while shifting all other elements so If I had a list of {1, 2, 3, 4, 5} and I pass the index 2 to it, it would return {1, 2, 4, 5, 3}. public void moveToLast(int index) throws IllegalArgumentException { if(index size()) throw new Ille...
1

votes
1

answer
393

Views

Seg fault in display function for singly linked list

I am trying to implement my display function (in my main.cc file). However, when I use my curr pointer get data on my Student object and then traverse the list, the program core dumps. MAIN.CC #include 'Node.h' #include 'Student.h' using namespace std; void append (Node **, Node *); //append Node to...
Caleb Faruki
1

votes
1

answer
96

Views

Modifying a Linked List in C

I want to change the value in a linked list. The list is defined as struct car_elements { char *car_rego; double time_parked; struct car_elements *next; }; typedef struct car_elements car; I have created a linked list that has 10 elements. I can change the time_parked easily but having trouble updat...
smuzoen
1

votes
1

answer
172

Views

How to add objects to singly linked list

Information from the file is being read into the Car struct 'newcar'. I need to use the 'sll_add' function below to add that information to the single liked list of type Carlist i named 'list'. I'm just having trouble understanding how it all starts. Thank you for any help. int main (void)//main...
user2812294
1

votes
1

answer
477

Views

_CrtIsValidHeapPointer(pUserData) Error: Realloc() pointer

I got the _CrtIsValidHeapPointer(pUserData) Message when compiling my Code in VS2012 (C). I realized I was trying to use the realloc on the pointer so when I read other posts, many suggested using memcpy and const char **. I really love to do so but I just do not know how. First Fragment of Code: Ty...
Beginner C
1

votes
4

answer
11.3k

Views

Converting an array into a LinkedList

I have an array of characters and I'm trying to convert each character into a node that links to the next node in line. The problem is I keep getting caught in infinite loops and I have no idea why. Here's my code: String map = 'ABBACBCCA'; char[] charArray = map.toCharArray(); ListNode head; ListNo...
Clinton Jooooones
1

votes
3

answer
590

Views

Writing a singly linked list and running test code on it

So we've got this assignment where we're supposed to write a singly linked list. I've done that part but the teacher wants us to add a method names isHealthy() that tests some conditions. I'm having trouble implementing code that tests one of the conditions. Here is the class and constructor and the...
user3043462
1

votes
1

answer
1.5k

Views

C++ Singly Linked List text file

I am pretty new to linked lists and they are confusing me very much right now. I need to write a code that reads a text file of integers into a singly linked list. I have already written the code to read in the text file and it works properly, but now I need to read the integers from the file into t...
user3251244
1

votes
2

answer
160

Views

Explain the output of the stack implemented using linked list

I am creating a stack with a linked list. I have coded the push and display stack portion but it is just producing the last entered data as output when I am trying to display the stack content. Why is this happening? struct LinkedStack { int data; struct LinkedStack* next; }; struct LinkedStack *fir...
arindrajit
1

votes
4

answer
759

Views

Removing the Last Node of a Linked List in C++

I can't seem to figure out what the problem is, my code compiles and runs past the problematic function shown below, but when I attempt to display the contents of all the nodes, I get this: Unhandled exception at 0x50CE1F98 (msvcp100d.dll) in LinkedList.exe: 0xC0000005: Access violation reading loca...
user3403204
1

votes
1

answer
146

Views

Finding two minimum using INT_MAX in linked list is giving segmentation error

I am trying to find the minimum two numbers in a linked list since yesterday but still not able to do yet so decided to ask at stackoverflow. My logic to do so is : (1) First set min1->freq and min2->freq to INT_MAX they are both of node type pointer. (2) Then i set the second smallest then the firs...
Sss
1

votes
1

answer
3k

Views

Python HashTable & linked lists. How to print a list from the HashTable class

I have an assignment and what I have to do is make a hash table (hash value x^2 % tablesize) and when given a key value I have to add it to the hashtable. But, if two keys have the same hashvalue I have to make a linked list at that slot in the hashtable. Here's my code... class Node: def __init__(s...
zachary
1

votes
1

answer
625

Views

Using fgets and linked lists

I am trying to get user input from fgets() and save the entry into a linked list but it isn't saving the entry into the linked lists, but if I directly put it in the call, it does. ex. add_to_list(&list, 'hello'); How do use fgets to save into a character array (called word), which I can stick into...
user1798299
1

votes
2

answer
817

Views

How to check the symmetry of an integer singly-linked list in C++?

I have a question about singly-linked list in C++. An integer array[0..n] is called symmetric if array[0] = array[n], array[1] = array[n-1], ... For example: 1 2 3 4 5 4 3 2 1 So, is there any way to check the symmetry of an integer singly-linked list? I've thought about copying the value of them d...
Noob From Mars
1

votes
1

answer
1.1k

Views

Path between two cities using BFS in java

I am trying to make a data structure for storing Map contents like roads and cities information. I am using linkedlist of vertices/cities and when a vertex/city is created it creates another linkedlist of edges in its constructor. I need an algorithm to find a shortest path between two random citie...
Faiqa Babar
1

votes
1

answer
136

Views

(2nd term C++)(Homework)Linked Lists Advice

I am currently writing a program in C++ that uses linked lists. The problem is, I can't really seem to figure out how to use linked lists. I know (A little) how to use classes though. This is the LinkedList.h file given by the teacher (So I can't modify anything) LinkedList.h struct Node { void* dat...
user262163

View additional questions