Inserting different strings read from file in a linked list - c

I'm writing a programm in c that reads words from a file, and adds the different from them in a linked list. However, i don't get the right result.I have problem finding if a word exists in the list. Any help could be appreciated.Thanks.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct list_el {
char val[30];
struct list_el * next;
};
typedef struct list_el item, item2;
void main() {
item * curr, * head, * curr2, * head2;
FILE *fp;
char words[30];
int i;
head = NULL;
head2 = NULL;
if ((fp=fopen("file.txt","r"))==NULL)
printf("cannot open file\n");
i=0;
while (fscanf(fp,"%s",&words)!=EOF) {
if (i!=0)
while(curr2) {
if (!strcmp(words,curr2->val)){
break;
}
curr2 = curr2->next ;
}
if (curr2==0 || i==0){
curr = (item *)malloc(sizeof(item));
curr2 = (item2 *)malloc(sizeof(item2));
strcpy(curr->val,words);
strcpy(curr2->val,words);
curr->next = head;
curr2->next = head2;
head = curr;
head2 = curr2;
}
i++;
}
while(curr) {
printf("%s\n", curr->val);
curr = curr->next ;
}
fclose(fp);
}

int main(void) {//void main() is invalid.
item *curr, *head = NULL;
FILE *fp;
char words[30];
if ((fp=fopen("file.txt","r"))==NULL){
printf("cannot open file\n");
return EXIT_FAILURE;//can't continue
}
while (fscanf(fp,"%29s", words) != EOF) {//remove &
curr = head;
while(curr) {//Search the current list
if (!strcmp(words, curr->val)){
break;
}
curr = curr->next;
}
if (curr == NULL){//not find words
item *node = malloc(sizeof(*node));
strcpy(node->val, words);
node->next = head;
head = node;
}
}
fclose(fp);
curr = head;
while(curr) {
printf("%s\n", curr->val);
curr = curr->next ;
}
}

Related

Inserting Element single linked List C

