问题遇到的现象和发生背景
使用链表,输入两个句子,并对其中的单词进行排序
遇到的现象和发生背景,请写出第一个错误信息
第一个句子可以完美排序,到了第二个句子就运行出错
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
typedef struct LinkList {
char word[M];
struct LinkList* next;//下一节点
}LNode, * LinkList;
LinkList InitList()
{//初始化链表
LinkList L = (LinkList)malloc(sizeof(LNode));// 为头结点分配空间
if (!L) exit(OVERFLOW);
L->next = NULL;
return L;
}
status InsertList(LinkList L, char* e,int i)
{//在第i个位置之后插入节点
LNode* p = L;
int j = 0;
while (p && j < i)
{
p = p->next;
j++;
}
LinkList s = (LinkList)malloc(sizeof(LNode));
strcpy(s->word, e);
s->next = p->next;
p->next = s;
return OK;
}
void bubble_sort(LinkList L)
{
int i, j;
LNode* p, * q;
char temp[10];
for (p = L->next, i = 1; p; i++, p = p->next)
{
for (q = L->next, j = 1; q; j++, q = q->next)
{
if (strcmp(p->word, q->word) < 0)
{
strcpy(temp,p->word);
strcpy(p->word, q->word);
strcpy(q->word, temp);
}
}
}
}
void showlist(LinkList L)
{
LNode* p = L->next;
while (p)
{
printf("%s ", p->word);
p = p->next;
}
}
void main()
{
LinkList l1 = InitList();
LinkList l2 = InitList();
char word[N][M];
char c = ' ';
int i = 0,j = 0;
printf("请输入第一个句子: ");
while (c != '.' && c != '!' && c != '?')
{
c = getchar();
if (c != ' ' && c != '.' && c != '!' && c != '?')
word[j][i++] = c;
else
{
word[j++][i] = '\0';
i = 0;
}
}
for (i = 0; i < j; i++)
InsertList(l1, word[i], 0);
bubble_sort(l1);
showlist(l1);
i = 0; j = 0; c = ' ';
printf("\n请输入第二个句子: \n");
while (c != '.' && c != '!' && c != '?')
{
c = getchar();
if (c != ' ' && c != '.' && c != '!' && c != '?')
word[j][i++] = c;
else
{
word[j++][i] = '\0';
i = 0;
}
}
for (i = 0; i < j; i++)
InsertList(l2, word[i], 0);
bubble_sort(l2);
showlist(l2);
}