singly-linked-list

Deleting node inside of linked list

和自甴很熟 提交于 2021-01-28 13:46:08
问题 I'm stuck on this particular function that frees all even nodes from the linked list. I've figured out how to free all the nodes from a linked list but I cannot figure this out. The code i'm posting is very wrong. What I don't understand is how to use a node *temp variable and link it to the head->next node, as the head is what is being freed (because it is even). Also, at the end of the while loop, I know that I need to increment to the next node in the list, but I seem to be doing that

Copy an array to linked List in each data field

冷暖自知 提交于 2021-01-28 09:16:36
问题 First, sorry if my question has already been answered. I found some threads that are (in a way) similiar but I was not able to solve my problem. Second, I am new to single-linked-list in C so I would be happy if you could answer my question as easy as possible. I made a simple linke-list, that has characters in it: #include <stdio.h> #include <stdlib.h> // declaration of node struct _Node_ { char data_string; struct _Node_ *next; }; int main() { //a simple linked list with 3 Nodes, Create

Find cardinal number of a list in C

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-27 06:31:39
问题 How can i find only the elements that appears once in the list and return the cardinal number?For example if my list consist of {3,2,1,1,2,4} i expect for return the counter to be 4 and not 6 cause we do not count the duplicate numbers. Here is the code that i have written so far. struct Node { int data; struct Node *next; }; int Find_cardinal(struct Node *start) { struct Node *ptr1, *ptr2 ptr1 = start; int counter=0; /* Pick elements one by one */ while (ptr1 != NULL && ptr1->next != NULL) {

How would you get the nth node from the tail in a singly linked list (in one traverse)?

社会主义新天地 提交于 2020-07-05 10:27:06
问题 So I got this question from an exam. How would you get the nth node from the tail in a singly linked list? Each Node has a value and a next (which is a pointer to the next value). We are given this: getNodeFromTail(Node head, int x) { } So the way I did it is to find the length of the list by traversing it once. Then going again to get the (length - x) node. So in total, 2 traversals. getNodeFromTail(Node head, int x) { int length = 0; Node headdupe = head; while (headdupe.next != NULL) {

How to handle recursion in member functions?

天涯浪子 提交于 2020-06-27 18:35:27
问题 E.g., I have an empty function to clear a linked list: void empty(Node* head) { if (head->next) { empty(head->next); } delete head; head = nullptr; } But then I created a class for the linked list, so now I don't need to pass the head argument: void empty() { if (head->next) { empty(head->next); } delete head; head = nullptr; } But empty(head->next) line is obviously wrong as empty doesn't take any arguments. The idea comes to my mind to create a function inside a function (with lambda),

C - add element to sorted-linked-list (alphabetical sorting)

北城余情 提交于 2020-06-17 14:20:08
问题 I have to do some kind of work here. My task is to write words from file to a struct in alphabetical order. I thought "hmmmm.. that's easy", but not - it isn't. Or just I f*cked it up :( Can you guys tell me how should I alphabetically add words to my list using these 3 functions? Or maybe it is other way to do this? I share most of my code. struct Word { char* word; struct Word* pNext; }; typedef struct Word Word; bool IsLegitWord(char* word){ int word_length = strlen(word); int i = 0; for(i