I try to insert an Element in an empty single Linked List and print that.
The code looks like this.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Node{
int val;
struct Node* next;
}ll;
void addElement(ll* List, int num){
ll* new = malloc(sizeof(ll));
if(new == NULL){
printf("NO MEMORY\n");
exit(0);
}
new->val = num;
new->next = NULL;
if(List == NULL){
List = new;
return;
}
ll* curr = List;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = new;
}
void printElements(ll* List){
ll* curr = List;
while(curr != NULL){
printf("%i\n", curr->val);
curr = curr->next;
}
}
*int main(){
ll* list = NULL;
addElement(list, 20);
addElement(list, 30);
addElement(list, 19);
printElements(list);
return 0;*
}
Does anybody see my mistake? Because it only works if i already have an Element in my List and nothing will be printed.
The function deals with a copy of the value of the pointer to the head node used as the function argument.
void addElement(ll* List, int num){
So this statement
List = new;
does not change the value of the original pointer. It changes the value of the copy of the original pointer.
The function can be defined the following way
int addElement( ll **List, int num )
{
ll *new_node = malloc( sizeof( ll ) );
int success = new_node != NULL;
if ( success )
{
new_node->val = num;
new_node->next = NULL;
while ( *List != NULL ) List = &( *List )->next;
*List = new_node;
}
return success;
}
And the function is called like
ll* list = NULL;
addElement( &list, 20);
addElement( &list, 30);
addElement( &list, 19);

Not reading number properly?

I'm trying to read from file line by line. It takes the first number of the line and the rest it connects it using a char of linked list. But when I run it, i get the connection as -38 (which is wrong) and it only prints it once and does not go through the rest of the line. but it reads first element perfectly.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next;
};
int main(void)
{
FILE * fp;
char * line = NULL;
char * storage;
size_t len = 0;
ssize_t read;
struct node *G[1000];
for (int i = 1; i < 1000; i++)
{
G[i]= malloc(sizeof(struct node));
G[i]->data = i;
G[i]->next = NULL;
}
fp = fopen("idk2.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
int vertexGettingConntected = line[0];
struct node* newItem;
printf("Retrieved line of length %zu :", read);
int i = 0;
while(line[i] != '\0'){
if ( line[i] == ' '){
i++;
}
else if (i == 0){
newItem = malloc(sizeof(struct node));
int itemStorage = line[0] - '0';
newItem->next = NULL;
newItem->data = itemStorage;
G[itemStorage] = newItem;
printf("This is first Number:%d\n", itemStorage);
}
else if (line[i] != ' '){
struct node* addingItem = newItem;
while(addingItem->next != NULL){
addingItem = addingItem->next;
}
int itemStorage = line[i] - '0';
struct node* newConnection = malloc(sizeof(struct node));
addingItem->next = newConnection;
newConnection->data = itemStorage;
newConnection->next = NULL;
printf("This is character:%c\n", line[i]);
printf("This is connection:%i\n", itemStorage);
}
i++;
}
}
fclose(fp);
if (line)
free(line);
exit(EXIT_SUCCESS);
for(int printer = 1; printer<20; printer++){
printf("%d\n",G[printer]->data);
}
}
EDIT:
Just wanted to include file im reading from:
1 3 4
2 4
3 1 4
4 2 1 3
I am not sure why you want to have an array of pointers to node which you all allocate memory for at the beginning of your program without knowing how many nodes will be needed. Then you allocate memory again while reading from the file.
Given the constraints of not being allowed to use functions, thats how i'd read the file and build a list:
#include <stdlib.h>
#include <stdio.h>
typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;
int main(void)
{
char const *filename = "test.txt";
FILE *input_file = fopen(filename, "r");
if (!input_file) {
fprintf(stderr, "Couldn't open \"%s\" for reading :(\n\n", filename);
return EXIT_FAILURE;
}
// read from file and build a list:
node_t *head = NULL;
node_t *tail = NULL;
int value;
int result = EXIT_SUCCESS;
while (fscanf(input_file, "%d", &value) == 1) {
node_t *new_node = calloc(1, sizeof *new_node);
if (!new_node) {
fputs("Not enough memory :(\n\n", stderr);
result = EXIT_FAILURE;
goto glean_up;
}
new_node->value = value;
if (!head) { // there is no head yet so new_node becomes the head
head = tail = new_node; // which is also the lists tail
continue;
}
tail->next = new_node;
tail = new_node;
}
// print the list:
for (node_t *current_node = head; current_node; current_node = current_node->next)
printf("%d ", current_node->value);
putchar('\n');
clean_up:
fclose(input_file);
for (node_t *current_node = head, *temp; current_node; current_node = temp) {
temp = current_node->next;
free(current_node);
}
return result;
}
Ideally you'd write functions to manage the list:
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;
typedef struct list_tag {
node_t *head;
node_t *tail;
} list_t;
void list_create(list_t *list)
{
list->head = list->tail = NULL;
}
bool list_push_back(list_t *list, int value)
{
node_t *new_node = calloc(1, sizeof *new_node);
if (!new_node)
return false;
new_node->value = value;
if (!list->head) {
list->head = list->tail = new_node;
return true;
}
list->tail->next = new_node;
list->tail = new_node;
return true;
}
void list_print(list_t *list)
{
for (node_t *current_node = list->head; current_node; current_node = current_node->next)
printf("%d ", current_node->value);
}
void list_free(list_t *list)
{
for (node_t *current_node = list->head, *temp; current_node; current_node = temp) {
temp = current_node->next;
free(current_node);
}
}
bool read_int(FILE *input_file, int *value)
{
return fscanf(input_file, "%d", value) == 1;
}
int main(void)
{
char const *filename = "test.txt";
FILE *input_file = fopen(filename, "r");
if (!input_file) {
fprintf(stderr, "Couldn't open \"%s\" for reading :(\n\n", filename);
return EXIT_FAILURE;
}
// read from file and build a list:
list_t list;
list_create(&list);
int value;
while (read_int(input_file, &value)) {
if (!list_push_back(&list, value)) {
fputs("Not enough memory :(\n\n", stderr);
// clean up:
fclose(input_file);
list_free(&list);
return EXIT_FAILURE;
}
}
// print the list:
list_print(&list);
putchar('\n');
// clean up:
fclose(input_file);
list_free(&list);
}
Output:
1 3 4 2 4 3 1 4 4 2 1 3
When you hit a space character you iterate i. At the end of your loop you iterate i.
So when you hit a space character you iterate i twice, skipping the number and landing on the next space character.
Which is why you get the first number but miss the rest.
edit: removed getline() comments due to feedback from #swordfish. Current implementation is not problematic.

