How to insert multiple data in linked list - c

This program asks for two data ( a name and an id), and then stores them in a linked list and then it should output all the data one by one.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Test
{
char name[16];
int id;
};
typedef struct Node {
struct Test structure;
struct Node * next;
}TNode;
typedef TNodo* Node;
void NewDatainNode(struct Test p, Node *pp)
{
TNode *temp;
temp = malloc(sizeof(struct Node));
temp->structure = p;
temp->next = NULL;
*pp = temp;
}
void ReadData(struct Test * p)
{
printf("\nName:");
scanf(" %s", p->name);
printf("\nID:");
scanf(" %d", &p->id);
}
void ViewNodes(Node node)
{
while(node != NULL)
{
printf("%s %d\n",node->structure.name, node->structure.id);
node = node->next;
}
}
int Menu()
{
int c;
printf("\n\tM E N U ***\n"
"1 - New\n"
"2 - Print\n"
"0 - Exit\n"
"\n>> ");
scanf(" %d", &c);
return c;
}
int main()
{
int c;
struct Prova test;
struct Node * list = NULL;
do {
c = Menu();
switch (c)
{
case 1: ReadData(&test);
NewDatainNode(test, &list);break;
case 2: ViewNodes(lista); break;
default: c = 0;
}
} while (c != 0);
return 0;
}
The problem is that it outputs only the last data that has been inserted, what should i do to make it output all the data retrieved in function NuovaPrenotazione()?.
UPDATE: Now the function NewDatainNodes() looks like this:
void NewDatainNode(struct Test p, Node *pp)
{
TNode *temp;
temp = malloc(sizeof(struct Node));
temp->structure = p;
temp->next = *pp;
*pp = temp;
}
Thanks to Sandeep for the correction, the function works even without a return value,the problem was temp->structure = NULL.

This question is exactly duplicate copy of a question posted yesterday and marked as duplicate. But, here is your working code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
struct Prova
{
char nome[16];
int id;
};
typedef struct Node {
struct Prova struttura;
struct Node * next;
}TNodo;
typedef TNodo* Nodo;
Nodo NuovaPrenotazione(struct Prova p, Nodo pp)
{
Nodo temp;
temp = (Nodo)malloc(sizeof(struct Node));
temp->struttura = p;
temp->next = pp;
pp = temp;
return pp;
}
void LeggiPrenotazione(struct Prova * p)
{
printf("\nNome Cliente:");
scanf(" %s", p->nome);
printf("\nID:");
scanf(" %d", &p->id);
}
void Visualizza(struct Prova p)
{
printf("%s %d\n", p.nome, p.id);
}
void VisualizzaPrenotazione(Nodo nodo)
{
while(nodo != NULL)
{
Visualizza(nodo->struttura);
nodo = nodo->next;
}
}
int Menu()
{
int scelta;
printf("*** M E N U ***\n"
"1 - New\n"
"2 - Print\n"
"0 - Esci\n"
"Scelta --> ");
scanf(" %d", &scelta);
return scelta;
}
int main()
{
int scelta;
struct Prova test;
Nodo lista = NULL;
do {
scelta = Menu();
switch (scelta)
{
case 1: LeggiPrenotazione(&test);
lista = NuovaPrenotazione(test, lista);
break;
case 2: VisualizzaPrenotazione(lista); break;
default: scelta = 0;
}
} while (scelta != 0);
return 0;
}
Update: Here is the output

Related

Segmentation fault error in Linked-List program

