c - 链表排序不重复

标签 c linked-list

我想对链表进行排序,我写了这段代码:

courseList *sortList(courseList *list) 
{

// 
if(list == NULL || list->nextCourse == NULL)
    return list; // the list is sorted.

//replace largest node with the first : 

//1- find largest node : 
courseList *curr, *largest,*largestPrev ,*prev;
curr = list;
largest = list;
prev = list;
largestPrev = list;
while(curr != NULL) {

        if( strcmp(curr->courseName,largest->courseName)<0)/*curr->num > largest->num*/ {
            largestPrev = prev;
            largest = curr;
        }
        prev = curr;
        curr = curr->nextCourse;
   }
//largest node is in largest. 

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

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

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


return largest;
}

例如,我的链表是这样的:

MATH101 CSE100 MATH259 BLAW203 MATH101 STAT253 STAT253 MATH259 MATH259 HIST111 STAT253

这就是我得到的:

BLAW203 CSE100 HIST111 MATH101 MATH101 MATH259 MATH259 MATH259 STAT253 STAT253 STAT253

但我想弄清楚如何摆脱这种重复

最佳答案

这将帮助您删除重复的字符串:

courseList *sortList(courseList *list)
{
    //
    if(list == NULL || list->nextCourse == NULL)
        return list; // the list is sorted.

    //replace largest node with the first :

    //1- find largest node :
    courseList *curr, *largest,*largestPrev ,*prev;
    curr = list;
    largest = list;
    prev = list;
    largestPrev = list;
    while(curr != NULL)
    {
            if( strcmp(curr->courseName,largest->courseName)<0)/*curr->num > largest->num*/ {
                largestPrev = prev;
                largest = curr;
            }
            prev = curr;
            curr = curr->nextCourse;
    }
    //largest node is in largest.

    //2- switching first node and largest node :
    courseList *tmp;
    if(largest != list)
    {
        largestPrev->nextCourse = list;
        tmp = list->nextCourse;
        list->nextCourse = largest->nextCourse;
        largest->nextCourse = tmp;
    }

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

    // calling the function again with the sub list :
    //            list minus its first node :


    // *****Changed*******

    courseList *next_node = sortList(largest->nextCourse);

    // Removing Repetition
    while(next_node != NULL)
    {
        if( strcmp(next_node->courseName,largest->courseName)==0)
          next_node = next_node->nextCourse;
        else
          break;
    }
    largest->nextCourse = next_node;

    // *****Changed*******

    return largest;
}

关于c - 链表排序不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659746/

相关文章:

java - 试图计算出链表空指针错误的大小

c - 具有多个 Pthread 的 MPI

c - 在 C 中使用 getopt 检测没有选项(在 Linux 中)

c - 替换运算符 new 和删除似乎会影响 C 库

c - 使用链表的堆栈实现不起作用

java - 将项目添加到 java 链表时遇到问题

c - 使用结构体比较字符串

c - 如何释放带有指针的链表

java - 如何使用java在文本文件中查找和分隔字符串值?

python - python中链表的递归合并排序