C 如果输入的数据不在列表末尾,则带有插入排序的双链接列表将继续覆盖
我试图实现一个插入排序双链表与用户输入,排序列表每次用户输入数据 如果在用户输入所有数据后调用插入排序函数,我的代码就可以正常工作C 如果输入的数据不在列表末尾,则带有插入排序的双链接列表将继续覆盖,c,doubly-linked-list,insertion-sort,C,Doubly Linked List,Insertion Sort,我试图实现一个插入排序双链表与用户输入,排序列表每次用户输入数据 如果在用户输入所有数据后调用插入排序函数,我的代码就可以正常工作 User Input: 5 User Input: 1 User Input: 3 User Input: 8 User Input: 7 User Input: 9 Unsorted List: 5 1 3 8 7 9 //Insertion Sort Function gets called after all the data have be
User Input: 5
User Input: 1
User Input: 3
User Input: 8
User Input: 7
User Input: 9
Unsorted List: 5 1 3 8 7 9
//Insertion Sort Function gets called after all the data have been entered//
Output: 1 3 5 7 8 9
但如果每次用户输入一个新数据时我调用插入排序函数,它就会出错。如果插入的节点不在列表的末尾,它就会覆盖下一个节点,即使它已成功地将新数据插入列表之间
User Input: 1
//Insertion Sort Function gets called//
Output: 1
User Input: 2
//Insertion Sort Function gets called//
Output: 1 2
User Input: 9
//Insertion Sort Function gets called//
Output: 1 2 9
User Input: 3
//Insertion Sort Function gets called//
Output: 1 2 3 9
User Input: 4
//Insertion Sort Function gets called//
Output: 1 2 3 4
如您所见,3被很好地插入到列表中,但不知怎的,我的代码认为列表的结尾现在在3之后,所以当我在新列表中输入一个新数据时,例如,4,9被覆盖,因为它认为3是列表的结尾
User Input: 1
//Insertion Sort Function gets called//
Output: 1
User Input: 2
//Insertion Sort Function gets called//
Output: 1 2
User Input: 9
//Insertion Sort Function gets called//
Output: 1 2 9
User Input: 3
//Insertion Sort Function gets called//
Output: 1 2 3 9
User Input: 4
//Insertion Sort Function gets called//
Output: 1 2 3 4
我的插入排序函数有问题吗
如何导航回列表末尾并使其在列表末尾添加新数据
我的全部代码包括在下面
#include<stdio.h>
#include<stdlib.h>
//// structure for a node ////
struct Node
{
int data;
struct Node *prev;
struct Node *next;
};
typedef struct Node NODE;
void UserInput();
void printList(struct Node *head, );
void insertionSort(struct Node **head_ref);
void sortedInsert(struct Node** head_ref, struct Node* newNode);
void main()
{
UserInput();
_getch();
}
void UserInput()
{
int loop=0;
int data;
struct Node *temp;
NODE *tail = NULL;
struct Node *head;
head = (struct Node*)malloc(sizeof(struct Node)); ////create node, store address to temp
printf("\nEnter Data: ");
scanf(" %d" , &data);
head->data = data;
head->prev = NULL; //// head node's prev is set to NULL
head->next = NULL; //// head node's next is set to NULL
tail = head;
printList(head);
while (loop < 9999)
{
temp = (struct Node*)malloc(sizeof(struct Node)); ////create node, store address to temp
printf("\nEnter Data: ");
scanf(" %d" , &data);
if(data == 9999)
{
break;
}
temp->data = data;
printList(head);
temp->prev = tail; //// prev pointer of temp is referenced to the last node
temp->next = NULL; //// next pointer of temp is NULL
tail->next = temp; //// next pointer of the tail node is referenced to the temp
tail = temp; //// temp is made as the tail node
insertionSort(&head);
printList(head);
loop++;
}
printf("Insertion of Numbers Terminated.");
//// If insertionSort function is called after all User Inputs, there are no problems in sorting.
//insertionSort(&temp);
printList(temp);
}
////Printing Function to get called every loop////
void printList(struct Node *head)
{
struct Node *temp1 = head;
temp1 = head;
printf("\nSorted Numbers: ");
while(temp1 != NULL)
{
printf(" %d ", temp1->data);
temp1 = temp1->next;
}
}
////Insertion Sort Algorithm to sort the list////
void insertionSort(struct Node **head_ref)
{
//// Initialize 'sorted' - a sorted doubly linked list
struct Node* sorted = NULL;
//// Traverse the given doubly 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;
//// removing all the links so as to create 'current' as a new node for insertion
current->prev = current->next = NULL;
//// insert current in 'sorted' doubly linked list
sortedInsert(&sorted, current);
//// Update current
current = next;
}
//// Update head_ref to point to sorted doubly linked list
*head_ref = sorted;
}
void sortedInsert(struct Node** head_ref, struct Node* newNode)
{
struct Node* current;
//// if list is empty
if (*head_ref == NULL)
{
*head_ref = newNode;
}
//// if the node is to be inserted at the beginning of the doubly linked list
else if ((*head_ref)->data >= newNode->data)
{
newNode->next = *head_ref;
newNode->next->prev = newNode;
*head_ref = newNode;
}
else
{
current = *head_ref;
//// locate the node after which the new node is to be inserted
while (current->next != NULL && current->next->data < newNode->data)
{
current = current->next;
}
////Make the appropriate links ////
newNode->next = current->next;
//// if the new node is not inserted at the end of the list
if (current->next != NULL)
{
newNode->next->prev = newNode;
}
current->next = newNode;
newNode->prev = current;
}
}
#包括
#包括
////节点的结构////
结构节点
{
int数据;
结构节点*prev;
结构节点*下一步;
};
typedef结构节点;
void UserInput();
无效打印列表(结构节点*头,);
void insertionSort(结构节点**head\u ref);
void-sortedInsert(结构节点**头部参照,结构节点*新节点);
void main()
{
用户输入();
_getch();
}
void UserInput()
{
int循环=0;
int数据;
结构节点*temp;
NODE*tail=NULL;
结构节点*头部;
head=(结构节点*)malloc(sizeof(结构节点));///创建节点,将地址存储到临时
printf(“\n输入数据:”);
scanf(“%d”和数据);
头部->数据=数据;
head->prev=NULL;///head节点的prev设置为NULL
head->next=NULL;///head节点的next设置为NULL
尾=头;
印刷品清单(标题);
while(循环<9999)
{
temp=(结构节点*)malloc(sizeof(结构节点));///创建节点,将地址存储到temp
printf(“\n输入数据:”);
scanf(“%d”和数据);
如果(数据==9999)
{
打破
}
温度->数据=数据;
印刷品清单(标题);
temp->prev=tail;///temp的prev指针被引用到最后一个节点
temp->next=NULL;///temp的下一个指针为NULL
tail->next=temp;///tail节点的下一个指针引用到temp
tail=temp;///temp作为tail节点
插入端口(&head);
印刷品清单(标题);
loop++;
}
printf(“插入终止的数字”);
////如果在所有用户输入之后调用insertionSort函数,则排序没有问题。
//插入排序(&temp);
打印列表(temp);
}
////打印函数以获得每个循环的调用////
无效打印列表(结构节点*头)
{
结构节点*temp1=头部;
temp1=头部;
printf(“\n未选数字:”);
while(temp1!=NULL)
{
printf(“%d”,temp1->data);
temp1=temp1->next;
}
}
////插入排序算法对列表进行排序////
void insertionSort(结构节点**head\u ref)
{
////初始化“已排序”-已排序的双链接列表
结构节点*sorted=NULL;
////遍历给定的双链接列表并将每个节点插入“排序”
结构节点*current=*head\u ref;
while(当前!=NULL){
////为下一次迭代存储下一个
结构节点*下一步=当前->下一步;
////删除所有链接以创建“当前”作为插入的新节点
当前->上一个=当前->下一个=空;
////在“已排序”双链接列表中插入当前
分拣机(&分拣,当前);
////更新当前
当前=下一个;
}
////更新head_ref以指向已排序的双链接列表
*head_ref=已排序;
}
void sortedInsert(结构节点**头部参照,结构节点*新节点)
{
结构节点*当前;
////如果列表为空
如果(*head_ref==NULL)
{
*head_ref=新节点;
}
////如果要在双链接列表的开头插入节点
如果((*head\u ref)->data>=newNode->data)
{
newNode->next=*head\u ref;
newNode->next->prev=newNode;
*head_ref=新节点;
}
其他的
{
电流=*水头参考;
////找到要插入新节点的节点
while(当前->下一步!=NULL&¤t->next->datadata)
{
当前=当前->下一步;
}
////建立适当的链接////
新建节点->下一步=当前->下一步;
////如果未在列表末尾插入新节点
如果(当前->下一步!=NULL)
{
newNode->next->prev=newNode;
}
当前->下一步=新节点;
newNode->prev=当前;
}
}
问题出在UserInput()
插入数据时,临时节点的下一个应该指向尾部的下一个,因此更改此行
temp->next = NULL;
进入这个
temp->next = tail->next;
为什么??因为您正在交换节点本身,所以在每次
insertionSort()
之后,下一个更新的tail不会为NULL,而是指向以前添加的某个节点的地址,因为我们正在交换节点,所以地址正在交换。检查此处的示例代码:与其交换节点本身,不如交换数据部分?此行不会导致异常吗<代码>打印列表(头、尾)代码>@BeshambherChaukhwan练习的中心是交换节点,所以我不能只交换数据部分。是的,对不起,打印列表(头,尾);该部分是我在尝试让代码知道列表末尾在哪里时所做的一些编辑的残余,因为printList仍然完美地打印整个节点。我搬走了