linked-list

Read word from file into simple linked list

孤人 提交于 2019-12-11 00:57:13
问题 I need to write a program to read from a file, then save the words into a linked list for further use. I decided to read the text character by character using fgetc, then save all into the list each time a newline ( '\n' ) or space ( ' ' ) is detected, indicating one word. Sorry I'm a newbie in file pointers, this is what I've gotten so far: struct list { //global char string[30]; struct list *next; }; int main(void) { FILE *filePtr; char file[] = "text.txt"; char tempStr[30]; list *curr,

Segmentation fault in a function to reverse a singly linked list recursivley

怎甘沉沦 提交于 2019-12-11 00:49:43
问题 I am implementing a function to recursively reverse a linked-list, but getting seg-fault. typedef struct _node { int data; struct _node *next; } Node, *NodeP; NodeP recursiveReverseList(NodeP first){ if(first == NULL) return NULL; if(first->next == NULL) return first; NodeP rest = recursiveReverseList(first->next); rest->next = first; first->next = NULL; return first; } Can you please help? P.S. The iterative version is working fine though. Its not homework. Just practicing C. Thank you all :

using malloc for block of structs

可紊 提交于 2019-12-11 00:36:14
问题 I am trying to allocate a block of memory, and store a list of structures without using multiple mallocs for each... this is just a generic example, I don't have the original code I was working with earlier, but this is the general idea, but my problem was that I was getting heap corruption when other parts of my code executed after the InitPoints() function call. I don't know what part of my code is illegal, but I suspect it is in the for loop of the InitPoints() function. I am trying to use

Inserting Node in a Sorted linked list

风格不统一 提交于 2019-12-11 00:12:54
问题 The following code ensures that elements are inserted in a linked list in a sorted manner. After understanding the logic behind this i decided to test it on my own. However when i wrote my version of the code it as follows. public class SortedList { private Node first; public SortedList() { first = null; } public boolean isEmpty() { return first == null; } public void insert(int j) { Node newNode = new Node(j); Node previous = null; Node current = first; while (current != null && j > current

Retrieve only one specific data in a LinkedList (the data field holds several data)

天涯浪子 提交于 2019-12-10 23:34:33
问题 I implemented the LinkedList class from scratch and they have the following methods: getHead(), getCount(), addFront(), deleteFront() I have a Student class with constructor(String studentAdmNo, String studentName, String gender, String diploma, int year, String status), get set method, toString() and the Node contains method getData(), setData(), getLink(), setLink(), toString() I added the Student object into the LinkedList ( and so this LinkedList data field holds several data ; which is

linked list program

送分小仙女□ 提交于 2019-12-10 23:21:56
问题 I am expecting the below linked list program to print 1 but its not.can anyone figure out why? #include<stdio.h> #include <stdlib.h> #include<conio.h> struct node { int data; struct node * link; }; typedef struct node NODE; void display(NODE *); void add(NODE *,int ); int main() { NODE *head=NULL; add(head,1); display(head); printf("\n"); getch(); return 0; } void display(NODE *pt) { while(pt!=NULL) { printf("element is"); printf("%d",pt->data); pt=pt->link; } } void add(NODE *q,int num) {

Delete elements within a Linked List

守給你的承諾、 提交于 2019-12-10 22:27:24
问题 Looking for help again as my professor seems to do an awful job of explaining things (assumes that we know way too much about java programming, when in fact, it's the first java class most of us are taking). Not looking for someone to write the code for me, but rather someone who can let me know if I'm on the right track and guide me in the right direction. I really want to learn this stuff, not be spoon-fed it, but the professor is making it very hard to do so, so I turn here for help. The

C++ lists and pointers

蹲街弑〆低调 提交于 2019-12-10 21:34:41
问题 I am working on homework and wanted to know what this is actually defined as: list < NAME > * m_ofList Where name comes from a struct like so: typedef struct name { int age; int height; } NAME; I want to know what it is so I know how to insert to it or access it: push_back , insert , etc . So I understand this now, but I am stuck because of some type of memory access: it produces a segmentation fault and I have been unable to figure this out. Where do I need to initialize my new list? it

Merging 2 linked lists and appending to the end of linked lists c++

穿精又带淫゛_ 提交于 2019-12-10 20:33:51
问题 I don't have much so far but I am trying to get the hang of using linked lists. Struct: struct Node { int value; Node *next; }; How can I add a node to the end of the list? I am just trying to take in a pointer for the head of a list and an int value to add in as a new node. When I try running what I have currently I get an exception. void addNode(Node* head, int x) { Node* temp = new Node; temp->data = x; temp->next = NULL; if(!head) { head = temp; return; } else { Node* last = head; while

Deleting first and last element of a linked list in C

可紊 提交于 2019-12-10 19:28:27
问题 struct person { int age; char name[100]; struct person *next; }; void delfirst(struct person **p)// For deleting the beginning { struct person *tmp,*m; m = (*p); tmp = (*p)->next; free(m); return; } void delend(struct person **p)// For deleting the end { struct person *tmp,*m; tmp=*p; while(tmp->next!=NULL) { tmp=tmp->next; } m->next=tmp; free(tmp); m->next = NULL; return; } I'm looking for two separate functions to delete the first and last elements of a linked list. Here is what I tried.