I'm facing the segmentation fault error in the following dynamic linked list implementation; GDB shows the issue is on the node = node->next line in the "l_print" function of the linked-list ("LIST_CL.H" file). Anyone can help? I even tried "drawing debug" on paper but still I'm not getting the issue here.
Note: I report the whole code for both the insert and the print, in case it's useful. Therefore LIST.C file include the "switch case 6" to insert the element ("l_add" function in "LIST_CL.H" file) and the "switch case 1" to print the list ("l_print" function in "LIST_CL.H" file)
Note: The issue only happen when the list has one or more items. There's no errors when the list is empty ("list_cl" struct has NULL for both head and tail nodes)
LIST.C:
#include <stdio.h>
#include <string.h> //for strcpy
#include "cl_list.h"
#include "list_cl.h"
#define STRING_SIZE 25
int main(){
list_cl class = L_EMPTYLIST_CL;
puts("Select command:");
puts("0. Exit.");
puts("1. Insert node.");
puts("6. Print all nodes in the linked list.");
int k = 0;
scanf("%d", &k);
while(k != 0){
switch(k){
case 1:
char cf[17] = "";
char first_name[STRING_SIZE] = "";
char last_name[STRING_SIZE] = "";
getchar();
puts("Insert name:");
fgets(first_name, sizeof(first_name), stdin);
puts("Insert surname:");
fgets(last_name, sizeof(last_name), stdin);
puts("Insert fiscal code:");
fgets(cf, sizeof(cf), stdin);
client cliente;
strcpy(cliente.cf, cf);
cliente.first_name = first_name;
cliente.last_name = last_name;
class = l_add_cl(class, cliente);
puts("Node inserted.");
break;
case 6:
l_print(class);
break;
default:
break;
}
scanf("%d", &k);
}
}
LIST_CL.H:
list_cl l_add_cl(list_cl l, client p){
l_node node;
node.id = 1;
node.person = p;
node.next = NULL;
if(l.head == NULL){
//List is empty
l.head = &node;
l.tail = &node;
} else {
l.tail -> next = &node;
l.tail = &node;
}
return l;
}
void l_print(list_cl l){
l_node *node = NULL;
node = l.head;
while(node != NULL){
//client *cliente = &node->person;
//printf("ID Elemento: %d | Name: %s Surname: %s Fiscal Code: %s", node->id, cliente->first_name, cliente->last_name, cliente->cf);
node = node->next; // SEGMENTATION FAULT ERROR HERE!
}
}
CL_LIST.H:
#include "client.h"
typedef struct _node {
unsigned int id;
client person;
struct _node *next;
} l_node;
typedef struct {
l_node *head;
l_node *tail;
} list_cl;
#define L_EMPTYLIST_CL {NULL,NULL}
CLIENT.H:
typedef struct {
char cf[17];
char *first_name;
char *last_name;
} client;
Fixed code:
LIST.C:
#include <stdio.h>
#include <string.h> //for strcpy
#include "cl_list.h"
#include "list_cl.h"
#define STRING_SIZE 25
int main(){
list_cl class = L_EMPTYLIST_CL;
puts("Seleziona un comando:");
puts("0. Exit.");
puts("1. Insert one node.");
puts("6. Print all nodes.");
int k = 0;
scanf("%d", &k);
while(k != 0){
switch(k){
case 1:
char cf[17] = "";
//char first_name[STRING_SIZE] = ""; THIS CAUSE DATA INCONSISTENCY
//char last_name[STRING_SIZE] = ""; THIS CAUSE DATA INCONSISTENCY
char *first_name = malloc(sizeof(char)*STRING_SIZE);
char *last_name = malloc(sizeof(char)*STRING_SIZE);
getchar();
puts("Insert name:");
//fgets(first_name, sizeof(first_name), stdin); ISSUE WITH SIZE OF FIRST_NAME
fgets(first_name, sizeof(char)*STRING_SIZE, stdin);
puts("Insert surname:");
//fgets(last_name, sizeof(last_name), stdin); ISSUE WITH SIZE OF LAST_NAME
fgets(last_name, sizeof(char)*STRING_SIZE, stdin);
puts("IInsert fiscal code:");
fgets(cf, sizeof(cf), stdin);
client *cliente = malloc(sizeof(client));
strcpy(cliente->cf, cf);
cliente->first_name = first_name;
cliente->last_name = last_name;
class = l_add_cl(class, *cliente);
puts("Element inserted.");
break;
case 6:
l_print(class);
break;
default:
break;
}
puts("Select command:");
scanf("%d", &k);
}
}
LIST_CL.H
list_cl l_add_cl(list_cl l, client p){
l_node *node = malloc(sizeof(struct _node));
node->id = 1;
node->person = p;
node->next = NULL;
if(l.head == NULL){
//Empty list
l.head = node;
l.tail = node;
} else {
l.tail -> next = node;
l.tail = node;
l.tail -> next = NULL;
}
return l;
}
void l_print(list_cl l){
l_node *node = NULL;
node = l.head;
while(node != NULL){
client *cliente = &node->person;
printf("ID Element: %d | Name: %s Surname: %s Fiscal code: %s", node->id, cliente->first_name, cliente->last_name, cliente->cf);
node = node->next;
}
}
CL_LIST.H
#include "client.h"
typedef struct _node {
unsigned int id;
client person;
struct _node *next;
} l_node;
typedef struct {
l_node *head;
l_node *tail;
} list_cl;
#define L_EMPTYLIST_CL {NULL,NULL}
CLIENT.H
typedef struct {
char cf[17];
char *first_name;
char *last_name;
} client;

Sorted List not printed as expected (C)

I tried to make a simpler example of my problem, I hope the code doesn't look too weird. I am trying to make a sorted list. If the input is for ex wrt, er, ff, fr, when I try to print the list it says fr fr fr fr and I can't understand why.
Unfortunately I can't post the original code, the struct tit doesn't make much sense in this example but I don't think that's the problem.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct nodo *link;
typedef struct lista *TLIST;
typedef struct tit *TITOLO;
TLIST LISTinit();
link NEW(TITOLO val, link next);
void LISTinsert(TLIST l, TITOLO val);
void ListPrint(TLIST list);
TLIST caricaFile();
struct tit {
char nome[20];
};
struct lista {
link head;
int n;
};
struct nodo {
TITOLO val;
link next;
};
int main() {
TLIST lista;
lista = caricaFile();
ListPrint(lista);
return 0;
}
TLIST LISTinit() {
TLIST list = malloc(sizeof *list);
list->head = NULL;
list->n = 0;
return list;
}
link NEW(TITOLO val, link next) {
link x = malloc(sizeof *x);
x->next = next;
x->val = val;
return x;
}
void LISTinsert(TLIST l, TITOLO val) {
link x, p;
l->n++;
if (l->head == NULL || (strcmp(val->nome, l->head->val->nome) < 0)) {
l->head = NEW(val, l->head);
return;
}
for (x = l->head->next, p = l->head;
x != NULL && (strcmp(val->nome, x->val->nome) > 0);
p = x, x = x->next);
p->next = NEW(val, x);
}
void ListPrint(TLIST list) {
link x;
for (x = list->head; x != NULL; x = x->next)
printf("%s ", x->val->nome);
printf("\n");
}
TLIST caricaFile() {
TLIST list;
list = LISTinit();
TITOLO titolo = malloc(sizeof *titolo);
int i;
for (i = 0; i < 4; i++) {
printf("Insert title: ");
scanf("%s", titolo->nome);
LISTinsert(list, titolo);
}
return list;
}
in caricaFile you save all the read titles in the same location (titolo->nome) so only the last is finaly memorized because you save all the time the same pointer (titolo) in list.
You need to duplicate the val when you save it in the list, rather than to just do x->val = val; in NEW, for instance :
link NEW(TITOLO val, link next) {
link x = malloc(sizeof *x);
x->next = next;
TITOLO copy = malloc(sizeof (struct tit));
strcpy(copy->nome, val->nome);
x->val = copy;
return x;
}
Execution with the new definition of NEW :
Insert title: aze
Insert title: qsd
Insert title: wxc
Insert title: iop
aze iop qsd wxc
It is a very bad idea to make typedef being pointer like TITOLO is because this put the reader in error thinking it is a value rather than a pointer

Command terminated by signal 11

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
char data;
int p;
struct node *ptr;
};
struct node *start=NULL;
struct node *insert()
{
struct node *new_node,*z;
int num;
char s;
printf("\nEnter -1 to stop.");
printf("\nEnter the characters and their priorities: ");
scanf("\n%c %d",&s,&num);
while(num!=-1)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=s;
new_node->p=num;
if(start==NULL)
{
start=new_node;
z=start;
new_node->ptr=NULL;
}
else
{
z->ptr=new_node;
new_node->ptr=NULL;
z=z->ptr;
}
scanf("%c %d",&s,&num);
}
return start;
}
struct node *display()
{
struct node *z;
z=start;
printf("\nDisplaying elements:\n");
while(z!=NULL)
{
printf("\t%c [Priority=%d]\n",z->data,z->p);
z=z->ptr;
}
return start;
}
struct node *sortit()
{
struct node *z,*q;
int t;
char x;
z=start;
while(z->ptr!=NULL)
{
q=z->ptr;
while(q!=NULL)
{
if(z->p>q->p)
{
t=z->p;
x=z->data;
z->p=q->p;
z->data=q->data;
q->p=t;
q->data=x;
}
q=q->ptr;
}
z=z->ptr;
}
return start;
}
struct node *new_insert()
{
struct node *y,*z;
int n;
char s;
y=(struct node *)malloc(sizeof(struct node));
printf("\nEnter character and priority for new node:");
scanf("%c %d",&s,&n);
printf("%c",s);
y->data=s;
y->p=n;
printf("%c",y->data);
printf("%d",y->p);
if(n<start->p)
{
y->ptr=start;
start=y;
printf("%d",y->p);
}
else
{printf("\nff");
z=start;
while(z->ptr->p<=n&&z->ptr!=NULL)
z=z->ptr;
y->ptr=z->ptr;
z->ptr=y;
}
return start;
}
int main()
{
insert();
display();
sortit();
display();
new_insert();
display();
return 0;
}
In this C code, I have tried to implement priority queues.
Everything works perfectly fine except the new_insert() function. The print statements after y->p=n; in new_insert() function print 0.
Hence, the function doesn't work properly. Also, in display() function the print statement prints the [Priority] twice.
Hence, I am not able to add an external node in my priority queue.
Well, you code is not far from working I think.
There are some problems that can be corrected:
the scanf usage is problematic
%c selector must be use with caution, use " %c %d" to prevent end of line problems
return value must be checked
in function insert, z may not be initialized (if insert() is called when start is not NULL)
You don't need to cast malloc returned value.
I tested the following code with this input:
a 2
b 3
c 1
d -1
e 0
And I got this result:
Enter -1 to stop.
Enter the characters and their priorities:
Displaying elements:
a [Priority=2]
b [Priority=3]
c [Priority=1]
Displaying elements:
c [Priority=1]
a [Priority=2]
b [Priority=3]
Enter character and priority for new node:
s: 'e' n: '0'
e00
Displaying elements:
e [Priority=0]
c [Priority=1]
a [Priority=2]
b [Priority=3]
Seems to work isn't? You can test it online
And here's the corrected code:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
char data;
int p;
struct node *ptr;
};
struct node *start=NULL;
struct node *insert()
{
/* initialize z in case of start is not null */
struct node *new_node,*z = start;
int num, ok;
char s;
printf("\nEnter -1 to stop.");
printf("\nEnter the characters and their priorities: ");
/* read the scanf return value */
ok = scanf(" %c %d",&s,&num);
/* and assert two elements have been read*/
while(ok == 2 && num!=-1)
{
new_node=malloc(sizeof(struct node));
new_node->data=s;
new_node->p=num;
if(start==NULL)
{
start=new_node;
z=start;
new_node->ptr=NULL;
}
else
{
z->ptr=new_node;
new_node->ptr=NULL;
z=z->ptr;
}
ok = scanf(" %c %d",&s,&num);
}
return start;
}
struct node *display()
{
struct node *z;
z=start;
printf("\nDisplaying elements:\n");
while(z!=NULL)
{
printf("\t%c [Priority=%d]\n",z->data,z->p);
z=z->ptr;
}
return start;
}
struct node *sortit()
{
struct node *z,*q;
int t;
char x;
z=start;
while(z->ptr!=NULL)
{
q=z->ptr;
while(q!=NULL)
{
if(z->p>q->p)
{
t=z->p;
x=z->data;
z->p=q->p;
z->data=q->data;
q->p=t;
q->data=x;
}
q=q->ptr;
}
z=z->ptr;
}
return start;
}
struct node *new_insert()
{
struct node *y,*z;
int n, ok;
char s;
printf("\nEnter character and priority for new node:");
ok = scanf(" %c %d",&s,&n);
if (2 == ok)
{
/* alloc y after having check that user gave usage stuff */
y = malloc(sizeof(struct node));
y->data=s;
y->p=n;
printf("%c",y->data);
printf("%d",y->p);
if(n<start->p)
{
y->ptr=start;
start=y;
printf("%d",y->p);
}
else
{
printf("\nff");
z=start;
while(z->ptr->p<=n&&z->ptr!=NULL)
z=z->ptr;
y->ptr=z->ptr;
z->ptr=y;
}
}
return start;
}
int main()
{
insert();
display();
sortit();
display();
new_insert();
display();
return 0;
}

Pointer problem while using Linked list in C

