linked-list

Reading a data from a file and writing it to a linked list

六眼飞鱼酱① 提交于 2019-12-26 08:56:41
问题 #include<stdio.h> struct Node{ char *name; int time; int sec; int x; int y; struct Node *next; }; int main(){ FILE *file; int index; struct Node *data=malloc(sizeof(struct Node)); struct Node *tmp=data,*tmp2=data; int enter; file=fopen("data.txt","r"); if(file==NULL){ printf("File was not opened!\n"); return 0; } while(!feof(file)){ tmp=malloc(sizeof(struct Node)); fscanf(file,"%d",&index); fscanf(file,"%d",&tmp->time); fscanf(file,"%d",&tmp->sec); fscanf(file,"%d",&tmp->x); fscanf(file,"%d",

Reading a data from a file and writing it to a linked list

本秂侑毒 提交于 2019-12-26 08:53:26
问题 #include<stdio.h> struct Node{ char *name; int time; int sec; int x; int y; struct Node *next; }; int main(){ FILE *file; int index; struct Node *data=malloc(sizeof(struct Node)); struct Node *tmp=data,*tmp2=data; int enter; file=fopen("data.txt","r"); if(file==NULL){ printf("File was not opened!\n"); return 0; } while(!feof(file)){ tmp=malloc(sizeof(struct Node)); fscanf(file,"%d",&index); fscanf(file,"%d",&tmp->time); fscanf(file,"%d",&tmp->sec); fscanf(file,"%d",&tmp->x); fscanf(file,"%d",

I don't understand the implementation of inserting a new node in linked list

不羁的心 提交于 2019-12-25 18:56:39
问题 In linked list implementation, the insertion of a new node to the linked list is usually written like this: void push(struct node** head_ref, int new_data) /* 1. allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* 2. put in the data */ new_node->data = new_data; /* 3. Make next of new node as head */ new_node->next = (*head_ref); /* 4. move the head to point to the new node */ (*head_ref) = new_node; (I took this code from http://quiz.geeksforgeeks.org

Linked list text file loop

て烟熏妆下的殇ゞ 提交于 2019-12-25 18:43:36
问题 My program crashes under a certain instance/case, it complies and runs fine otherwise. We were given a text file formatted like so: 12 JackSprat 2 1 65000 13 HumptyDumpty 5 3 30000 17 BoPeep 2 3 30000 20 BoyBlue 3 2 58000 0 we are required to read from file and store into a struct using a linked list. so far my code looks like this: #include <stdio.h> #include <stdlib.h> #include <string.h> #define NAME_LENGTH 20 typedef struct employeeData { int EMP_ID; char* Name; int Dept; int Rank; double

C linked list why is my list head variable remaining null (new to C) [closed]

廉价感情. 提交于 2019-12-25 18:38:23
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . i received great help on my other questions in relation to this link list problem. My current problem is now that the head of the list remains null so i cannot actually link any nodes to it or print anything out to the console. The problem is the insert_node function, so when print is called the while loop doesn

C linked list why is my list head variable remaining null (new to C) [closed]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 18:38:15
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . i received great help on my other questions in relation to this link list problem. My current problem is now that the head of the list remains null so i cannot actually link any nodes to it or print anything out to the console. The problem is the insert_node function, so when print is called the while loop doesn

Insert for singly-linked list C

徘徊边缘 提交于 2019-12-25 18:23:20
问题 I am having trouble with my insert to the front of the linked list fucntion in C #define arrSIZE = 100; struct listNode { char data[arrSIZE]; struct listNode *nextPtr; }; typedef struct listNode ListNode; void insertHead(ListNode *sPtr, char value[arrSIZE]){ ListNode *newPtr = (ListNode *)malloc(sizeof(ListNode)); strncpy(newPtr->data, value, arrSIZE); if(sPtr ==NULL){ newPtr->nextPtr=NULL; sPtr = newPtr; }else{ newPtr->nextPtr=sPtr; sPtr =newPtr; } } 回答1: I can see why. -You're setting sPtr,

custom node class with java's linkedlist class

强颜欢笑 提交于 2019-12-25 18:23:09
问题 Been a while since I used java. I want to use Java's built in linkedlist class but I want a custom node class that has children and siblings. How can I write a custom node class that Java's linked list will use? Just a matter of creating the node class and declaring a linkedlist class but just create a new constructor that will override javas? Thanks! 回答1: LinkedList uses a private Node<E> class internally. If this is just an academic exercise, given that the source code is available you

I am trying to write a function that removes all the odd elements from a given linked-list and also returns

社会主义新天地 提交于 2019-12-25 18:21:53
问题 an adress of a new list of the odd elements removed. I find this task quite complicated, and I would be happy if you could help to repair or improve my code. Here is what I've done by far: typedef struct list{ int data; struct list* next; } List; List* removeOddValues(List** source) { List* curr= *source; List* prev; List* odd= NULL; while (curr) { if ((curr->data)%2==1) //odd// { insertNodeToEnd(&odd, curr->data); //creating a new list of those odd elements// prev->next = curr->next; } else

C++ Linked List node counting (need help)

我是研究僧i 提交于 2019-12-25 16:43:57
问题 I'm trying to create a program that gets string input from a text file, inserting the content into a list, word by word. I also have to calculate the numbers of the duplicates. My program works fine for the small input text file (1 line of string). But whenever I feed it with a bigger text file, it crashes. Any help will be great. Here is my code: #include <iostream> #include <fstream> #include <sstream> using namespace std; class Bag { private: struct BagNode { string dataValue; int