C 无法对动态分配的节点的链接列表进行排序
我的程序接收用户输入的任意数量的单词,并且仅当用户键入三个星号(***)时才会停止。这些单词将存储在链接列表中并进行排序,但是当我尝试打印列表时,列表中的元素没有排序。为什么会这样C 无法对动态分配的节点的链接列表进行排序,c,linked-list,C,Linked List,我的程序接收用户输入的任意数量的单词,并且仅当用户键入三个星号(***)时才会停止。这些单词将存储在链接列表中并进行排序,但是当我尝试打印列表时,列表中的元素没有排序。为什么会这样 #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_STRING_LEN 32 // Limit on length of each string /* Link list node
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STRING_LEN 32 // Limit on length of each string
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
// Function to insert a given node in a sorted linked list
void sortedInsert(struct Node**, struct Node*);
// function to sort a singly linked list using insertion sort
void insertionSort(struct Node **head_ref)
{
// Initialize sorted linked list
struct Node *sorted = NULL;
// Traverse the given linked list and insert every
// node to sorted
struct Node *current = *head_ref;
while (current != NULL)
{
// Store next for next iteration
struct Node *next = current->next;
// insert current in sorted linked list
sortedInsert(&sorted, current);
// Update current
current = next;
}
// Update head_ref to point to sorted linked list
*head_ref = sorted;
}
/* function to insert a new_node in a list. Note that this
function expects a pointer to head_ref as this can modify the
head of the input linked list (similar to push())*/
void sortedInsert(struct Node** head_ref, struct Node* new_node)
{
struct Node* current;
/* Special case for the head end */
if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
{
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = *head_ref;
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
/* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */
/* Function to print linked list */
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%s \n ", temp->data);
temp = temp->next;
}
}
/* A utility function to insert a node at the beginning of linked list */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// Main function
int main()
{
struct Node *a = NULL;
int index=1;
int i;
char * strings[MAX_STRINGS]; // Array of pointers
do
{
strings[index] = malloc(MAX_STRING_LEN * sizeof(char));
printf("Please input a word : ", index);
fgets(strings[index],MAX_STRING_LEN,stdin);
strtok(strings[index], "\n")=='\0';
}
while(strcmp(strings[index++], "***")!=0);
printf("\nThe input set, in alphabetical order:\n");
for (i = 1; i < index-1; i++)
{
push(&a, strings[i]);
}
insertionSort(&a);
printList(a);
free(strings[i]);
return 0;
}
给定输出:
pear
apple
预期产出:
apple
pear
嗨,我花了一些时间想你想做什么。并发现您的代码存在许多问题。我想你想要这样的东西
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STRING_LEN 32 // Limit on length of each string
#define MAX_STRINGS 100 // Limit the number of strings
struct Node {
char *data;
struct Node *next;
};
void sortedInsert(struct Node **head_ref, struct Node* new_node) {
struct Node *counter = *head_ref;
struct Node *previous = NULL;
while (counter != NULL && strcmp(counter->data,new_node->data) < 0){
previous = counter;
counter = counter->next;
}
if (previous != NULL) {
previous->next = new_node;
new_node->next = counter;
} else {
new_node->next = *head_ref;
*head_ref = new_node;
}
}
void push(struct Node **head_ref, char *new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
if (!head_ref) {
*head_ref = new_node;
return;
}
sortedInsert(head_ref,new_node);
}
void printList(struct Node *head) {
struct Node *temp = head;
while(temp != NULL) {
printf("%s\n", temp->data);
temp = temp->next;
}
}
void freeLinkedList(struct Node *head) {
struct Node *next;
while(head) {
next = head;
head = head->next;
free(next);
}
}
// Main function
int main() {
struct Node *a = NULL;
int index = 0;
int i;
char * strings[MAX_STRINGS]; // Array of pointers
do {
strings[index] = malloc(MAX_STRING_LEN * sizeof(char));
printf("Please input a word %d: ", index);
scanf("%s",strings[index]);
fflush(stdin);
} while(strcmp(strings[index++], "***") != 0 && index < MAX_STRINGS);
printf("\nThe input set, in alphabetical order:\n");
for (i = 0; i < index-1; i++) push(&a, strings[i]);
printList(a);
return 0;
}
#包括
#包括
#包括
#定义最大字符串长度32//每个字符串的长度限制
#定义最大字符串数100//限制字符串数
结构节点{
字符*数据;
结构节点*下一步;
};
void sortedInsert(结构节点**头部参照,结构节点*新节点){
结构节点*计数器=*头\u参考;
结构节点*previous=NULL;
while(计数器!=NULL&&strcmp(计数器->数据,新节点->数据)<0){
先前=计数器;
计数器=计数器->下一步;
}
如果(上一个!=NULL){
上一步->下一步=新建_节点;
新建_节点->下一步=计数器;
}否则{
新建节点->下一步=*头部\u参考;
*head\u ref=新节点;
}
}
无效推送(结构节点**head\u ref,char*新数据){
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建_节点->数据=新建_数据;
新建节点->下一步=空;
如果(!head_ref){
*head\u ref=新节点;
返回;
}
分拣机(主机参考,新节点);
}
无效打印列表(结构节点*头){
结构节点*温度=头部;
while(temp!=NULL){
printf(“%s\n”,临时->数据);
温度=温度->下一步;
}
}
void freeLinkedList(结构节点*头){
结构节点*下一步;
while(head){
下一个=头部;
头部=头部->下一步;
免费(下一个);
}
}
//主要功能
int main(){
结构节点*a=NULL;
int指数=0;
int i;
char*strings[MAX_strings];//指针数组
做{
strings[index]=malloc(MAX_STRING_LEN*sizeof(char));
printf(“请输入一个单词%d:”,索引);
scanf(“%s”,字符串[索引]);
fflush(stdin);
}while(strcmp(strings[index++],“***”!=0&&index
我已经测试过了,效果很好。
如果你有任何疑问,我想澄清。
如果您想知道代码中的错误,请随时询问。
int index=1代码>从索引1开始存储,然后从索引0打印。好的,我在for循环中将I=0更改为I=1。但是我的链表仍然没有被排序:(你的编译器没有发出警告吗?push(结构节点**head\u ref,int new\u data)
这样,节点结构将int
存储为数据。但是push(&a,strings[i])
。您正在传递一个字符*
。我不确定您想要什么。为什么节点不存储字符串?为什么存储int
?嗨,吉姆·赫克,如果我的答案是您要找的答案,请上传并将其标记为正确答案。您能解释一下新的sortedInsert和push函数是如何工作的吗?我选择了this作为答案插入排序的基本原则是“在已排序的列表中插入一个新值”。这里从单个元素开始,并将新元素插入正确的位置。sortedInsert正在检查链接列表(通过将新_节点中的字符串与计数器进行比较,并将其插入previous和counter之间。如果没有previous,则新_节点将成为标头。感谢您的提问:)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STRING_LEN 32 // Limit on length of each string
#define MAX_STRINGS 100 // Limit the number of strings
struct Node {
char *data;
struct Node *next;
};
void sortedInsert(struct Node **head_ref, struct Node* new_node) {
struct Node *counter = *head_ref;
struct Node *previous = NULL;
while (counter != NULL && strcmp(counter->data,new_node->data) < 0){
previous = counter;
counter = counter->next;
}
if (previous != NULL) {
previous->next = new_node;
new_node->next = counter;
} else {
new_node->next = *head_ref;
*head_ref = new_node;
}
}
void push(struct Node **head_ref, char *new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
if (!head_ref) {
*head_ref = new_node;
return;
}
sortedInsert(head_ref,new_node);
}
void printList(struct Node *head) {
struct Node *temp = head;
while(temp != NULL) {
printf("%s\n", temp->data);
temp = temp->next;
}
}
void freeLinkedList(struct Node *head) {
struct Node *next;
while(head) {
next = head;
head = head->next;
free(next);
}
}
// Main function
int main() {
struct Node *a = NULL;
int index = 0;
int i;
char * strings[MAX_STRINGS]; // Array of pointers
do {
strings[index] = malloc(MAX_STRING_LEN * sizeof(char));
printf("Please input a word %d: ", index);
scanf("%s",strings[index]);
fflush(stdin);
} while(strcmp(strings[index++], "***") != 0 && index < MAX_STRINGS);
printf("\nThe input set, in alphabetical order:\n");
for (i = 0; i < index-1; i++) push(&a, strings[i]);
printList(a);
return 0;
}