Search Results

Search found 1 results on 1 pages for 'user408041'.

Page 1/1 | 1 

  • Rotate a linked list

    - by user408041
    I want to rotate a linked list that contains a number. 123 should be rotated to 231. The function created 23 but the last character stays empty, why? typedef struct node node; struct node{ char digit; node* p; }; void rotate(node** head){ node* walk= (*head); node* prev= (*head); char temp= walk->digit; while(walk->p!=NULL){ walk->digit=walk->p->digit; walk= walk->p; } walk->digit=temp; } How I create the list: node* convert_to_list(int num){ node * curr, * head; int i=0,length=0; char *arr=NULL; head = NULL; length =(int) log10(((double) num))+1; arr =(char*) malloc((length)*sizeof(char)); //allocate memory sprintf (arr, "%d" ,num); //(num, buf, 10); for(i=length;i>=0;i--) { curr = (node *)malloc(sizeof(node)); (curr)->digit = arr[i]; (curr)->p = head; head = curr; } curr = head; return curr; }

    Read the article

1