简体   繁体   中英

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

MISP (Order of occurrence without repetition)

ISPM (Frequency)

IMPS (Alphabetical order)

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 .

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.

My suggestion is you should use a temporary pointer to hold "current1"'s location and then advance it or what ever is needed.

MISP (Order of occurrence without repetition)

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.

Take an array of 26 size (as alphabets are 26. eg: 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] ++;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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