C 在用户输入后使用快速排序对单链表进行排序,然后插入新节点并重新排序列表

C 在用户输入后使用快速排序对单链表进行排序,然后插入新节点并重新排序列表,c,C,我有我的大部分代码,但有一个粗略的去尝试让我的快速排序功能的工作和排序通过实际的链接列表创建。不知道我是否调用了不正确的函数,或者我的结构是否正确 程序将编译并运行,直到到达快速排序的调用函数。然后它就冻结了,什么也不做。任何帮助都会很好。谢谢你的时间 #include <stdio.h> #include <stdlib.h> struct node{ int data; struct node *link_list; }; struct n

我有我的大部分代码,但有一个粗略的去尝试让我的快速排序功能的工作和排序通过实际的链接列表创建。不知道我是否调用了不正确的函数,或者我的结构是否正确

程序将编译并运行,直到到达快速排序的调用函数。然后它就冻结了,什么也不做。任何帮助都会很好。谢谢你的时间

    #include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node *link_list;
};

struct node *insertion(struct node *pointer, int i){
    struct node *temp_val;
    if(pointer == NULL){
        pointer = (struct node *)malloc(sizeof(struct node));
        if(pointer == NULL){
            printf("Error Exiting\n");
            exit(0);
        }
        pointer->data = i;
        pointer->link_list = pointer;
    }else{
        temp_val = pointer;
        while(temp_val->link_list != pointer){
            temp_val = temp_val->link_list;
        }
        temp_val->link_list = (struct node *)malloc(sizeof(struct node));
        if(temp_val->link_list == NULL){
            printf("Error Exiting\n");
            exit(0);
        }
        temp_val = temp_val->link_list;
        temp_val->data = i;
        temp_val->link_list = pointer;
    }
    return(pointer);    
};


struct node *findPivot(struct node *head, struct node *term, struct node **newHead, struct node **newTerm){
    struct node *pivot = term;
    struct node *previous = NULL, *current = head, *tail = pivot;
    //finding the pivot and dividing the list while also updating the head and term
    // with newHead and newTerm
    while(current != pivot){
        if(current->data < pivot->data){
            //assigning the newHead to the first value less then the pivot
            if((*newHead) == NULL){
                (*newHead) = current;
            }
            previous = current;
            current = current->link_list;
        }else{
            // if the current node has a higher value then the pivot
            // assinging it to newTerm
            if(previous){
                previous->link_list = current->link_list;
            }
            struct node *temp = current->link_list;
            current->link_list = NULL;
            tail->link_list = current;
            tail = current;
            current = temp;
        }
    }
    //Checks the case if the pivot is the smallest value and moves to head
    if((*newHead)== NULL){
        (*newHead) = pivot;
    }
    (*newTerm) = tail; // makes sure the last element is newEnd

    return pivot;

}
//finds the last node in the list and returns it
struct node *getTail(struct node *current){
    while(current != NULL && current->link_list != NULL){
        current = current->link_list;
    }
    return current;
}

// the actual recursive quicksort algorithm
struct node *quickSort(struct node *head, struct node *term){
    if(!head || head == term)  //base case for the recursion
        return head;

    struct node *newHead = NULL, *newTerm = NULL;

    // the recursive case
    struct node *pivot = findPivot(head, term, &newHead, &newTerm);

    //no need for recursion if pivot is smallest value
    if(newHead != pivot){
        struct node *temp = newHead;
        while(temp->link_list != pivot){
            temp = temp->link_list;
        }
        temp->link_list = NULL;
        newHead = quickSort(newHead, temp);
        temp = getTail(newHead);
        temp->link_list = pivot;
    }

    pivot->link_list = quickSort(pivot->link_list, newTerm);

    return newHead;

}

void quickSortFunction(struct node **pointer){
    *pointer = quickSort(*pointer, getTail(*pointer));
    return;
}

void printList_Unsorted(struct node *pointer){
    struct node *temp;
    temp = pointer;
    printf("\nThe Data values in the list are:\n");
    if(pointer != NULL){
        do{
            printf("%d\t", temp->data);
            temp = temp->link_list;
        }while(temp != pointer);
    }else{
        printf("the list is empty\n");
    }
}


void printList_Sorted(struct node *node){
    while(node!= NULL){
        printf("%d ", node->data);
        node = node->link_list;
    }
    printf("\n");
}