I'm working on a c programming code and most of the code is working fine except the polynomial Multiplication part. It has a runtime error. please help me in removing this runtime error from Polynomial Multiplication, I couldn't find the error i think it's in the third for loop.
Thanks...
It will be great if you solve the error
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX 17
typedef struct node
{
int coeff;
struct node *next;
}node;
node * init();
void read(node *h1);
void print(node *h1);
node * add(node *h1,node *h2);
node * multiply(node *h1, node *h2);
void main()
{
node *h1=NULL,*h2=NULL,*h3=NULL;
int option;
do
{
printf("\n1 : create 1’st polynomial");
printf("\n2 : create 2’nd polynomial");
printf("\n3 : Add polynomials");
printf("\n4 : Multiply polynomials");
printf("\n5 : Quit");
printf("\nEnter your choice :");
scanf("%d",&option);
switch(option)
{
case 1:
h1=init();
read(h1);
break;
case 2:
h2=init();
read(h2);
break;
case 3:
h3=add(h1,h2);
printf("\n1’st polynomial -> ");
print(h1);
printf("\n2’nd polynomial -> ");
print(h2);
printf("\n Sum = ");
print(h3);
break;
case 4:
h3=multiply(h1,h2);
printf("\n1’st polynomial -> ");
print(h1);
printf("\n2’nd polynomial -> ");
print(h2);
printf("\n Product = ");
print(h3);
break;
}
}while(option!=5);
}
void read(node *h)
{
int n,i,j,power,coeff;
node *p;
p=init();
printf("\n Enter number of terms :");
scanf("%d",&n);
/* read n terms */
for (i=0;i<n;i++)
{
printf("\nenter a term(power coeff.)");
scanf("%d%d",&power,&coeff);
for(p=h,j=0;j<power;j++)
p=p->next;
p->coeff=coeff;
}
}
void print(node *p)
{
int i;
for(i=0;p!=NULL;i++,p=p->next)
if(p->coeff!=0)
printf("%dX^%d ",p->coeff,i);
}
node * add(node *h1, node *h2)
{
node *h3,*p;
h3=init();
p=h3;
while(h1!=NULL)
{
h3->coeff=h1->coeff+h2->coeff;
h1=h1->next;
h2=h2->next;
h3=h3->next;
}
return(p);
}
node * multiply(node *h1, node *h2)
{
node *h3,*p,*q,*r;
int i,j,k,coeff,power;
h3=init();
for(p=h1,i=0;p!=NULL;p=p->next,i++)
for(q=h2,j=0;q!=NULL;q=q->next,j++)
{
coeff=p->coeff * q->coeff;
power=i+j;
for(r=h3,k=0;k<power;k++)
r=r->next;
r->coeff=r->coeff+coeff;
}
return(h3);
}
node * init()
{
int i;
node *h=NULL,*p;
for(i=0;i<MAX;i++)
{
p=(node*)malloc(sizeof(node));
p->next=h;
p->coeff=0;
h=p;
}
return(h);
}
There is at least a probleme here in the multiply function:
...
for (r = h3, k = 0; k < power; k++)
r = r->next;
r->coeff = r->coeff + coeff;
...
At some point r becomes NULL and at the next step when you dereference r with r->coeff (r being NULLnow) your program will result un undefined behaviour (a segfault on most platforms).
There were several problems:
Memory was leaked: make sure you can point out a free for every malloc.
read was creating a new list, but leaking it and never updating the data in h1 or h2.
add didn't check for NULL h2.
The result of addition/multiplication was unnecessarily retained.
There was an arbitrary maximum of 17 nodes - the whole point of a linked list is so that there's no need to maintain such arbitrary limits.
Variables had scopes that were too large. This is 2011: one can declare variables near to point of use and not at the beginning of the block.
The use of scanf was non-idiomatic and inapplicable to interactive line-oriented input. For such input, read complete lines and parse them, not the stdin stream. In the stdin stream, a newline is a field separator and you don't want that, I think.
There weren't any asserts: program defensively, assert what you think should be true.
This works and should be correct and free of undefined behavior under all conditions.
// https://github.com/KubaO/stackoverflown/tree/master/questions/c-linked-debug-52867729
#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
int power; // primary key for sorting
int coeff;
struct Node *next;
} Node;
Node *new_node(Node *prev, Node *next, int power);
Node *get_node(Node **head, Node *hint, int power);
void delete_nodes(Node *head);
Node *read(void);
void print(const Node *head);
Node *add(const Node *head1, const Node *head2);
Node *multiply(const Node *head1, const Node *head2);
void print_nodes(const Node *n1, const Node *n2, const char *extra_label,
const Node *extra);
const char *arity_suffix(int n);
bool parse_line(int max_length, const char *fmt, int count, ...);
static void *guarded_malloc(size_t size) {
void *result = malloc(size);
if (!result) {
fprintf(stdout, "Memory allocation has failed: exiting.");
abort();
}
return result;
}
int main() {
Node *h1 = NULL, *h2 = NULL;
int option;
do {
printf("\n1 : Create 1'st polynomial");
printf("\n2 : Create 2'nd polynomial");
printf("\n3 : Print polynomials");
printf("\n4 : Add polynomials");
printf("\n5 : Multiply polynomials");
printf("\n6 : Quit");
printf("\nEnter your choice: ");
if (!parse_line(10, "%d", 1, &option)) continue;
switch (option) {
case 1:
delete_nodes(h1);
h1 = read();
break;
case 2:
delete_nodes(h2);
h2 = read();
break;
case 3:
print_nodes(h1, h2, NULL, NULL);
break;
case 4: {
Node *sum = add(h1, h2);
print_nodes(h1, h2, "Sum", sum);
delete_nodes(sum);
break;
}
case 5: {
Node *prod = multiply(h1, h2);
print_nodes(h1, h2, "Product", prod);
delete_nodes(prod);
break;
}
}
} while (option != 6);
delete_nodes(h1);
delete_nodes(h2);
}
Node *read() {
int n;
printf("\n Enter number of terms: ");
if (!parse_line(10, "%d", 1, &n)) return NULL;
/* read n terms */
Node *head = NULL;
for (int i = 0; i < n;) {
int power, coeff;
printf("\nEnter the %d%s term (power coeff): ", i + 1, arity_suffix(i + 1));
if (!parse_line(80, "%d%d", 2, &power, &coeff) || !coeff) continue;
Node *p = get_node(&head, NULL, power);
if (!p->coeff) i++; // count only new terms
p->coeff = coeff;
}
return head;
}
void print(const Node *p) {
for (; p; p = p->next) printf("%dX^%d ", p->coeff, p->power);
}
void add_to(Node **sum, const Node *h) {
Node *r = NULL;
for (; h; h = h->next) {
r = get_node(sum, r, h->power);
r->coeff += h->coeff;
}
}
Node *add(const Node *h1, const Node *h2) {
Node *sum = NULL;
add_to(&sum, h1);
add_to(&sum, h2);
return sum;
}
Node *multiply(const Node *h1, const Node *h2) {
Node *prod = NULL;
for (const Node *p = h1; p; p = p->next) {
Node *r = NULL;
for (const Node *q = h2; q; q = q->next) {
int power = p->power + q->power;
r = get_node(&prod, r, power);
r->coeff += p->coeff * q->coeff;
}
}
return prod;
}
Node *new_node(Node *prev, Node *next, int power) {
assert(!prev || prev->power < power);
assert(!next || next->power > power);
Node *node = guarded_malloc(sizeof(Node));
node->power = power;
node->coeff = 0;
node->next = next;
if (prev) prev->next = node;
return node;
}
void delete_nodes(Node *head) {
while (head) {
Node *p = head;
head = head->next;
free(p);
}
}
static bool list_contains(Node *head, Node *elt) {
for (; head; head = head->next)
if (head == elt) return true;
return false;
}
Node *get_node(Node **head, Node *hint, int power) {
Node *node = hint;
Node *next = hint ? hint->next : head ? *head : NULL;
assert(!hint || !*head || list_contains(*head, hint));
assert(!hint || hint->power <= power);
assert(!node || !next || node->power < next->power);
while (next && next->power <= power) {
node = next;
next = next->next;
}
if (!node || node->power != power) {
assert(!node || node->power < power);
Node *n = new_node(node, next, power);
if (!node) *head = n;
node = n;
}
return node;
}
void print_nodes(const Node *h1, const Node *h2, const char *extra_label,
const Node *extra) {
printf("\n1'st polynomial -> ");
print(h1);
printf("\n2'nd polynomial -> ");
print(h2);
if (extra_label) {
printf("\n %s = ", extra_label);
print(extra);
}
printf("\n");
}
const char *arity_suffix(int n) {
if (n == 0) return "st";
if (n == 1) return "nd";
return "rd";
}
bool parse_line(int max_length, const char *fmt, int count, ...) {
bool result = false;
int const buf_size = max_length + 2; // include \n and zero termination
char *const buf = guarded_malloc((size_t)buf_size);
char *const str = fgets(buf, buf_size, stdin);
if (str) {
size_t n = strlen(str);
if (str[n - 1] == '\n') { // we must have read a whole line
str[n - 1] = '\0'; // remove the newline
va_list ap;
va_start(ap, count);
int rc = vsscanf(buf, fmt, ap);
va_end(ap);
result = rc == count;
}
}
free(buf);
return result;
}
I think you should use memcopy in read method
void read(node *h)
{
int n,i,j,power,coeff;
node *p;
p=init();
printf("\n Enter number of terms :");
scanf("%d",&n);
/* read n terms */
for (i=0;i<n;i++)
{
printf("\nenter a term(power coeff.)");
scanf("%d%d",&power,&coeff);
for(p=h,j=0;j<power;j++)
p=p->next;
p->coeff=coeff;
}
memcpy (h, p, sizeof(node))
}

