Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct list{
- int value;
- struct list* next;
- } node;
- node* reformat(node* first, int number, node* thisnode){
- while(number && thisnode->next){
- thisnode = thisnode->next;
- number--;
- }
- if(first != thisnode){
- node* last = thisnode;
- while(last->next){
- last = last->next;
- }
- node* prev = first;
- node* prev_of_prev = NULL;
- while(prev->next != thisnode){
- prev_of_prev = prev;
- prev = prev->next;
- }
- if(prev_of_prev){
- prev_of_prev->next = thisnode;
- }else{
- first = thisnode;
- }
- prev->next = NULL;
- last->next = prev;
- return reformat(first, number, thisnode);
- }else{
- return first;
- }
- }
- node* add_node(node* list, int value){
- node* prev = NULL;
- node* thisnode = list;
- while(thisnode){
- prev = thisnode;
- thisnode = thisnode->next;
- }
- node* new = malloc(sizeof(node));
- new->next = NULL;
- new->value = value;
- if(prev == NULL) return new;
- prev->next = new;
- return list;
- }
- void printlist(node* list){
- while(list){
- printf("%d ", list->value);
- list = list->next;
- }
- printf("\n");
- }
- int main(void) {
- node* list = NULL;
- for(int i = 0; i < 10; i++){
- list = add_node(list, i);
- }
- printlist(list);
- list = reformat(list, 5, list);
- printlist(list);
- }
Advertisement
Add Comment
Please, Sign In to add comment