-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.c
158 lines (146 loc) · 3.18 KB
/
library.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node
{
struct Node *next;
char title[30];
char author[30];
char publisher[30];
int isbn;
};
struct Node *createnode()
{
struct Node *newnode = (struct Node*)malloc(sizeof(struct Node));
if(newnode == NULL)
{
printf("Memory not allocated");
return NULL;
}
printf("Enter title of book: ");
scanf("%s",newnode->title);
printf("Enter name of author: ");
scanf("%s",newnode->author);
printf("Enter name of publisher: ");
scanf("%s",newnode->publisher);
printf("Enter ISBN of book: ");
scanf("%d",&newnode->isbn);
newnode->next=NULL;
return newnode;
}
struct Node *add(struct Node *head)
{
struct Node *newnode = createnode();
if(head==NULL)
{
head=newnode;
return head;
}
struct Node *temp = head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
return head;
}
void search(struct Node *head)
{
struct Node *ptr=head;
int item, i=0, flag=1;
if(ptr==NULL)
{
printf("No Books found \n");
return;
}
printf("Enter ISBN of book to be searched: ");
scanf("%d",&item);
while(ptr!=NULL)
{
if(ptr->isbn == item)
{
printf("Book found, Book title: %s \n",ptr->title);
flag=0;
}
i++;
ptr=ptr->next;
}
if(flag==1)
{
printf("Book not found \n");
}
}
void display(struct Node *head)
{
if (head == NULL) {
printf("No books available.\n");
return;
}
printf("Books are: \n");
struct Node *temp = head;
while(temp!=NULL)
{
printf("%s \n",temp->title);
temp=temp->next;
}
printf("\n");
}
struct Node *deleteBook(struct Node *head, int place)
{
if (head == NULL) {
printf("No books to delete.\n");
return head;
}
struct Node *todel = head, *temp = head;
if (place == 1)
{
head = head->next;
free(temp);
printf("Book deleted successfully.\n");
return head;
}
for(int i=1 ; i<place ; i++)
{
temp=todel;
todel=todel->next;
}
temp->next=todel->next;
free(todel);
return head;
}
int main()
{
struct Node *head=NULL;
int ch, place;
while(1)
{
printf("\n1.Add a book\n2.Search for a book by ISBN\n3.Delete a book\n4.Display Books\n5.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
head = add(head);
printf("Book added \n");
break;
case 2:
search(head);
break;
case 3:
display(head);
printf("Enter position of book to be deleted: ");
scanf("%d",&place);
head=deleteBook(head, place);
break;
case 4:
display(head);
break;
case 5:
exit(0);
break;
default:
printf("Enter valid choice \n");
}
}
return 0;
}