从创建一个链表数组和排序的列C [英] Creating a array from a linked list and sorting that array C

查看:108
本文介绍了从创建一个链表数组和排序的列C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,排序链表最好的办法是到链表复制到一个数组和排序的数组。

 的#define SIZE 7000

所以,我的链接列表:

  typedef结构没有{    字符*诺姆;
    诠释计数;
    结构没有* PROX;
}*链接;

我的数组:

  typedef结构MYARRAY
{
    字符名称[141];
    诠释计数;
} MYARRAY;MeuArray v [SIZE]

现在我创建阵列功能:

 无效create_array()
{
链接TMP =头;
INT CONT = 0;
INT I;而(TMP!= NULL)
    {
        的strcpy(V [续] .nome,tmp->诺姆);
        v [续] .Count之间= tmp->计数;
        TMP = tmp-> PROX;
        续++;
    }
对于(i = 0; I<大小;我++)
    的printf(%S%d个\\ N,V [I] .nome,V [I] .Count之间);
}

不知道这是否是正确的。
现在,我不知道这是最好的/最快的。的qsort或其他。
如果的qsort:

  INT比较(结构MeuArray * elem1,结构MeuArray * elem2时)
{
如果(elem1-> COUNT< elem2->计数)
  返回-1;否则,如果(elem1->计数> elem2->计数)
  返回1;其他
{
    如果(的strcmp(elem1->名称,elem2->名称)→1)
        返回1;
    其他
         返回-1;
 }
}

我也试过这种方式(排序我的链接列表):

 无效插入排序(链接电流)
{
链接头=电流;
链接inserP =头;
电流=电流 - > PROX;
而(电流!= NULL)
{
    inserP =头;    而(inserP!=电流)
     {
        如果(inserP->计数>&CURRENT- GT;计数)
        {
            INT TEMP =电流 - >计数;
            电流 - >计数= inserP->计数;
             inserP->计数=温度;
        }
        否则/ *如果(inserP-> COUNT<&CURRENT- GT;计数)* /
            inserP = inserP-> PROX;        /*其他
            {
                如果(STRCMP(inserP->的名字,电流 - >名)大于0)
                {
                    焦炭TEMP2 =的strcpy(TEMP2,电流 - >名);
                    的strcpy(电流 - >的名字,inserP->名);
                    的strcpy(inserP->的名字,TEMP2);
                 }
                 其他
                     inserP = inserP-> PROX;
            } * /
         }
    }
    电流=电流 - > PROX;
}

 链接头= NULL;

任何帮助preciated。

修改

我的qsort我想通过计数则首先比较的名字。
问题是,我只能排序计数。
如何按名称排序后?

code:

  INT比较(常量无效*一,常量无效* B)
{MeuArray * MeuArrayA =(MeuArray *)一个;
MeuArray * MeuArrayB =(MeuArray *)B:如果(MeuArrayB->计数> MeuArrayA->计数)
    返回1;
否则,如果(MeuArrayB-> COUNT< MeuArrayA->计数)
    返回-1;
其他
{
    如果(STRCMP(MeuArrayB->诺姆,MeuArrayA->诺姆))
        返回1;
    其他
        返回-1;
}
}


解决方案

如果要排序的链接列表,那么你就需要将其转换为数组。你可以做到这一点在链接列表中有效地

 ,而(T!= NULL)
 {
  算上++;
  T1 = T1-> PTR;
 }
 对于(i = 0; I<计数;我++)
  {
   T1 =启动;
   为(J = 0; J< COUNT-I-1; J ++)
    {
      如果(T1->信息>(T1-> PTR) - GT;信息)
       {
 INT TEMP = T1->信息;
 T1->信息=(T1-> PTR) - GT;信息;
 (T1-> PTR) - GT;信息=温度;
       }
       T1 = T1-> PTR;
    }
  }

它使用的冒泡排序


更多信息点击这里

Someone told me that the best way to sort a linked list is to copy that linked list into a array and sort that array.

#define SIZE 7000

So my linked list:

typedef struct no{

    char *nome;
    int count;
    struct no * prox;
}*link;

My array:

typedef struct MyArray 
{
    char name[141];
    int count;
}MyArray;

MeuArray v[SIZE];

Now my create array function:

void create_array()
{
link tmp = head;
int cont = 0;
int i;

while (tmp != NULL)
    {
        strcpy(v[cont].nome, tmp->nome);
        v[cont].count = tmp->count;
        tmp = tmp->prox;
        cont++;
    }
for (i = 0; i < SIZE; i++)
    printf("%s %d\n", v[i].nome, v[i].count);
}

Don't know if this is correct. Now I don't know which is the best/fastest. qsort or other. If qsort:

int compare(struct MeuArray *elem1, struct MeuArray *elem2)
{
if ( elem1->count < elem2->count)
  return -1;

else if (elem1->count > elem2->count)
  return 1;

else
{
    if (strcmp(elem1->name, elem2->name) > 1)
        return 1;
    else
         return -1;
 }
}

I also tried this way (sort my linked list):

void insertionSort(link current)
{   
link head = current;
link inserP = head;
current = current->prox;
while (current != NULL)
{
    inserP = head;

    while (inserP != current)
     {
        if (inserP->count > current->count)
        {
            int temp = current->count;
            current->count = inserP->count;
             inserP->count = temp;
        }
        else /* if (inserP->count < current->count) */
            inserP = inserP->prox;

        /*else
            {
                if (strcmp(inserP->name, current->name) > 0)
                {
                    char temp2 = strcpy(temp2, current->name);
                    strcpy(current->name, inserP->name);
                    strcpy(inserP->name, temp2);
                 }
                 else
                     inserP = inserP->prox;
            } */
         }
    }
    current = current->prox;
}

with:

link head = NULL;

Any help apreciated.

EDIT

I in qsort I want to compare first by count then by name. Problem is that I can only sort by count. How to sort by name after?

Code:

int compare (const void * a, const void * b)
{

MeuArray *MeuArrayA = (MeuArray *)a;
MeuArray *MeuArrayB = (MeuArray *)b;

if ( MeuArrayB->count > MeuArrayA->count )
    return 1;
else if ( MeuArrayB->count < MeuArrayA->count )
    return -1;
else
{
    if (strcmp(MeuArrayB->nome, MeuArrayA->nome))
        return 1;
    else
        return -1;  
}
}

解决方案

if you want to sort the link list then you don't need to convert it to array. you can do it in link list in efficient manner

while(t1!=NULL)
 {
  count++;
  t1=t1->ptr;
 }
 for(i=0;i<count;i++)
  {
   t1=START;
   for(j=0;j<count-i-1;j++)
    {
      if(t1->info>(t1->ptr)->info)
       {
 int temp=t1->info;
 t1->info=(t1->ptr)->info;
 (t1->ptr)->info=temp;
       }
       t1=t1->ptr;
    }
  }

which used bubble sort for more info click here

这篇关于从创建一个链表数组和排序的列C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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