I have a project that I'm working on for a Systems Programming course. I'm building off of my professor's code. (Please don't mind her lack of labelling, etc. - I'm gonna try to clean this up as best as I can.)
Does anybody know why her linked list code is printing backwards?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char name[15];
char title[15];
int year;
struct Node *next;
struct Node *prev;
};
typedef struct Node *Box;
Box print_list(Box pointer);
Box insert_node(FILE *inputp);
int main() {
Box head = NULL, temp;
FILE *inputp, *outputp;
int i;
inputp = fopen("input.txt", "r");
outputp = fopen("output.txt", "w");
head = insert_node(inputp);
for (i = 0; i < 4; i++) {
temp = insert_node(inputp);
temp->next = head;
head = temp;
}
print_list(head);
return 0;
}
Box print_list(Box pointer) {
Box here = pointer;
while (here != NULL) {
printf("%s, %s, %d \n", here->name, here->title, here->year);
here = here->next;
}
return pointer;
}
Box insert_node(FILE *inputp) {
Box temp = NULL;
temp = (Box)malloc(sizeof(struct Node));
fscanf(inputp, "%s", &temp->name);
fscanf(inputp, "%s", &temp->title);
fscanf(inputp, " %d", &temp->year);
temp->next = NULL;
temp->prev = NULL;
return temp;
}
This program's purpose is to read a .txt file "playlist" of songs and create a linked list out of them. The input is:
Rachmaninov Concerto_No_2 1999
Mozart Symphony_No_41 2000
Vivaldi The_Seasons 2003
Beethoven Symphony_No_5 1994
Bach Toccatas 2005
While the program outputs:
Bach, Toccatas, 2005
Beethoven, Symphony_No_5, 1994
Vivaldi, The_Seasons, 2003
Mozart, Symphony_No_41, 2000
Rachmaninov, Concerto_No_2, 1999
(I also don't know why she included an output file in the code, all of the output is in the console, not stored in a file. Ignore that.)
The list prints in reverse order because you insert each new node at the beginning of the list. You should use a tail pointer to keep track of the end of the list.
Also note these remarks:
both the next and the prev links should be updated.
hiding pointers behind typedefs as in typedef struct Node *Box; is considered bad practice because it is confusing and error prone.
insert_node is a confusing name for a function that merely allocates a new node from file data.
insert_node should test if fscanf() succeeded at reading the data
fscanf(inputp, "%s", &temp->name); has undefined behavior if the name of the composer exceeds 14 bytes. The same applies to the title. The maximum number of characters to store into the destination arrays before the null terminator should be specified as %14s and these arrays should be defined with a larger length.
main should check if a node was successfully allocated and initialized from file data. Instead of hardcoding the number of nodes, one should iterate as long as nodes can be read from the file.
Here is a modified version:
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char name[40];
char title[40];
int year;
struct Node *next;
struct Node *prev;
};
void print_list(const Node *pointer);
Node *read_node(FILE *inputp);
int main() {
Node *head = NULL;
Node *tail = NULL;
Node *node;
FILE *inputp, *outputp;
int i;
inputp = fopen("input.txt", "r");
if (!inputp) {
fprintf(stderr, "cannot open input.txt: %s\n", strerror(errno));
return 1;
}
outputp = fopen("output.txt", "w");
if (!outputp) {
fprintf(stderr, "cannot open output.txt: %s\n", strerror(errno));
return 1;
}
while ((node = read_node(inputp)) != NULL) {
if (!head) {
head = tail = node;
} else {
node->prev = tail;
tail = tail->next = node;
}
}
print_list(head);
// should free node list
return 0;
}
void print_list(const Node *pointer) {
while (pointer != NULL) {
printf("%s, %s, %d\n", pointer->name, pointer->title, pointer->year);
pointer = pointer->next;
}
}
Node *read_node(FILE *inputp) {
Node *temp = malloc(sizeof(*temp));
if (temp != NULL
&& fscanf(inputp, "%39s%39s%d", &temp->name, &temp->title, &temp->year) == 3) {
temp->next = NULL;
temp->prev = NULL;
return temp;
} else {
free(temp);
return NULL;
}
}
Related
I want to make a program that would load data from txt file and put it into a linked list then print that linked list out. It seems to work but it also prints out some junk too. I'm quite new to linked lists so it's probably some obvious mistake. I would appreciate if someone told how to fix it, thanks in advance.
That's what inside my txt file:
FORD FIESTA 2010 1.6
OPEL ASTRA 2005 1.4
And that's what my program prints out:
FORD FIESTA 2010 1.6
OPEL ASTRA 2005 1.4
iŁ ;C:\Windows;C:\Windows\System32\ 1251983754 132.41
And here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Cars
{
char brand[30], model[30];
int year;
float engine;
struct Cars *next;
} Cars_t;
void load_cars(Cars_t *head, char file_name[30])
{
Cars_t *temp = head;
FILE *baza = fopen(file_name, "r");
if(baza!=NULL)
{
while(fscanf(baza, "%s%s%d%f", temp->brand, temp->model, &(temp->year), &(temp->engine)) == 4)
{
temp->next=malloc(sizeof(Cars_t));
temp=temp->next;
}
temp->next=NULL;
}
fclose(baza);
}
void print_list(Cars_t *head)
{
Cars_t *current = head;
while (current != NULL)
{
printf("%s\t%s\t%d\t%.2f\n", current->brand, current->model, current->year, current->engine);
current = current->next;
}
}
int main()
{
Cars_t *head = malloc(sizeof(Cars_t));
load_cars(head, "baza.txt");
print_list(head);
return 0;
}
You read 2 structs and alloc 3 times! First in main for head, then twice when you sucessfuly read from file. So your last alloc make no sense. I also add some security checks for your code but you only need to pay atention to lines comented with // <---- Special atention here!
Btw this example is not the best aproach i just did a fast fix for you to understand where is the problem.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Cars Cars_t, *pCars_t;
struct Cars
{
char brand[30], model[30];
int year;
float engine;
pCars_t next;
};
void load_cars(Cars_t *head, char file_name[30])
{
Cars_t *temp = head;
Cars_t *previous = NULL; // <---- Special atention here!
FILE *baza = fopen(file_name, "r");
if(baza!=NULL)
{
while(fscanf(baza, "%s%s%d%f", temp->brand, temp->model, &(temp->year), &(temp->engine)) == 4)
{
temp->next=malloc(sizeof(Cars_t));
if(temp->next == NULL)
{
fprintf(stderr, "Allocation error!\n");
exit(1);
}
previous = temp; // <---- Special atention here!
temp=temp->next;
}
temp->next=NULL;
free(previous->next); // <---- Special atention here!
previous->next=NULL; // <---- Special atention here!
}
else
{
fprintf(stderr, "File problem!");
return;
}
fclose(baza);
}
void print_list(Cars_t *head)
{
Cars_t *current = head;
while (current != NULL)
{
printf("%s\t%s\t%d\t%.2f\n", current->brand, current->model, current->year, current->engine);
current = current->next;
}
}
int main()
{
pCars_t head = malloc(sizeof(Cars_t));
if(head == NULL)
{
fprintf(stderr, "Allocation error!\n");
return 1;
}
load_cars(head, "baza.txt");
print_list(head);
return 0;
}
I'm working on two functions. The first one loads a list from a file into the program (at the start) and the second one saves the list's data to the file. I've tried to write the two functions but I'm having a hard time(I'm not really good at programming). Here are the structs I'm using I'm also adding the define of index and the creation of the list:
#define index 30
typedef struct dataR* data;
struct dataR{
int age;
char name[index];
};
typedef struct nodeR* node;
struct nodeR{
data a;
node next;
};
typedef struct listR* list;
struct listR{
node head, tail, curr;
int size;
};
list list_create(){
list List=malloc(sizeof(struct listR));
assert(List);
List->head=NULL;
List->tail=NULL;
List->curr=NULL;
List->size=0;
return List;
}
Here is the function to load the file data into the list (at the start of the program). Obviously it is wrong but I don't know how to load the data to each node since the list is empty:
list load(char *filename, list List)
{
FILE *fd=fopen("filename","r");
fscanf(fd, "%d",&(List->head->a->age));
fscanf(fd, "%s",&(List->head->a->name[index-1]));
fclose(fd);
fd=NULL;
}
And here is the function that save all of the list's data at the end of the program :
void save(char *filename, list List)
{
FILE *fd=fopen("filename.dat","w");
if (fd==NULL)
{
printf("File does not exist");
return;
}
fwrite(List->head->a, sizeof(struct dataR),1,fd);
node tmp=List->head->next;
while(tmp->next!=NULL)
{
fwrite(tmp->next->a, sizeof(struct dataR),1,fd);
tmp=tmp->next;
}
fclose(fd);
fd=NULL;
}
The data in the file should look kind like this:
35 Nick
29 Jim
19 Helen
Well, ofcourse the functions don't do that as they are. So I need some help to improve them. Any tips (on files) and help is much appreciated.I'm sorry for the long post. Thank you for your time.
To fix like this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define S_(v) #v
#define S(v) S_(v) //stringify
#define MAX_NAME_LENGTH 30 //index is bad name
typedef struct dataR* data;
struct dataR{
int age;
char name[MAX_NAME_LENGTH + 1];
};
typedef struct nodeR* node;
struct nodeR{
data a;
node next;
};
typedef struct listR* list;
struct listR{
node head, tail, curr;//curr unused?
int size;
};
list list_create(void){
list List = malloc(sizeof(*List));
assert(List);
List->curr = List->tail = List->head = NULL;
List->size=0;
return List;
}
void addList(list List, data new_data){
node new_node = malloc(sizeof(*new_node));
new_node->a = new_data;
new_node->next = NULL;
if(List->head == NULL)
List->head = List->tail = new_node;
else
List->tail = List->tail->next = new_node;
++List->size;
}
list load(const char *filename, list List){
FILE *fd = fopen(filename,"r");//"filename"--> filename, LOL
if(!fd){
fprintf(stderr, "%s can't open in load.\n", filename);
perror("fopen");
return NULL;
}
int age;
while(EOF != fscanf(fd, "%d", &age)){// or 1 == fscanf(fd, "%d", &age)){
data new_data = malloc(sizeof(*new_data));
new_data->age = age;
fscanf(fd, "%" S(MAX_NAME_LENGTH) "s", new_data->name);
addList(List, new_data);
}
fclose(fd);
//fd=NULL;//meaningless
return List;//need return value;
}
void save(const char *filename, list List){
FILE *fd=fopen(filename, "w");
if(!fd){
fprintf(stderr, "%s can't open in save.\n", filename);
perror("fopen");
return ;
}
//fwrite(List->head->a, sizeof(struct dataR),1,fd);//fwrite doesn't match load
node tmp = List->head;
while(tmp){
fprintf(fd, "%d %s\n", tmp->a->age, tmp->a->name);
tmp = tmp->next;
}
fclose(fd);
}
int main(void){
list List = list_create();
load("list.txt", List);
printf("List hold %d data.\n", List->size);
data addData = malloc(sizeof(*addData));
addData->age = 73;
strcpy(addData->name, "Ken");
addList(List, addData);
save("list.txt", List);
//deallocate
return 0;
}
I have an assignment that requires me to edit a C program provided to me so that it can read from a text document where each line looks like:
int%char%char%Double%int%int%int
with any number of lines and an empty line at the end of the file.
This text file is passed to this program:
#include <stdlib.h>
#include <stdio.h>
struct node{
int element;
struct node * next;
};
// node structure
struct node * head = NULL; // head node
void add_node(int num) {
if (head == NULL){
head = (struct node *) malloc(sizeof(struct node));
head->element = num;
}
else{
struct node * p = head;
while(p->next != NULL)
p = p->next;
p->next = (struct node *) malloc(sizeof(struct node));
p->next->element = num;
}
}
void print_list() {
struct node * p = head;
while(p != NULL){
printf("%d ", p->element);
p = p->next;
}
}
void free_list() {
struct node * p = head;
while(head != NULL){
p = head->next;
free(head);
head = p;
}
}
int main(int argc, char const *argv[]) {
int n, i;
for (i = 0; i < 10; i++) {
scanf("%d", &n);
add_node(n);
}
print_list();
free_list();
return 0;
}
I need to edit this program to include the 7 fields from the file (ID, Category, Detail, Amount, Year, Month, Day) in the struct node. Then have it read from the text file (File_name.txt for now) add the fields without the % separator between them in the struct node, and then print them out in order as such (RecordID: (ID) Category: (category) Amount: $(amount) Date: (Month)-(Day)-(Year) Detail: (detail)) and free all pointers before the program is terminated. I don't expect you all to do the assignment for me, its just that I have no idea how C programming works and I need to do this so if anyone can help point me in the right direction on how to go about this it would be much appreciated.
From what I see.
You'd be having to Create a struct of your own with specified variables from the file.
Read every line and intelligently parse them... the function strtok should do be able to do this in C.
Extract variable to store in created struct using a function which should not be too hard to write
And the rest you can or should be able to take care of
This might have not been as useful as you hope would buh you can try to make me understand so I can help do better
This should get you started on the right path:
int main(int argc, char const *argv[]) {
char filename[100];
printf("Enter the name of the file: ");
/*stdin is a FILE pointer to standard input */
fgets(filename, 100, stdin);
/*Now you open the file for reading "r" means read mode*/
FILE *fp = fopen(filename, "r");
/*Basic error checking*/
if (!fp) {
perror("File opening failed");
return EXIT_FAILURE; /*This is macro defined in stdlib.h*/
}
int id, year, month, day;
char category, detail;
double amt;
/* Here the fun begins. Use fscanf*/
while(fscanf(fp, "ID(%d)%%Category(%c)%%Detail(%c)%%Amount(%lf)%%Year(%d)%%Month(%d)%%Day(%d)",
&id, &category, &detail, &amt, &year, &month, &day) == 7) {
/* Do what ever you want with those values - create a node or something*/
}
/* Finally close the file */
fclose(fp);
return 0;
}
Remember that each time through the loop, the variables change.
I am trying basic creation of linked list using C. I have written the following code which is working up until first node but fails eventually on second one. I think the issue is where I am trying to display the node values in list separated by arrow(->). I think my logic is right but please correct me. Thanks in advance
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int number;
struct node *next;
};
typedef struct node NODE;
NODE *node1, *node2, *start, *save;
int main()
{
node1 = (NODE *)malloc(sizeof(NODE));
int i = 0;
start = NULL;
for(i = 0; i < 3; i++)
{
int inf;
printf("Enter node value:");
scanf("%d", &inf);
node1->number = inf;
node1->next = NULL;
if(start == NULL)
{
start = node1;
save = node1;
}
else
{
// save=start;
// start=node1;
// node1->next=save;
node1->next = start;
start = node1;
}
while(node1 != NULL)
{
printf("%d ->",node1->number);
node1 = node1->next;
}
}
return 0;
}
The issues are
How you're allocating your nodes for insertion (i.e. save for one, you're not).
How they're placed in the list once you fix the above.
Don't cast malloc in C programs (read here for why).
Fail to check the success of your scanf invoke.
Fail to check the success of your malloc invoke
Before you get discouraged, things you did correctly:
Did not mask a node pointer in a typedef
Properly included a MCVE for review
Prospected the things you may be doing wrong.
A very simple example of iterating three values into a linked list would look something like this:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int number;
struct node *next;
};
typedef struct node NODE;
int main()
{
NODE *head = NULL, *p;
int i = 0;
for(i = 0; i < 3; i++)
{
int inf;
printf("Enter node value:");
if (scanf("%d", &inf) == 1)
{
p = malloc(sizeof *p);
if (p != NULL)
{
p->number = inf;
p->next = head;
head = p;
}
else
{
perror("Failed to allocate new node");
return EXIT_FAILURE;
}
}
else
{
// failed to read data. break
break;
}
// report current linked list
printf("%d", p->number);
for (p=p->next; p; p = p->next)
printf(" -> %d", p->number);
fputc('\n', stdout);
}
// cleanup the linked list
while (head)
{
p = head;
head = head->next;
free(p);
}
head = NULL;
return 0;
}
Input
The values 1 2 3 are input upon being prompted:
Output
Enter node value:1
1
Enter node value:2
2 -> 1
Enter node value:3
3 -> 2 -> 1
Best of luck.
You should use malloc() inside for loop.
Since it is outside, same memory is being used.
As said by Vamsi, you should use malloc to put the nodes on the heap. You also generally shouldn't cast the output of malloc, it isn't needed. And then you could play around with making a doubly-linked list, where you also have a prev pointer inside your struct.
I'm trying to implement a linked list abstraction, however I am running into problems. Once I create the linked list and add elements to it. When I print the list it only prints the first element in it in an infinite loop fashion, meaning that either the first element is linked to itself or the print function is incorrect. However, I can't find the problem, could someone help?
The following is the list abstraction:
typedef struct _friend {
char *firstname;
char *lastname;
char birthdate[9];
} friend;
typedef struct _node {
friend *value;
struct _node *next;
} node;
typedef struct _linkedlist {
node *head;
} linkedlist;
The program must follow this abstraction, as it is part of something bigger.
The following are the functions that should print the list and add a node to the beginning of the list:
/* addHead
*
* This function takes two parameters - a linked list and a friend.
* This creates a node for the linked list and connects the friend to the
* node. Then it adds the node to the head of the linked list.
*/
void addHead(linkedlist *llist, friend *f)
{
// create a node and put the friend in it
node *n = (node *)malloc(sizeof(node));
n->value = f;
n->next = NULL;
// if the list is empty
if (llist == NULL)
{
// this link is the entire list
llist->head = n;
printf("adding friend to null list\n");
}
// if the list is not empty
else
{
// make the new link's next pointer point to
// the first link in the list
n->next = llist->head;
printf("adding %s to head\n", n->value->firstname);
// make the head pointer point to the new link
llist->head = n;
}
}
/*
* printList
*
* This steps down through each of the nodes in a linked list and
* prints out the information stored in the friend to which the node points.
* Instead of automatically printing to the screen, it prints to the
* file pointer passed in. If the programmer wants to print to the screen,
* he/she will pass in stdout.
*/
void printList(linkedlist *llist,FILE *fp)
{
node *n;
friend *f;
// for each node, print out the friend attached to it
for(n = llist->head; n != NULL ; n = llist->head->next)
{
// assign f to the friend of the right node
f = n->value;
// print the friend out
fprintf(fp,"%s %s: %s\n",
f->firstname, f->lastname, f->birthdate);
}
}
Thank You
The for loop in printList isn't quite right:
for(n = llist->head; n != NULL ; n = llist->head->next)
This should read:
for(n = llist->head; n != NULL ; n = n->next)
Otherwise from the second iteration onwards, n gets set to the same value every single time.
The following isn't related to the problem you're having, but I thought I'd mention it anyway. In the following code:
if (llist == NULL)
{
// this link is the entire list
llist->head = n;
printf("adding friend to null list\n");
}
if llist == NULL, the llist->head = n will segfault.
With the current signature of addHead(), there's not a lot you can do if llist is NULL (other than printing an error message and bailing out).
If instead you meant to check whether llist->head is NULL, you don't need to do that since the else block already handles that correctly.
Try:
void printList(linkedlist *llist,FILE *fp)
{
node *n;
friend *f;
// for each node, print out the friend attached to it
for(n = llist->head; n != NULL ; n = n->next)
{
// assign f to the friend of the right node
f = n->value;
// print the friend out
fprintf(fp,"%s %s: %s\n",
f->firstname, f->lastname, f->birthdate);
}
}
I have done the following to your program:
slightly modified the friend structure. Declared firstname and lastname as arrays for convenience.
Wrote a main() which calls other functions
error checking in addHead()
added create_friend() function which creates friend struct
added freeList() to release the memory which was malloc()'ed
corrected looping error in your print function
So here it goes..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _friend {
char firstname[10];
char lastname[10];
char birthdate[9];
} friend;
typedef struct _node {
friend *value;
struct _node *next;
} node;
typedef struct _linkedlist {
node *head;
} linkedlist;
void addHead(linkedlist *llist, friend *f)
{
node *n = NULL;
if (( n = (node *)malloc(sizeof(node))) == NULL) {
printf("unable to allocate memory \n");
exit(1);
}
n->value = f;
n->next = NULL;
if (llist == NULL) {
llist->head = n;
printf("adding friend to null list\n");
} else {
n->next = llist->head;
printf("adding %s to head\n", n->value->firstname);
llist->head = n;
}
return;
}
void printList(linkedlist *llist)
{
node *n;
friend *f;
if (llist->head == NULL) {
printf("Empty list \n");
return;
}
for(n = llist->head; n != NULL ; n = n->next) {
f = n->value;
printf("%s %s %d \n", f->firstname, f->lastname, f->birthdate);
}
return;
}
friend * create_friend(char *fn, char *ln, char *dob)
{
friend *fp = NULL;
if ((fp = malloc(sizeof(friend))) == NULL) {
printf("unable to allocate memory \n");
exit(1);
}
strcpy(fp->firstname, fn);
strcpy(fp->lastname, ln);
strcpy(fp->birthdate, dob);
return fp;
}
void freeList(linkedlist *llist)
{
node *cur = llist->head;
node *prev = cur;
friend *f;
while (cur != NULL) {
prev = cur;
cur = cur->next;
f = prev->value;
printf("freeing .. %s %s %d \n", f->firstname, f->lastname, f->birthdate);
free(prev->value);
free(prev);
}
return;
}
int main(void)
{
linkedlist ll;
friend *f;
ll.head = NULL;
f = create_friend("firstname1", "lastname1", "12345678");
addHead(&ll, f);
f = create_friend("firstname2", "lastname2", "12345678");
addHead(&ll, f);
f = create_friend("firstname3", "lastname3", "12345678");
addHead(&ll, f);
printList(&ll);
freeList(&ll);
ll.head = NULL;
printList(&ll);
return 0;
}
Hope this helps!
Should be n = n ->next otherwise you're just getting the next of the head every time.