简体   繁体   English

链表实现

[英]Linkedlist implementation

I was trying this program which asks us to change a word in various ways . 我正在尝试这个程序,要求我们以各种方式更改单词。

For example if "MISSISSIPPI" is given to us then output should be 例如,如果给我们“ MISSISSIPPI”,则输出应为

MISP (Order of occurrence without repetition) MISP(发生顺序,无重复)

ISPM (Frequency) ISPM(频率)

IMPS (Alphabetical order) IMPS(字母顺序)

I was able to do the alphabetical order thing , and could also code for the order of occurrence .I was able to run the alphabetical function successfully, but the code sort of hangs when it encounters order function , on CODEBLOCKS . 我能够执行字母顺序的事情,也可以为出现的顺序编写代码。我能够成功运行字母功能,但是遇到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 ;
   }
}

In this function current points to the head of the list , while current one points to the next of head . 在此功能中,当前指向列表的开头,而当前一个指向列表的下一个。 I increment current one and free the node which has a repeating alphabet . 我增加当前值一并释放具有重复字母的节点。 My query is why the code must be stopping ? 我的查询是为什么代码必须停止? Also suggest some logic for the frequency thing . 也为频率事物提出一些逻辑。

Thanks in advance 提前致谢

I guess the problem lies in here. 我猜问题出在这里。

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

Here you are freeing current1 and then advancing it. 在这里,您要释放current1,然后进行推进。

My suggestion is you should use a temporary pointer to hold "current1"'s location and then advance it or what ever is needed. 我的建议是,您应该使用一个临时指针来保存“ current1”的位置,然后将其前进或进行任何其他操作。

MISP (Order of occurrence without repetition) MISP(发生顺序,无重复)

Error is: 错误是:

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

ISPM (Frequency): Quick idea. ISPM(频率):快速提示。

Take an array of 26 size (as alphabets are 26. eg: count[26] ) 取一个26个大小的数组(因为字母是26。例如: count[26]

  • Increment the corresponding alphabet element by traversing through the linked list. 通过遍历链表增加相应的字母元素。
  • at the end of the traverse you will be having the number of occurrences in the array. 在遍历结束时,您将获得数组中出现的次数。

say for element A -> increment A[0] ++; 对元素A说->增量A [0] ++;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM