kske
/
linked-list
Archived
1
Fork 0

Separated book struct into separate header

This commit is contained in:
Kai S. K. Engelbart 2020-01-23 14:11:20 +01:00
parent 55977d065a
commit 6c4fb1ef14
5 changed files with 63 additions and 41 deletions

View File

@ -142,6 +142,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="book.c" />
<ClCompile Include="main.c" /> <ClCompile Include="main.c" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -152,6 +153,9 @@
<Project>{43c12dd0-611c-43ee-82f4-f25b33eed30b}</Project> <Project>{43c12dd0-611c-43ee-82f4-f25b33eed30b}</Project>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClInclude Include="book.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@ -18,5 +18,13 @@
<ClCompile Include="main.c"> <ClCompile Include="main.c">
<Filter>Quelldateien</Filter> <Filter>Quelldateien</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="book.c">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="book.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

35
Demonstration/book.c Normal file
View File

@ -0,0 +1,35 @@
#include "book.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Book* book_create(const char* title, const char* author, int year)
{
// initialize book
Book* book = calloc(1, sizeof(Book));
if (!book)
{
return NULL;
}
// initialize title and author
book->title = calloc(strlen(title), sizeof(char));
book->author = calloc(strlen(author), sizeof(char));
if (!book->title || !book->author)
{
return NULL;
}
strcpy(book->title, title);
strcpy(book->author, author);
book->year = year;
return book;
}
void book_print(const void* data)
{
const Book* book = (Book*)data;
printf("Book[title=%s,author=%s,year=%d]\n", book->title, book->author, book->year);
}

11
Demonstration/book.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
typedef struct {
char* title;
char* author;
int year;
} Book;
Book* book_create(const char* title, const char* author, int year);
void book_print(const void* data);

View File

@ -1,45 +1,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "../Implementation/linked_list.h" #include "../Implementation/linked_list.h"
#include "../../MenuLib/menu.h" #include "../../MenuLib/menu.h"
#include "book.h"
typedef struct {
char* title;
char* author;
int year;
} Book;
Book* book_create(const char* title, const char* author, int year)
{
// initialize book
Book* book = calloc(1, sizeof(Book));
if (!book)
{
return NULL;
}
// initialize title and author
book->title = calloc(strlen(title), sizeof(char));
book->author = calloc(strlen(author), sizeof(char));
if (!book->title || !book->author)
{
return NULL;
}
strcpy(book->title, title);
strcpy(book->author, author);
book->year = year;
return book;
}
void book_print(const void* data)
{
const Book* book = (Book*)data;
printf("Book[title=%s,author=%s,year=%d]\n", book->title, book->author, book->year);
}
void show_push(Node** list) void show_push(Node** list)
{ {
@ -85,7 +49,7 @@ void show_insert(Node** list)
printf("Enter place after which book is inserted: "); printf("Enter place after which book is inserted: ");
scanf_s(" %d", &index); scanf_s(" %d", &index);
list_insert(list_get(list, index), book_create(title, author, year)); list_insert(list_get(*list, index), book_create(title, author, year));
} }
void show_print(Node** list) void show_print(Node** list)
@ -98,7 +62,7 @@ void show_get(Node** list)
int index; int index;
printf("Enter place: "); printf("Enter place: ");
scanf_s(" %d", &index); scanf_s(" %d", &index);
list_print(list_get(list,index), &book_print); list_print(list_get(*list,index), &book_print);
} }
void show_remove(Node** list) void show_remove(Node** list)
@ -106,7 +70,7 @@ void show_remove(Node** list)
int index; int index;
printf("Enter place: "); printf("Enter place: ");
scanf_s(" %d", &index); scanf_s(" %d", &index);
list_remove(list_get(list, index)); list_remove(list_get(*list, index));
} }
@ -121,7 +85,7 @@ void main(void)
{"Print item", '4', (void*)&show_print, &list}, {"Print item", '4', (void*)&show_print, &list},
{"Look at item by index", '5', (void*)&show_get, &list}, {"Look at item by index", '5', (void*)&show_get, &list},
{"Remove item by index", '6', (void*)&show_remove, &list}, {"Remove item by index", '6', (void*)&show_remove, &list},
{"BLANK", NULL, NULL, NULL}, {"BLANK", ' ', NULL, NULL},
{"Quit", 'q', (void*)&exit, NULL} {"Quit", 'q', (void*)&exit, NULL}
}; };
struct MenuPage pages[] = { struct MenuPage pages[] = {