int main(int argc, char *argv[]) {
    int num_nodes, node_val;
    struct node *list = NULL;
    printf("Enter the number of nodes to be created: ");
    scanf("%d", &num_nodes);

    while(num_nodes --> 0){
        printf("\n\nEnter the data values to be placed in a node: ");
        scanf("%d", &node_val);
        list = insertion(list, node_val);
    }
    printf("\n\nThe Created list is as follow:\n");
    printList_Unsorted(list);
    printf("\n");

    quickSortFunction(&list);
    printList_Sorted(list);

    //getchar();
    //getchar();



    return 0;
}
#包括
#包括
结构节点{
int数据;
结构节点*链接列表;
};
结构节点*插入(结构节点*指针,int i){
结构节点*临时值;
if(指针==NULL){
指针=(结构节点*)malloc(sizeof(结构节点));
if(指针==NULL){
printf(“退出时出错\n”);
出口(0);
}
指针->数据=i;
指针->链接列表=指针;
}否则{
温度=指针;
while(临时值->链接列表!=指针){
临时值=临时值->链接列表;
}
临时值->链接列表=(结构节点*)malloc(sizeof(结构节点));
如果(临时值->链接列表==NULL){
printf(“退出时出错\n”);
出口(0);
}
临时值=临时值->链接列表;
温度值->数据=i;
临时值->链接列表=指针;
}
返回(指针);
};
结构节点*findPivot(结构节点*head,结构节点*term,结构节点**newHead,结构节点**newTerm){
结构节点*pivot=术语;
结构节点*previous=NULL,*current=head,*tail=pivot;
//查找轴心并划分列表,同时更新标题和术语
//带着新头和新术语
while(当前!=轴){
如果(当前->数据<透视->数据){
//将newHead指定给小于枢轴的第一个值
if((*newHead)==NULL){
(*新头)=当前;
}
先前=当前;
当前=当前->链接列表;
}否则{
//如果当前节点具有更高的值,则轴
//将其归入新术语
如果(先前){
上一个->链接列表=当前->链接列表;
}
结构节点*temp=当前->链接列表;
当前->链接列表=空;
尾部->链接列表=当前;
尾=电流;
电流=温度;
}
}
//检查轴是否为最小值并移动到头部
if((*newHead)==NULL){
(*newHead)=支点;
}
(*newTerm)=tail;//确保最后一个元素是newEnd
返回轴;
}
//查找列表中的最后一个节点并返回它
结构节点*getTail(结构节点*current){
while(当前!=NULL&¤t->link\u list!=NULL){
当前=当前->链接列表;
}
回流;
}
//实际递归快速排序算法
结构节点*快速排序(结构节点*头,结构节点*术语){
if(!head | | head==term)//递归的基本情况
回流头;
结构节点*newHead=NULL,*newTerm=NULL;
//递归情形
结构节点*pivot=findPivot(head、term、newHead和newTerm);
//如果pivot是最小值,则不需要递归
if(newHead!=枢轴){
结构节点*temp=newHead;
同时(临时->链接列表!=枢轴){
temp=temp->link\u列表;
}
临时->链接列表=空;
newHead=快速排序(newHead,temp);
temp=getTail(newHead);
临时->链接列表=枢轴;
}
透视->链接列表=快速排序(透视->链接列表,新术语);
返回newHead;
}
void quickSortFunction(结构节点**指针){
*指针=快速排序(*指针,getTail(*指针));
返回;
}
无效打印列表\u未排序(结构节点*指针){
结构节点*temp;
温度=指针;
printf(“\n列表中的数据值为:\n”);
if(指针!=NULL){
做{
printf(“%d\t”,临时->数据);
temp=temp->link\u列表;
}while(temp!=指针);
}否则{
printf(“列表为空\n”);
}
}
已排序的无效打印列表(结构节点*节点){
while(节点!=NULL){
printf(“%d”,节点->数据);
节点=节点->链接列表;
}
printf(“\n”);
}
int main(int argc,char*argv[]){
int num_nodes,node_val;
结构节点*list=NULL;
printf(“输入要创建的节点数:”);
scanf(“%d”、&num_节点);
而(节点数-->0){
printf(“\n\n输入要放置在节点中的数据值:”;
scanf(“%d”和节点值);
列表=插入(列表、节点值);
}
printf(“\n\n创建的列表如下:\n”);
打印列表\未分类(列表);
printf(“\n”);
quickSortFunction(&list);
已排序的打印列表(列表);
//getchar();
//getchar();
返回0;
}

请看这个工作示例

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *link_list;
};

void insertion(struct node **pointer, int i) {
    struct node *temp_val = malloc(sizeof *temp_val);
    temp_val->data = i;
    temp_val->link_list = (*pointer);
    (*pointer) = temp_val;
}

/* A utility function to print linked list */
void printList(struct node *node) {
    while (node != NULL) {
        printf("%d  ", node->data);
        node = node->link_list;
    }
    printf("\n");
}

// Returns the last node of the list
struct node *getTail(struct node *current) {
    while (current != NULL && current->link_list != NULL)
        current = current->link_list;
    return current;
}


struct node *findPivot(struct node *head, struct node *term,
                       struct node **newHead, struct node **newTerm) {
    struct node *pivot = term;
    struct node *previous = NULL, *current = head, *tail = pivot;


    while (current != pivot) {
        if (current->data < pivot->data) {

            if ((*newHead) == NULL)
                (*newHead) = current;

            previous = current;
            current = current->link_list;
        }
        else
        {

            if (previous)
                previous->link_list = current->link_list;
            struct node *tmp = current->link_list;
            current->link_list = NULL;
            tail->link_list = current;
            tail = current;
            current = tmp;
        }
    }

    // If the pivot data is the smallest element in the current list,
    // pivot becomes the head
    if ((*newHead) == NULL)
        (*newHead) = pivot;

    // Update newTerm to the current last node
    (*newTerm) = tail;

    // Return the pivot node
    return pivot;
}


// the actual recursive quicksort algorithe
struct node *quickSort(struct node *head, struct node *end) {
    // base case
    if (!head || head == end)
        return head;

    struct node *newHead = NULL, *newEnd = NULL;


    struct node *pivot = findPivot(head, end, &newHead, &newEnd);


    if (newHead != pivot) {

        struct node *tmp = newHead;
        while (tmp->link_list != pivot)
            tmp = tmp->link_list;
        tmp->link_list = NULL;


        newHead = quickSort(newHead, tmp);


        tmp = getTail(newHead);
        tmp->link_list = pivot;
    }

    pivot->link_list = quickSort(pivot->link_list, newEnd);

    return newHead;
}


void quickSortFunction(struct node **headRef) {
    (*headRef) = quickSort(*headRef, getTail(*headRef));
    return;
}


int main() {
    struct node *list = NULL;

    int num_nodes, node_val;
    printf("Enter the number of nodes to be created: ");
    scanf("%d", &num_nodes);
    while(num_nodes --> 0){
        printf("\n\nEnter the data values to be placed in a node: ");
        scanf("%d", &node_val);
        insertion(&list, node_val);
    }
    printf("\n\nThe Created list is as follows:\n");
    printList(list);
    printf("\n");
    quickSortFunction(&list);
    printList(list);
    return 0;
}

代码的问题是它进入了无限循环,因为参数不是指向节点的指针,而是指向结构的指针。您也不需要返回列表,因为您是通过引用传递它的

请看这个工作示例

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *link_list;
};

void insertion(struct node **pointer, int i) {
    struct node *temp_val = malloc(sizeof *temp_val);
    temp_val->data = i;
    temp_val->link_list = (*pointer);
    (*pointer) = temp_val;
}

/* A utility function to print linked list */
void printList(struct node *node) {
    while (node != NULL) {
        printf("%d  ", node->data);
        node = node->link_list;
    }
    printf("\n");
}

// Returns the last node of the list
struct node *getTail(struct node *current) {
    while (current != NULL && current->link_list != NULL)
        current = current->link_list;
    return current;
}


struct node *findPivot(struct node *head, struct node *term,
                       struct node **newHead, struct node **newTerm) {
    struct node *pivot = term;
    struct node *previous = NULL, *current = head, *tail = pivot;


    while (current != pivot) {
        if (current->data < pivot->data) {

            if ((*newHead) == NULL)
                (*newHead) = current;

            previous = current;
            current = current->link_list;
        }
        else
        {

            if (previous)
                previous->link_list = current->link_list;
            struct node *tmp = current->link_list;
            current->link_list = NULL;
            tail->link_list = current;
            tail = current;
            current = tmp;
        }
    }

    // If the pivot data is the smallest element in the current list,
    // pivot becomes the head
    if ((*newHead) == NULL)
        (*newHead) = pivot;

    // Update newTerm to the current last node
    (*newTerm) = tail;

    // Return the pivot node
    return pivot;
}


// the actual recursive quicksort algorithe
struct node *quickSort(struct node *head, struct node *end) {
    // base case
    if (!head || head == end)
        return head;

    struct node *newHead = NULL, *newEnd = NULL;


    struct node *pivot = findPivot(head, end, &newHead, &newEnd);


    if (newHead != pivot) {

        struct node *tmp = newHead;
        while (tmp->link_list != pivot)
            tmp = tmp->link_list;
        tmp->link_list = NULL;


        newHead = quickSort(newHead, tmp);


        tmp = getTail(newHead);
        tmp->link_list = pivot;
    }

    pivot->link_list = quickSort(pivot->link_list, newEnd);

    return newHead;
}


void quickSortFunction(struct node **headRef) {
    (*headRef) = quickSort(*headRef, getTail(*headRef));
    return;
}


int main() {
    struct node *list = NULL;

    int num_nodes, node_val;
    printf("Enter the number of nodes to be created: ");
    scanf("%d", &num_nodes);
    while(num_nodes --> 0){
        printf("\n\nEnter the data values to be placed in a node: ");
        scanf("%d", &node_val);
        insertion(&list, node_val);
    }
    printf("\n\nThe Created list is as follows:\n");
    printList(list);
    printf("\n");
    quickSortFunction(&list);
    printList(list);
    return 0;
}

代码的问题是它进入了无限循环,因为参数不是指向节点的指针,而是指向结构的指针。您也不需要返回列表,因为您是通过引用传递它的

建议您使用调试器跟踪程序的执行。这确实是你得到的最好的建议。建议你使用调试器来跟踪程序的执行。这真的是你能得到的最好的建议。所以我比我创建的插入函数更喜欢你的插入函数,它更优雅,更中肯。后来,我在main中添加了一个循环,询问用户是否希望插入新节点,然后调用插入函数和快速排序函数。但由于某种原因,当它进入问题时,程序将冻结,但如果我将其删除,则