且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

排序在C链表

更新时间:2023-11-10 10:46:46

有在 sortlist中函数的问题。

此功能只放一些大的节点列表的开头。它不soting所有列表。您可以在一个排序算法来排序文件:快速排序/冒泡/ ...
我把code在这个答案到底在做一个排序。

这里是一个code做排序列表:

//它与第一个替换最大节点然后做与子表相同的操作(列表的第一个元素)

  NODEPTR sortlist中(NODEPTR名单)
{//
如果(名单== NULL ||列表 - >接下来== NULL)
    返回列表; //列表进行排序。//与第一取代最大节点:// 1 - 找到最大的节点:
NODEPTR CURR,规模最大,最大的preV;
CURR =清单;
最大=清单;
preV =清单;
最大的preV =清单;
而(CURR!= NULL){
        如果(curr-> NUM> largest-> NUM){
            最大的preV = preV;
            最大= CURR;
        }
        preV = CURR;
        CURR = curr->接下来,    }
//最大节点处于最大。// 2 - 交换firt节点,节点最大:
NODEPTR TMP;
如果(最大!=名单)
{
    最大的preV->接下来=清单;
    TMP =列表 - >接下来,
    列表 - >接下来= largest->接下来,
    largest->接下来= TMP;
}//现在最大是列表的第一个节点。//与子列表中再次调用该函数:
//列表减去它的第一个节点:
largest->接下来= sortlist中(largest->下面);
最大的回报;
}

I'm trying to sort a linked list by finding the largest value, deleting it from its position, and then inserting it at the top of the list.

The difficulty I'm running into is the actual deleting and inserting at the top. The issue seems to be in the if condition in the while loop contained within the sortList function, but I'm not sure how to fix it.

Any help would be appreciated.

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

typedef struct node{
    int num;
    struct node *next;
} Node, *NodePtr;

void printList(NodePtr np);
NodePtr makeList(void);
NodePtr makeNode(int n);
NodePtr sortList(NodePtr list);

int main(void) {
    NodePtr list;
    printf("Enter numbers for the list (0 to end)\n");
    list = makeList();
    printList(list);
    list = sortList(list);
    printList(list);
    return 0;
}

NodePtr makeList(void) {
    NodePtr makeNode(int), np, top, last;
    int n;
    top = NULL;
    if(scanf("%d", &n) != 1)n = 0;
    while(n != 0) {
        np = makeNode(n);
        if(top == NULL)top = np;
        else last->next = np;
        last = np;
        if(scanf("%d", &n)!=1)n=0;
    }
    return top;
}


void printList(NodePtr np) {
    while(np != NULL) {
        printf("%d\n", np->num);
        np = np->next;
    }
}

NodePtr makeNode(int n) {
    NodePtr np = (NodePtr)malloc(sizeof(Node));
    np->num = n;
    np->next = NULL;
    return np;
}

NodePtr sortList(NodePtr list) {
    NodePtr top = list;
    NodePtr curr = NULL;
    NodePtr largest;
    NodePtr prev;
    prev = NULL;
    curr = top;
    largest = top;

    while(curr != NULL) {
        prev = curr;
        if(curr->num > largest->num) {
            largest = curr;
            prev->next = curr->next;
            largest->next = top;
        }
        curr = curr->next;
    }
    if(prev == NULL) {
        largest->next = top;
        return largest;
    }
    return largest;
}

There is issues in the sortList function.

This function only put some large nodes in the beginning of the list. It is not soting all the list. you can you a sort algorithm to sort the file : quicksort/ bubblesort/... i put a code doing a sort in the end of this answer.

here is a code doing the sort of the list :

//it is replacing largest node with first one then doing the same operation with sublist (list-first element)

NodePtr sortList(NodePtr list) 
{

// 
if(list == null || list->next == null)
    return list; // the list is sorted.

//replace largest node with the first : 

//1- find largest node : 
NodePtr curr, largest,largestPrev;
curr = list;
largest = list;
prev = list;
largestPrev = list;
while(curr != NULL) {
        if(curr->num > largest->num) {
            largestPrev = prev;
            largest = curr;
        }
        prev = curr;
        curr = curr->next;

    }
//largest node is in largest. 

//2- switching firt node and largest node : 
NodePtr tmp;
if(largest != list)
{
    largestPrev->next = list;
    tmp = list->next;
    list->next = largest->next;
    largest->next = tmp;
}

// now largest is the first node of the list.

// calling the function again with the sub list :
//            list minus its first node :
largest->next = sortList(largest->next);


return largest;
}