Save structs in a file

I'm doing a program to save a list of contacts in a file whit structs. I've tried a lot of things but when I try go read the file to the program, it doesn't read anything.
This is my program without opening files and saving to files:
#include <stdio.h>
#include <stdlib.h>
struct agenda {
int idContacte;
char name[50];
struct agenda *nextContacte;
};
struct agenda *pAgenda;
struct agenda *pFirst = NULL;
struct agenda *pIndex;
void insert();
void show();
int main()
{
//Menu
int opc;
while(1){
printf("1.Insert Contact.\n");
printf("2.Show Contacts.\n");
printf("3.Exit\n");
scanf("%d", &opc);
switch(opc){
case 1:
insert();
break;
case 2:
show();
break;
case 3:
return 0;
}
}
}
void insert(){
pAgenda = (struct agenda *)malloc(sizeof(struct agenda));
printf("Insert ID: ");
scanf("%d", &pAgenda->idContacte);
printf("Insert the name: ");
scanf("%s", pAgenda->name);
printf("\n");
if (pFirst==NULL || pAgenda->idContacte < pFirst->idContacte)
{
pAgenda->nextContacte=pFirst;
pFirst=pAgenda;
}
else if (pAgenda->idContacte > pFirst->idContacte){
pIndex=pFirst;
while(pIndex->nextContacte && pIndex->nextContacte->idContacte < pAgenda->idContacte)
{
pIndex = pIndex->nextContacte;
}
pAgenda->nextContacte = pIndex->nextContacte;
pIndex->nextContacte = pAgenda;
}
}
void show(){
pIndex = pFirst;
while(pIndex && pIndex->idContacte <= 100) {
printf("\nID: %d", pIndex->idContacte);
printf("\nNAME: %s", pIndex->name);
printf("\n\n");
pIndex = pIndex->nextContacte;
}
}
Can you help me how can I get contact at start of the program from a file, and then when insert a contact, rewrite the file and insert all the contacts again in the file?
When you end your program you should do the following
int save_list(struct agenda *head) {
FILE *save = fopen("file.name", "wb");
if(!save) return -1;
while(head) {
fwrite(head, sizeof *head - sizeof head, 1, save);
head = head->nextContacte;
}
fclose(save);
/* Somebody would free list memory after this function execution */
return 0;
}
At the start of your program you should do the following
struct agenda *restore_list() {
FILE *restore= fopen("file.name", "rb");
struct agenda *head = NULL;
struct agenda *cur = head;
struct agenda temp;
if(!restore) return head;
while( fwrite(&temp, sizeof temp - sizeof head, 1, save) == 1) {
struct agenda *node = malloc( sizeof(struct agenda) );
if(NULL == node) {
/* Handle out of memory error here, free list */
return NULL;
}
*node = temp;
node->nextContacte = NULL;
if(head) {
cur->nextContacte = node;
cur = node;
} else {
/* First node */
head = cur = node;
}
}
fclose(restore);
return head;
}

Resources