#include <stdio.h>
#include <string.h>

typedef struct node{
    struct node *next;
    char *str;
}node;

node *M;

void swap(char *a, char *b){
  char t = *a;
  *a = *b;
  *b = t;
}

void add(char *str){

   if(M == NULL){
      M = (node *)malloc(sizeof(node));
      M->str = (char *)malloc(sizeof(char)*(strlen(str)+1));
      strcpy(M->str, str);
      M->next = NULL;
   }else{
     node *t = M;
     while(t->next != NULL){
        t = t->next;
     }
     node *tmp = (node*)malloc(sizeof(node));
     tmp->str = (char *)malloc(sizeof(char)*(strlen(str)+1));
      strcpy(tmp->str, str);
      tmp->next = NULL;
     t->next = tmp;
   }
}

void print(node *t){
   while(t != NULL){
      printf("%s", t->str);
      if(t->next != NULL)
         printf("->");
      t = t->next;
   }

}
void permute(char *str, int idx){
   char *word = str;

   if(strlen(word)-1 == idx){
      add(word);

   }else{
      int i;
      for(i = idx; i < strlen(word); i++){
         swap(&word[idx], &word[i]);
         permute(word, idx+1);
         swap(&word[i], &word[idx]);
      }
   }
}
main(){
   char str[10] = "123";
   int i =0;
   permute(str, 0);
   node *tmp = M;
   print(tmp);
}

On Tue, Aug 30, 2011 at 10:51 PM, Anup Ghatage <ghat...@gmail.com> wrote:

> So, in short, find all permutations for a given string and create a linked
> list from them?
>
> --
> Anup Ghatage
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to