I am trying to make Library Management System program on C using Code Block editor.
So far I have been successful in making the program print list of library books with their names and author's name, but when it asks if I want to arrange it in ascending order and I press 'y'
, it again prints the same list without arranging it in ascending order (I am trying to sort using Bubble Sort Algorithm).
Please modify the code accordingly with detailed explanation.
Original:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char name[50];
char author[50];
struct Node* next;
};
struct Node* head; // Global Variable
void Insert(char q[50], char r[50]);
void Print();
void bsort();
int main()
{
int n,i; //Local Variables
char x[50],y[50]; // Local Variables
char d;
head=NULL; //Initially head is null
printf("How many books you want to enter?: ");
scanf("%d",&n);
for (i=1; i<=n; i++) // Loop iterate the number of times we have books in quantity.
{
printf("Enter a Book name: ");
fflush(stdin); // To clear buffer memory
gets(x); // Same as scanf
printf("Author: ");
gets(y);
Insert(x,y);
}
Print();
printf("Do you want to sort the data in ascending order?(y/n): ");
scanf("%c",&d);
printf("Your pressed %c",d);
if (d=='y')
{
bsort();
Print();
}
else
printf("alright!");
return 0;
}
void Insert(char q[50], char r[50]) //Adding items at the end of linked list
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->next=NULL; // Since we are adding a node to the end, we are linking it to NULL.
strcpy(temp->name,q); // copying the contents of "q" to "temp->name"
strcpy(temp->author,r); // same
if(head==NULL)
{
head=temp;
}
else
{
struct Node* temp1 = head;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
}
}
void Print() //Traversing
{
printf("\n");
printf("The Library data is as follows: \n");
struct Node* temp=head;
printf("\n");
while(temp!=NULL)
{
printf("%25s",temp->name);
printf("%25s",temp->author);
temp=temp->next;
printf("\n");
}
}
void bsort() //Bubble Sort Algorithm to arrange Library data in Ascending Order
{
int count=1,k=1;
char temp3[50];
struct Node* i=head;
struct Node* j;
j=i->next;
while(i!=NULL)
{
i=i->next;
count++;
}
{
for (k=1; k=count-1; k++)
{
for (i=head; i<=NULL-k; i=i->next)
{
if(i->name>j->name)
{
strcpy(temp3,i->name);
strcpy(i->name,j->name);
strcpy(j->name,temp3);
}
j=j->next;
}
}
}
}
First Edit:
void bsort() //Bubble Sort Algorithm to arrange Library data in Ascending Order
{
int count=1,k=1;
char temp3[50];
struct Node* i=head;
struct Node* j;
j=i->next;
while(i!=NULL)
{
i=i->next;
count++;
}
for (k=1; k=count-1; k++)
{
for (i=head; i<=count-k; i=i->next)
{
if (strcmp(i->name,j->name)>0)
{
strcpy(temp3,i->name);
strcpy(i->name,j->name);
strcpy(j->name,temp3);
}
j=j->next;
}
}
}
Second edit:
void bsort() //Bubble Sort Algorithm to arrange Library data in Ascending Order
{
int count=1,k=1;
char temp3[50];
struct Node* i=head;
struct Node* j;
j=i->next;
while(i!=NULL)
{
i=i->next;
count++;
}
for (k=1; k<=count-1; k++)
{
for (i=head; i<=count-k; i=i->next)
{
if (strcmp(i->name,j->name)>0)
{
strcpy(temp3,i->name);
strcpy(i->name,j->name);
strcpy(j->name,temp3);
}
j=j->next;
}
}
}
Third Edit
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char name[50];
char author[50];
struct Node* next;
};
struct Node* head; // Global Variable
void Insert(char q[50], char r[50]);
void Print();
void bsort_print();
int main()
{ int n,i; //Local Variables
char x[50],y[50]; // Local Variables
char d;
head=NULL; //Initially head is null
printf("How many books you want to enter?: ");
scanf("%d",&n);
for (i=1; i<=n; i++) // Loop iterate the number of times we have books in quantity.
{
printf("Enter a Book name: ");
fflush(stdin); // To clear buffer memory
gets(x); // Same as scanf
printf("Author: ");
gets(y);
Insert(x,y);
}
Print();
printf("Do you want to sort the data in ascending order?(y/n): ");
scanf("%c",&d);
printf("Your pressed %c",d);
if (d=='y')
{
bsort_print();
}
else
printf("alright!");
return 0;
}
void Insert(char q[50], char r[50]) //Adding items at the end of linked list
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->next=NULL; // Since we are adding a node to the end, we are linking it to NULL.
strcpy(temp->name,q); // copying the contents of "q" to "temp->name"
strcpy(temp->author,r); // same
if(head==NULL)
{
head=temp;
}
else {
struct Node* temp1 = head;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
}
}
void Print() //Traversing
{
printf("\n");
printf("The Library data is as follows: \n");
struct Node* temp=head;
printf("\n");
while(temp!=NULL)
{
printf("%25s",temp->name);
printf("%25s",temp->author);
temp=temp->next;
printf("\n");
}
}
void bsort_print() //Bubble Sort Algorithm to arrange Library data in Ascending Order
{
int count=1,k=1;
char temp3[50];
struct Node* i=head;
struct Node* j;
struct Node* current=i;
j=i->next;
while(i!=NULL)
{
i=i->next;
count++;
}
for (k=1; k<=count-1; k++)
{
for (count=1; count<count-k; count++)
{
if (strcmp(i->name,j->name)>0)
{
strcpy(temp3,i->name);
strcpy(i->name,j->name);
strcpy(j->name,temp3);
}
j=j->next;
}
}
printf("\n");
printf("The Library data is as follows: \n");
printf("\n");
while(current!=NULL)
{
printf("%25s",current->name);
printf("%25s",current->author);
current=current->next;
printf("\n");
}
}