簡體   English   中英

鏈表實現

[英]Linkedlist implementation

我正在嘗試這個程序,要求我們以各種方式更改單詞。

例如,如果給我們“ 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 ;
   }
}

在此功能中,當前指向列表的開頭,而當前一個指向列表的下一個。 我增加當前值一並釋放具有重復字母的節點。 我的查詢是為什么代碼必須停止? 也為頻率事物提出一些邏輯。

提前致謝

我猜問題出在這里。

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

在這里,您要釋放current1,然后進行推進。

我的建議是,您應該使用一個臨時指針來保存“ current1”的位置,然后將其前進或進行任何其他操作。

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] ++;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM