linked-list

free of doubly linked list

有些话、适合烂在心里 提交于 2019-12-24 12:09:43
问题 I am using a doubly linked list in a C program. I am getting confused about freeing the memory. Should I free the list node by node? Or, by assigning head and tail nodes to NULL? 回答1: You have to traverse the list and free each node. If you only set the head and tail pointers to NULL the list nodes are still in the heap and you have no pointers to them and that's a classic memory leak. Here's some pseudocode: Node* current = head; while( current != NULL ) { Node* next = current->Next; free(

Scanf skips second line of input, always

让人想犯罪 __ 提交于 2019-12-24 11:35:14
问题 I'm trying to read lines of text data, and store them in a linked list. Input data looks something like this: David Copperfield Charles Dickens 4250 24.95 32.95 10 6 END_DATA The way I read the data is to first read the first line to see if it is END_DATA. If it's not, then I pass the first line into a function which creates a linked list Node with the book data inside. For some reason, scanf does not read the second line after I pass the first line to the function. When I try to print the

quicksort with linked lists

南楼画角 提交于 2019-12-24 10:26:38
问题 I have written down the following program that uses the quicksort algorithm to sort how ever many ints are put into the command line using linked lists. Not only am I getting an ISO C90 error about mixed declarations but there is a memory leak somewhere in my code and I am not sure how to fix it. Any help would be appreciated! #include <stdio.h> #include "linked_list.h" #include <stdlib.h> #include "memcheck.h" #include <string.h> #include <assert.h> node *quicksort(node *list); int

How do I shuffle nodes in a linked list?

被刻印的时光 ゝ 提交于 2019-12-24 09:14:59
问题 I just started a project for my Java2 class and I've come to a complete stop. I just can't get my head around this method. Especially when the assignment does NOT let us use any other DATA STRUCTURE or shuffle methods from java at all. So I have a Deck.class in which I've already created a linked list containing 52 nodes that hold 52 cards. public class Deck { private Node theDeck; private int numCards; public Deck () { while(numCards < 52) { theDeck = new Node (new Card(numCards), theDeck);

Structure In C for linked list

邮差的信 提交于 2019-12-24 09:11:54
问题 Sorry for asking such a stupid question but I am really confused. struct Amit { int a; struct Amit *link; } *start; Here both *link and *start are used to point to a node of a linked list, but what's the difference between these two and why can't we put *start inside the structure body? 回答1: The link is a member of the structure type. Every structure of type struct Amit has one. The start is a variable of type 'pointer to struct Amit '. At any given time, there can be at most one variable

Writing a linked list to binary file(C)

余生长醉 提交于 2019-12-24 07:12:54
问题 So I have set up a linked list and read data from a file, done some calculation and manipulation and now I want to store the new list into a binary file. Here is my struct setup: typedef struct Comp{ char name[5]; char node1[5], node2[5]; float value; //value }ComponentType; typedef struct ListNodeT{ ComponentType Component; float voltage, power, current; struct ListNodeT *nextPtr; }ListNodeType; In a separate function I am trying to write to a a file: FILE *filePtr; char fileName[13] =

ArrayList vs LinkedList for both Random-Access & Additions-Removals

旧巷老猫 提交于 2019-12-24 07:03:09
问题 I am well versed with pros and cons of ArrayList and LinkedList. ArrayList is preferred for random access, when additions and removals are less, and vice versa. What if I need a data structure where I need to do both random access, and need to add & remove items from the list often? Which one to choose? 回答1: These data structures are API-compatible, just benchmark/profile your code with both. Another hint: with ArrayList assume you perform N lookups and N mutations. This totals to O(N) + O(N

Referring to “C - Help understanding how to write a function within a function (list_map)”

a 夏天 提交于 2019-12-24 06:58:18
问题 I have the same professor: I have read the forum: How to write a function within a function (list_map) It is very helpful understanding the concept of the function but I'm not sure if I am using it right... Here is my code.. Let me know if I am on the right track... Assume that I have an array of 10 linked lists in which the linked lists holds ints Now I would like to sort the list calling the list_map(); function So my main looks something like this: int x = 10; LLIST *mylist[x]; bzero

Print a simply linked list backwards with no recursion, in two passes at most, using constant extra memory, leaving it intact

戏子无情 提交于 2019-12-24 06:49:22
问题 You must print a simply linked list backwards: Without recursion With constant extra memory In linear time Leaving the list intact Added Later Two passes at most 回答1: Building on sharptooth's reply, you can combine the printing and second inversion in the same pass. Edit: The "list is left intact" from a single-threaded view because the post-condition equals the pre-condition. Edit 2: Not sure how I got the answer, but I'll take it since I've hit the rep cap for the day. I gave sharptooth a

Shuffling an linked list Java

时光毁灭记忆、已成空白 提交于 2019-12-24 06:47:21
问题 I'm having a very hard time using a pseudo code for the shuffling algorithm and turning it to a working java code. I'm attempting to shuffle a linked list. Overall the method takes the pointer of the head of the linked list and returns a pointer to the head of the same list randomly. I want to use a getLength and getItem method I've created. public static ListElement shuffle(ListElement head){ head = head.getLength(); ListElement head2= null; while( head == null) { int random = (int) Math