
Originariamente inviata da
Eduadie
Così proprio non funzionerà mai.
Tu hai definito in modo giusto la struct ma devi definirgli una variabile o un'array.
codice:
struct Book book;
struct Book book[10]
In questo modo tu potrai riempire nel modo giusto l'elemento creato.
codice:
book.title;
book[0].title;
Non avevo visto la struct all'interno...è conveniente crearti un'altra struct Date e inserire un campo all'interno della struct Book che punti all'elemento della struct Date.
Purtroppo seguo delle direttive del professore che ha esplicitamente richiesto una struct dentro una struct.
Visto che mi hai aiutato mi sa che approfitto totalmente:
dovrei creare una lista ordinata che fa delle determinate funzioni che mi ha chiesto il professore. Ho quasi finito ma ogni funzione che ho creato lavora con gli int invece dovrebbe lavorare con le struct del tipo che ho definito sopra. Il passo finale praticamente! Non sono capace a convertire il codice tanto meno a far si che da input possa riempire e utilizzare N struct organizzate in quel modo. Se mi dai una mano ti sono molto riconoscente:
codice:
/* Doubly Linked List implementation */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//#include "lista.h"
struct book {
int data;
struct book* next;
struct book* prev;
};
struct book* head; // global variable - pointer to head book.
struct book* Getnewbook(int x);
void InsertAtHead(int x);
void InsertAtTail(int x);
void Print();
void PrintbyID(int x);
void deletefrombeginning();
void riempiStruct();
//void deletefromend();
bool searchbyID(int x);
bool deletebyID(int x);
void riempiStruct();
struct Book { char title[64];
char writer[32];
/*enum genre {fantasy, novel, action};*/
struct date {short month;
short day;
short year};
short inLibrary;
short outLibrary;
short id;}
;
printf("Inserimento titolo: ")
scanf("%s" &Book.title);
printf("Il titolo è %s", Book.title);
//Creates a new book and returns pointer to it.
struct book* Getnewbook(int x) {
struct book* newbook = (struct book*)malloc(sizeof(struct book));
newbook->data = x;
newbook->prev = NULL;
newbook->next = NULL;
return newbook;
}
//Inserts a book at head of doubly linked list
void InsertAtHead(int x) {
struct book* newbook = Getnewbook(x);
if(head == NULL) {
head = newbook;
return;
}
head->prev = newbook;
newbook->next = head;
head = newbook;
}
//Inserts a book at tail of Doubly linked list
void InsertAtTail(int x) {
struct book* temp = head;
struct book* newbook = Getnewbook(x);
if(head == NULL) {
head = newbook;
return;
}
while(temp->next != NULL) temp = temp->next; // Go To last book
temp->next = newbook;
newbook->prev = temp;
}
//Prints all the elements in linked list in forward traversal order
void Print() {
struct book* temp = head;
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
void PrintbyID(int x) {
struct book* temp = head;
while(temp != NULL) {
if(temp->data==x)
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
//Cancellazione in testa
void deletefrombeginning(){
struct book *temp, *tail;
temp= (struct book*)malloc(sizeof(struct book)) ;
if(head==NULL)
return;
temp=head;
if(head==tail) /*if there's just one element then*/
head=tail=NULL; //clean this element
else{
(temp->next)->prev=NULL;
head=temp->next;
}
free(temp);
}
//camcellazione in coda
void deletefromend(){
struct book *temp,*tail;
temp= (struct book*)malloc(sizeof(struct book)) ;
temp=tail;
if(head==NULL)
return;
if(head==tail) /*if there's just one element then*/
head=tail=NULL; //clean this element
else{
(temp->prev)->next=NULL;
tail=temp->prev;
}
free(temp);
}
//ricerca tramite ID
bool searchbyID(int x){
//temp = temp->next;
struct book *temp;
temp=(struct book*)malloc(sizeof(struct book));
temp=head;
if(head==NULL)
return 0;
// Iterate through the linked list
while(temp!= NULL) {
if(temp->data == x){
return 1;
free(temp);
}
temp = temp->next;
}
return 0;
free(temp);
}
//cancella tramite ID
bool deletebyID(int x){
//temp = temp->next;
struct book *temp,*tail;
temp=(struct book*)malloc(sizeof(struct book));
temp=head;
if(head==NULL)
return 0;
// Iterate through the linked list
while(temp!= NULL) {
if((temp->data == x) && (head==tail)){ //if there's just one element then
head=tail=NULL; //clean this element
return 1;
free(temp);
}
if((temp->data == x) && (head!=tail)){
(temp->prev)->next=(temp->next);
return 1;
free(temp);
}
temp = temp->next;
}
return 0;
free(temp);
}
/******************************************* main ********************************************************/
int main() {
riempiStruct();
printf("Inserimento in testa: ");InsertAtHead(2); Print();
printf("Inserimento in testa: ");InsertAtTail(2); Print();
printf("Inserimento in testa: ");InsertAtHead(2); Print();
printf("Inserimento in coda: ");InsertAtTail(4); Print();
printf("Inserimento in testa: ");InsertAtHead(6); Print();
printf("Inserimento in coda: ");InsertAtTail(8); Print();
printf("Eliminazione in testa: ");deletefrombeginning();Print();
//printf("Eliminazione in coda: ");deletefromend();Print();
printf("Valore di Restituzione della ricerca per ID: %d\n", searchbyID(4));
printf("Inserimento in coda: ");InsertAtTail(3); Print();
printf("Eliminazione per ID: %d -- Lista dopo eliminazione: \n", deletebyID(4)); Print();
printf("Output per ID : ");PrintbyID(2);*/
return 0;
}