在C按字母顺序排序链表 [英] Sorting linked list alphabetically in c

查看:166
本文介绍了在C按字母顺序排序链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问问你,如果有可能简单的按字母顺序排序链接列出其名称?我认为这是可能的,但我不知道怎么办。你能帮助我吗?我会很感激。

我pssed应该扫描新的名字,这名添加到链接列表,然后$ P $来按字母顺序排序此列表
Dpressed应显示整个排序列表

Kpressed程序结束

我这样做是与结构数组,它工作得很好,但我不知道该怎么做同样的用链表...

非常感谢你:)

这里是code:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;typedef结构列表{
    字符名称[100];
    结构列表*接下来的;
}清单;诠释的main()
{
    INT I,N,K = 0,V = 0,m为0,J = 0;
    焦炭海峡[100],C;
    LIST * p_first = NULL,* P实际= NULL,* P_ preV = NULL;    而((C =的getchar())!='K')
    {
        如果(C =='我')
        {
            如果(K == 0)
            {
                p_first =(LIST *)malloc的(的sizeof(LIST)); //扫描结构的第一要素
                scanf函数(%S,p_first->名);
                P实际= p_first;
            }
            其他
            {
                p_act->接下来=(LIST *)malloc的(的sizeof(LIST)); //扫描结构的下一个元素
                P实际= p_act->接下来,
                scanf函数(%S,p_act->名);                //这里应该是code到按字母顺序排序文本
        }
        p_act->接着= NULL;
        ķ++;
    }
    否则,如果(C =='D')
    {
        //显示链表中的所有元素        对于(i = 0; I< k;我++){
            如果(我== 0)
                P实际= p_first;
            其他
                P实际= p_act->接下来,
            的printf(%S \\ n,p_act->名);
        }
    }
    }
    的getchar();
    返回0;
}


解决方案

如果你想实际code,你来错了地方。但我可以给你的基本理念。

每当你尝试解决这样的问题,试图找到你需要解决的根本问题较小。例如,(任何种类)排序,需要交换元件的能力,并且,在大多数情况下,要比较的元件。

的比较是pretty直观:你需要调用的strcmp()或类似的元素对。但是,你可能会想这样做,做这件工作,让您的code清洁的功能。

掉期是有点困难。如果你分析的交换过程中,你会看到,这意味着2基本操作:从本列表中的元素和插入在指定位置一个新的。所以,你可能要创建两个功能实现这些操作。我建议你​​impement一个函数删除您提供并插入后,您提供的元素一个接一个元素(列表是一个单一喜欢列表中,这是很难找到之前的元素)。

要删除后的元素,只需要使用以下分配:

  A->接下来=(A->下面) - >接下来

(这样A-> next指向它的邻居后,元素)

要A之后插入A:

  B->接下来= A->接下来
A->接着= B

现在,你有那些2的操作,这是简单的创建一个交换2个元素(实际上,我建议创建后,那些你指明交换2个元素的函数的函数,因此交换(A,B)掉期 - >下一个和B->下一个)。此外,比较函数应该比较您指定的元素之后的元素。

您可以使用我的任何排序算法,上述这些程序,像快速排序或冒泡实现你的结果。

请注意,这仅仅是一个实现的基本思想和你应该考虑在这样的问题的方式有些迹象。我希望这是非常明显的。

I would to ask you, if it is possible to simple sort alphabetically linked list with names? I think that it is possible, but i dont know how. Can you help me with that? I will be very thankful.

"i" pressed should scan new name and add this name to linked list and then to sort this list alphabetically "d" pressed should to display entire sorted list

"k" pressed program ends

I did this with array of struct and it work well, but i dont know how to do same with linked list...

Thank you very much :)

here is code:

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

typedef struct list{
    char name[100];
    struct list *next;
}LIST;

int main()
{
    int i, n, k = 0, v = 0, m = 0, j = 0;
    char str[100], c;
    LIST *p_first = NULL, *p_act = NULL, *p_prev = NULL;

    while((c=getchar())!='k')
    {
        if(c=='i')
        {
            if(k == 0)
            {
                p_first = (LIST *) malloc(sizeof(LIST));  //scan first element of struct
                scanf("%s", p_first->name);
                p_act = p_first;
            }
            else
            {
                p_act->next = (LIST *) malloc(sizeof(LIST));  //scan next element of struct
                p_act = p_act->next;
                scanf("%s", p_act->name);

                //here should be code to sort text alphabetically
        }
        p_act->next = NULL;
        k++;
    }
    else if(c=='d')
    {
        //display all elements of linked list

        for(i = 0; i < k; i++) { 
            if(i == 0)
                p_act = p_first;
            else
                p_act = p_act->next;
            printf("%s\n", p_act->name);
        }
    }
    }
    getchar(); 
    return 0;
}

解决方案

If you want the actual code, you came to the wrong place. But I can give you the basic idea.

Whenever you try to solve a problem like this, try to find the fundamental smaller problems that you need to solve. For example, sorting (of any kind), requires the ability to swap elements and, in most of the cases, to compare elements.

The comparison is pretty intuitive: you need to call strcmp() or similar on pairs of elements. But, you may want to do a function that does this job to keep your code clean.

The swap is a little bit harder. If you analyse the swap procedure, you will see that it implies 2 basic operations: removing an element from the list and inserting a new one at a given position. So, you may want to create 2 functions that implement those operations. I recommend you to impement a function that removes an element after the one you provide and insert after an element you provide (your list being a single-liked list, it is hard to find the element before).

To remove the element after A, just use the following assignment:

a->next=(a->next)->next 

(so A->next points to the element after it's neighbour)

To insert B after A:

B->next = A->next
A->next = B

Now, that you have those 2 operations, it is simple to create a function that swaps 2 elements (actually, I recommend to create a function that swaps 2 elements after those you indicate, so swap(A, B) swaps A->next and B->next). Also, the comparison function should compare elements after elements you indicated.

You could use those procedures I described above with any sorting algorithm, like quicksort or bubblesort to achieve your result.

Note that this is merely the basic idea of an implementation and some indications about the way you should think at a problem like this. I hope it is clear enough.

这篇关于在C按字母顺序排序链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