【发布时间】:2018-07-10 08:30:16
【问题描述】:
我正在尝试使用代码块编辑器在 C 上制作图书馆管理系统程序。
到目前为止,我已经成功地制作了带有姓名和作者姓名的图书馆书籍的程序打印列表,但是当它询问我是否要按升序排列它并按'y'时,它再次打印相同列表而不按升序排列(我正在尝试使用冒泡排序算法进行排序)。
请相应修改代码并附上详细说明。
原文:
#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;
}
}
}
}
第一次编辑:
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;
}
}
}
第二次修改:
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;
}
}
}
第三次修改
#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");
}
}
【问题讨论】:
-
乍一看:
for (k=1;k=count-1;k++)错了两次。首先,如果它应该是一个比较,那么它很少是真的,其次,它是一个分配。 -
if(i->name>j->name)你不能像那样比较字符串。 -
i<=NULL-k;应该比较什么?应该是i != NULL; -
j=i->nexti 未分配任何值 -
请注意
fflush(stdin)是UB。
标签: c sorting linked-list bubble-sort