linked-list

linked list implementation in C (debugging - incorrect output)

血红的双手。 提交于 2019-12-13 04:23:53
问题 I created this linked list program in C. It reads data in from a3data.txt (The contents of this text file are pasted after the output, below). In the text file, INSERT and REMOVE are commands. I want to be able to read the command INSERT, and insert the next integer (next line) into the list. The REMOVE command should remove the last node from the list. As you can see, my remove function is not working properly, and I don't understand why. Will someone please help me debug this? OUTPUT linux

Why do I get “request for member in something not a struct or union” from this code?

寵の児 提交于 2019-12-13 04:18:30
问题 I'm trying to teach myself C by coding up a linked list. I'm new to pointers and memory management and I'm getting a bit confused. I have this code: /* Remove a node from the list and rejiggle the pointers */ void rm_node(struct node **listP, int index) { struct node *prev; struct node *n = *listP; if (index == 0) { *listP = *listP->next; free(n); return; } for (index; index > 0; index--) { n = n->next; if (index == 2) { prev = n; } } prev->next = n->next; free(n); } to remove an element from

adding items to linked list without allowing duplicates

自作多情 提交于 2019-12-13 03:58:30
问题 I have to add items to a linked list without allowing any duplicates: the list typedef struct node { double info; struct node *next; } NODE; my function: void addToEnd(NODE **lista, double info) { NODE *novi = (NODE *) malloc(sizeof(NODE)); novi->info = info; novi->next = NULL; if (*lista == NULL) *lista = novi; else { NODE *tmp = *lista; while (tmp->next) { if(tmp->info == info) {free(new); return;} tmp = tmp->next; } tmp->next = novi; } } It does work if the numbers aren't just besides each

Drawing consisting of two linked lists with pointers and values

China☆狼群 提交于 2019-12-13 03:54:59
问题 I just started taking an algorithms course, however, due to some family issues, I did not have a chance to participate in the first two lectures. Now I'm in a bit of a pickle, because I don't understand much of what is going on. Above is a picture of a task that I need to solve. From what i understand, L0 is a list containing all values of S and L1 is a list containing all values of S and a pointer to the corresponding value in L0. However, what I do not understand is when they start bringing

Queue in and out according to clients’type

邮差的信 提交于 2019-12-13 03:43:41
问题 Queue in and out according to clients’type Question Based on previous implementation, modify the LIST command, so that it will print VIP clients first then ordinary clients in ascending order by queue number. Same as OUT command, VIP will be queued out first then ordinary clients. Input IN 1000001 Ordinary IN 2000003 VIP IN 2000009 VIP OUT OUT OUT OUT IN 1000007 Ordinary IN 2000005 VIP LIST OUT QUIT Output IN:1 1000001 Ordinary 0 IN:2 2000003 VIP 0 IN:3 2000009 VIP 1 OUT:2 2000003 VIP OUT:3

Deleting a node from singly linked list has the error “cannot move out of borrowed content”

冷暖自知 提交于 2019-12-13 03:29:45
问题 I am making a singly-linked list. When you delete a node, the previous node's next should become the current node's next ( prev->next = curr->next; ) and return data if the index matches. Otherwise, the previous node becomes the current node and the current node becomes the next node ( prev = curr; curr = curr->next; ): struct Node<T> { data: T, next: Option<Box<Node<T>>>, } struct LinkedList<T> { head: Option<Box<Node<T>>>, } impl LinkedList<i64> { fn remove(&mut self, index: usize) -> i64 {

How to search a specific node in a graph structure in C?

被刻印的时光 ゝ 提交于 2019-12-13 03:06:45
问题 Not that I have time to discuss this properly to reach a conclusion and adapt my code because the phase one (of three) of a school project is in 24hrs, but at least I need to know if I did the correct decision. I'm using linked lists and here's my structures: typedef struct sCity { int cityID; char *cityName; struct sCityLink *links; struct sCity *next; } nCity, *City; typedef struct sCityLink { City cityLinkParent; City cityLinkTo; struct sCityLink *next; } nCityLink, *CityLink; Basically, I

Why is concatenating two linked lists by assigning pointers wrong?

懵懂的女人 提交于 2019-12-13 03:06:10
问题 So our lecturer just showed us how to concatenate two linked list but then went on and showed us and example that would be wrong if implemented,I don't seem to quite understand why it won't work as good,since it wasn't explained.What would be the problem if I concatenate two linked lists this way: template <typename T> void LList<T>::concat(LList<T> const & list) { end->next = list.start; end = list.end; } 回答1: The approach used in the question is wrong because it incorrectly shares Ownership

singly linked list in the C++ standard library or other widely-used libraries?

假装没事ソ 提交于 2019-12-13 02:37:37
问题 Seems that there is only doubly linked list (but no singly linked list) in the C++ standard library, right? Is there any widely-used C++ libraries with singly linked list? 回答1: There is slist, which is an SGI extension ( __gnu_cxx::slist ) #include <iostream> #include <iterator> #include <ext/slist> int main(int argc, char** argv) { __gnu_cxx::slist<int> sl; sl.push_front(1); sl.push_front(2); sl.push_front(0); std::copy(sl.begin(), sl.end(), // The output is 0 2 1 std::ostream_iterator<int>

Deleting Node in Linked List C++

不羁岁月 提交于 2019-12-13 02:25:21
问题 So I've been searching forums, but im still very new to the language and linked lists so I can barely decipher the results. basically I made a delete function for my linked list. I can currently Create a list, traverse the list, sort the list, search the list, and insert before any node in the linked list. I recycled some code from the insert to locate the point in the list where I could delete. My main point of confusion is how to link the previous points to the node that is after the one I