Segmentation fault using argv[1]

I created a code that inserts and deletes nodes from a Linked List depending on what a .txt file instructs. I previously used scanf("%s",fname) to open the file but now I want to open it using the command line, specifically argv[1] == file name to open it and read from it. Now that I decided to use this I keep getting a segmentation fault. Any ideas why this is occurring? I just started taking a C course in my University and am brand new to it.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct node{
int val;
struct node *next;
} Node;
void count_node(Node *head){
Node *temp;
int i=0;
temp = head;
while(temp!=NULL){
i++;
temp=temp->next;
}
printf("number of nodes are %d \n ",i);
}
void displayList(Node *head){
Node *temp;
temp = head;
while(temp != NULL){
printf(" %d ",temp->val);
temp = temp->next;
}
}
Node * findBefore(Node *head,int value){
Node *curr = head;
Node *prev = NULL;
while(curr != NULL){
if(value <= curr->val){
return prev;
}
prev = curr;
curr = curr->next;
/*printf("a\n");
break;*/
}
return prev; // 1 2 4
//printf("b\n");
}
void add(Node *head,int newValue){ //return a Node 0 for some reason
ask TA
printf("check \n");
Node *newNode = malloc(sizeof(Node));
if(newNode == NULL){
printf("check \n");
return;
}
newNode->val = newValue;
//if list is empty:
if(head == NULL){
printf("check \n");
head = newNode;
}
//if list is not empty:
Node *prev = findBefore(head,newNode->val); //a -> b
if(prev == NULL){
printf("check \n");
newNode->next = head;
head = newNode;
return;
}
printf("check \n");
Node *temp = prev->next;
prev->next = newNode;
newNode->next = temp;
}
void delete(Node *head, int delVal){
if(head == NULL){
return;
}
Node *prev = NULL;
Node *curr = head;
if(delVal == curr->val && prev !=NULL ){ // 1 2
prev->next = curr->next;
free(curr);
return;
}
else if(delVal == curr->val && prev == NULL){ // 1
head = NULL;
free(curr);
return;
}
while(curr != NULL) {
while(curr != NULL && curr->val != delVal){
prev = curr;
curr = curr->next;
}
if (curr == NULL){
return;
}
prev->next = curr->next;
free(curr);
curr = prev->next;
}
}
int main(int argc,char *argv[]) {
FILE * fp;
char singleLine[150];
char command;
int val;
fp = fopen(argv[1],"r");
if(fp == NULL ){
exit(1);
}
Node *head = malloc(sizeof(Node));
if(head == NULL){ //malloc returns null is there is a memory leak
return 1;
}
while(fgets(singleLine,100,fp)){
//printf("%s\n",singleLine);
sscanf(singleLine,"%c %d",&command,&val);
//printf("command character is: %c\n" , command);
//printf("The value we have to add is: %d\n",val);
if(command == 'i'){
printf("Found i, and add %d\n",val);
add(head,val);
}
else if(command == 'd'){
delete(head,val);
printf("found d and delete %d\n",val);
}
}
free(head);
fclose(fp);
printf("\n");
count_node(head);
printf("The linked list is : \n ");
displayList(head);
return 0;
}
What is the command you are using to run the program? Especially in larger projects, when we need to deal with external input, it's good practice to check it's validity.
Maybe something like:
if (argc < 2) {
println("Invalid input: No arguments");
return 1;
}
char *filename = argv[1];
fp = fopen(filename,"r");
To see what may be the reason the call is failing. Try to add this short debug code before your call to argv[1]:
println("Number of arguments: %d", argc);
for (int i = 0, i < argc, i++) {
println("Argument %d: \"%s\"", i, argv[i]);
}

