我要实现以上功能:
struct _book
{
int id;
char name[20];
char Author[20];
int quantity;
float Price;
int rack_no;
};
typedef struct _book book;
typedef struct _node
{
book* data;
struct _node* next;
} Node;
Node* book_list=NULL;
void createPassword(void);
int logIn(void);
void sellBook(void);
void addBook(void);
void changePassword(void);
void searchBook(void);
void printZeroQuantity(void);
void printBooks(void);
void logOut(void);
void updateQuantity(void);
void entry(void);
void deleteBook(void);
void updateQuantity(void);
嘿,我是一个新的“程序员”,才两个月,我有一个关于 C 中的结构和链表的问题。
我想构建一个应用程序来管理图书馆数据库。
此外,我想接收作为参数获取的实现函数 void
并返回 void
例如这个函数:addBook(void)
,这意味着所有的结构在整个程序中都是全局的。
我的问题是如何从函数中读取链表的头部而不是将指针发送到 link_structures
的顶部,例如:book make_book(book *booklist ,int 计数)
.
您认为构建将创建的半函数是否可取
Node* new_node(int data)
还是没有必要?
book *Addbook(book *book_list,int *counter)
{
book Newbook;
int size, num, size1, size2, flag, check, i, size3;
char *tempfirst, *templast, *tempphone;
int *temprackno, *tempid;
double *tempprice;
(*counter)++;
book_list = (book*)realloc(book_list, (sizeof(book)*(*counter)));
book_list[*counter-1] = Getbook(book_list, ((*counter-1)));
printf("\nA book was added seccesfully");
return(book_list);
}
book Getbook(book *book_list, int count)
{
book NewBook;
int size, num, size1, size2, flag, check, i, size3, counte;
char *tempfirst, *Author, *tempphone;
int *temprackno = 0, *tempid = 0;
double *tempprice;
tempfirst = (char*)malloc(M*(sizeof(char)));
Author = (char*)malloc(M*(sizeof(char)));
tempphone = (char*)malloc(M*(sizeof(char)));
while((tempfirst==NULL) ||(Author==NULL) || (!tempid) || (!tempprice) )
{
tempfirst = (char*)malloc(M*(sizeof(char)));
Author = (char*)malloc(M*(sizeof(char)));
tempid = (int*)malloc(M*(sizeof(int)));
tempprice = (double*)malloc(M*(sizeof(double)));
}
最后一个问题:这行有什么问题?程序运行时掉线,新节点没有输入值。
scanf("%d", &(new_node->data->id));
{
Node* new_node = (Node*) malloc(sizeof(Node));
printf("\nPlease enter the book id:");
scanf("%d", &(new_node->data->id));
printf("\nPlease enter the book name:");
scanf("%s", &(new_node->data->Author));
printf("\nPlease enter the book quantity:");
scanf("%d", &(new_node->data->quantity));
printf("\nPlease enter the book rack:");
scanf("%d", &(new_node->data->rackno));
}
最佳答案
您可以将它保存在一个全局变量中,然后在您的函数中使用它。喜欢:
...
typedef struct _node
{
book* data;
struct _node* next;
}Node;
struct Node *head;
struct Node *tail;
//more declarations
int main(void)
{
//code
}
...
注意:用小写字母命名变量。
首先初始化head
和tail
,然后直接在你的函数中使用它们。
关于你的第二个问题,为什么不。但更好的选择是 append
(类似于您的 new_node),然后是 insert
函数。
注意:你把“Node* new_node(int data)
”放在上面,这会给你错误,因为类型不兼容,最好是:Node* new_node(book **data)
。
希望对您有所帮助。
关于c - C中的节点和链表语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34577205/