Linkedlist实现

时间:2014-10-29 13:00:31

标签: c linked-list

我正在尝试这个程序,要求我们以各种方式改变一个词。

例如,如果" MISSISSIPPI"给了我们 那么输出应该是

MISP(不重复的发生顺序)

ISPM(频率)

IMPS(按字母顺序排列)

我能够按字母顺序排序,也可以编码出现的顺序。我能够成功运行字母功能,但代码排序在遇到订单功能时挂起,在CODEBLOCKS上。

void ord()
{
    current = head1 ;
    while(current != NULL)
    {
      current1 = current -> next ;
      while(current1 != NULL)
      {
        if(current1 -> data == current -> data)
        {
           free(current1);
           current1 = current1 -> next ;
        }
        else
        current1 = current1 -> next ;
      }
      current = current -> next ;
    }
   ptr = head1 ;
   while(ptr != NULL)
   {
       printf("%c" , ptr->data) ;
       ptr = ptr -> next ;
   }
}

在此函数中,当前指向列表的头部,而当前指向下一个头部。我增加当前的一个并释放具有重复字母表的节点。 我的查询是为什么代码必须停止?还建议一些频率事物的逻辑。

提前致谢

2 个答案:

答案 0 :(得分:3)

我猜问题就在这里。

if(current1 -> data == current -> data)
    {
       free(current1);
       current1 = current1 -> next ;
    }

在这里你释放current1然后推进它。

我的建议是你应该使用一个临时指针来保存“current1”的位置然后推进它或者需要什么。

答案 1 :(得分:1)

MISP(不重复的发生顺序)

错误是:

if(current1 -> data == current -> data)
{
    free(current1); // use a temporary varaible and move to next node and free
    current1 = current1 -> next ;
}

ISPM(频率):快速的想法。

采用26个大小的数组(字母为26.例如:count[26]

  • 遍历链表增加相应的字母元素。
  • 在遍历结束时,您将获得数组中出现的次数。

对于元素A - >>增量A [0] ++;