c linked list delete smallest element [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Lists seem really hard for me. I want to find the smallest element in a file: 1 5 8 6 4 8 6 48 9. It's 1 and I want to delete that 1. I can find the smallest element but can not delete it. I find the smallest element place but not the value. I tried copying deleting function from the web, however I cant understand it due to the fact that I'm really new to C. It writes an error that dereferencing to incomplete type. Please help. Post whole code because it should be more convenient to understand.
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedList {
int value;
struct linkedList *next;
} linkedList, head;
linkedList *readList(linkedList *head) {
FILE *dataFile;
dataFile = fopen("duom.txt", "r");
if (dataFile == NULL) {
printf("Nepasisekė atidaryti failo\n");
} else {
printf("Duomenų failą pavyko atidaryti\n");
}
while (!feof (dataFile))
if (head == NULL) {
head = malloc(sizeof(linkedList));
fscanf(dataFile, "%d", &head->value);
head->next = NULL;
} else {
struct linkedList *current = head;
struct linkedList *temp = malloc(sizeof(linkedList));
while (current->next != NULL) {
current = current->next;
}
fscanf(dataFile, "%d", &temp->value);
current->next = temp;
temp->next = NULL;
}
return head;
}
void search(linkedList *head, int *lowest) {
int a[100];
int i = 0;
int minimum;
int b = 0;
linkedList *current = head;
while (current != NULL) {
a[i] = current->value;
current = current->next;
i++;
}
b = i;
i = 0;
minimum = a[0];
while (b > 0) {
if (minimum > a[i]) {
minimum = a[i];
lowest = i;
}
i++;
b--;
}
}
void deleteNode(struct node **head_ref, int key) {
struct node* temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return;
prev->next = temp->next;
free(temp);
}
void printList(linkedList *head) {
linkedList *current = head;
while (current != NULL) {
printf("%d->", current->value);
current = current->next;
}
printf("NULL\n");
return;
}
int main() {
linkedList *A = NULL;
A = readList(A);
search(A);
head = head->next;
minimum = head->value;
headk->next = head->next;
free(head);
printList(A);
return 0;
}
like this
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct linkedList{
int value;
struct linkedList *next;
} linkedList, node;
linkedList *readList(void){
FILE *dataFile;
dataFile = fopen("duom.txt", "r");
if(dataFile == NULL) {
perror("file open");
return NULL;
}
int v;
node head = {0, NULL}, *curr = &head;
while (1 == fscanf(dataFile, "%d", &v)){
node *new_node = malloc(sizeof(node));
if(new_node == NULL){
perror("malloc");
break;
}
new_node->value = v;
new_node->next = NULL;
curr = curr->next = new_node;
}
fclose(dataFile);
return head.next;
}
int searchMin(linkedList *head){
if(head == NULL){
fprintf(stderr, "%s: The list MUST NOT be NULL.\n", __func__);
return INT_MIN;
}
int min = head->value;
node *p = head->next;
while(p){
if(p->value < min)
min = p->value;
p = p->next;
}
return min;
}
void deleteNode(node **head_ref, int key){
node *curr = *head_ref, *prev = NULL;
while (curr != NULL && curr->value != key){
prev = curr;
curr = curr->next;
}
if (curr == NULL) return;//not found
if(prev)
prev->next = curr->next;
else
*head_ref = curr->next;
free(curr);
}
void printList(linkedList *head){
node *current = head;
while (current != NULL) {
printf("%d->", current->value);
current = current -> next;
}
puts("NULL");
}
void freeList(linkedList *list){
while(list){
node *temp = list;
list = list->next;
free(temp);
}
}
int main(void){
linkedList *A = readList();
int min = searchMin(A);
printList(A);
deleteNode(&A, min);
printList(A);
freeList(A);
return 0;
}
Please try if this program can help you.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct node {
int data;
struct node *next;
};
void push(struct node **head_ref, int new_data) {
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(struct node **head_ref, int key) {
struct node *temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
void printList(struct node *node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
void min(struct node **q) {
struct node *r;
int min = INT_MAX;;
r = *q;
while (r != NULL) {
if (r->data < min) {
min = r->data;
}
r = r->next;
}
printf("The min is %d", min);
deleteNode(q, min);
printf("\n");
}
int main() {
struct node *head = NULL;
FILE *file = fopen("duom.txt", "r");
int i = 0;
fscanf(file, "%d", &i);
while (!feof(file)) {
push(&head, i);
fscanf(file, "%d", &i);
}
fclose(file);
puts("Created Linked List: ");
printList(head);
min(&head);
puts("\nLinked List after Deletion of minimum: ");
printList(head);
return 0;
}
file duom.txt
1 5 8 6 4 8 6 48 9
Test
./a.out
Created Linked List:
9 48 6 8 4 6 8 5 1 The min is 1
Linked List after Deletion of minimum:
9 48 6 8 4 6 8 5 ⏎

Program quits out after running?

The program runs fine and displays the data I want but then crashes. I don't think it is a memory leak from the linked list because I've tried many ways to free it and none have worked.
#include "Buildings.h"
void displayBuilding(struct node *x){
printf("Building Name: %s\n\tFloors: %d\n\tValue: %d\n\n", x->payload.name, x->payload.floors, x->payload.value);
if(x->last!=1){
displayBuilding(x->next);
}
}
void insert(struct node *x, char str[50]){
srand(t);
int f = (rand() % 6)+1;
int v = ((rand() % 20)+1)*f;
//printf("Floors: %d and Value: %d\n", f, v);
strcpy(x->payload.name, str);
x->payload.floors = f;
x->payload.value = v;
srand(time(NULL));
t+=f+v*(rand()%1000);
}
FILE* openData(FILE *f){
f = fopen("Buildings-Data.txt", "r");
if(f==NULL){
printf("No File!");
return 0;
}
return f;
}
void freeList(struct node* head)
{
struct node* tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}
int main(){
FILE *f1;
struct node *head;
struct node *curr;
head = malloc(sizeof(struct node));
curr = malloc(sizeof(struct node));
curr = head;
int i;
for(i=0;i<9;i++){
curr->next = malloc(sizeof(struct node));
curr = curr->next;
}
f1 = openData(f1);
curr = head;
int readNum;
char trash;
fscanf(f1, "%d", &readNum);
fscanf(f1, "%c", &trash);
for(i=0;i<readNum;i++){
char str[50];
fscanf(f1, "%s",str);
insert(curr, str);
curr->last = 0;
if(readNum-i==1){
curr->last = 1;
}else{
curr = curr->next;
}
}
fscanf(f1, "%c", &trash);
fclose(f1);
curr = head;
printf("\n");
displayBuilding(curr);
curr = head;
freeList(head);
return 0;
}
My header file:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
struct building{
char name[50];
int floors;
int value;
};
typedef struct building place;
struct node{
place payload;
struct node *next;
int last;
};
int t = 500;
The problem is that when you create the linked list, you never set next to NULL in the last node of the list. You need to do that after the loop:
for(i=0;i<9;i++){
curr->next = malloc(sizeof(struct node));
curr = curr->next;
}
curr->next = NULL;
Because of this, the while (head != NULL) loop in freeList() never stops, and eventually tries to free the uninitialized pointer in the last node of the list.

Resources