Exemplo n.º 1
0
struct node * reverse_list_recursive(struct node *T) {
  if( !T || !T->next ) {
    return T;
  }
  struct node *temp = reverse_list_recursive(T->next);
  T->next->next = T;
  T->next = NULL;
  return temp;
}
Exemplo n.º 2
0
void reverse_list_recursive(Node **root){
    Node *first;
    Node *rest;
    if(*root == NULL) return;
    first = *root;
    rest = first->next;
    if(rest == NULL)
        return ;
    reverse_list_recursive(&rest);
    first->next->next = first;
    first->next = NULL;
    *root = rest;
}
Exemplo n.º 3
0
main() {
  struct node *T = NULL;;
  int i = 0, n = 0, val = 0;

  printf("Number of values: ");
  scanf("%d", &n);
  
  for(i = 0; i < n; i++) {
    scanf("%d", &val);
    T = insert_element(T, val);
  }
  print_list(T);
  T = reverse_list(T);
  print_list(T);
  T = reverse_list_recursive(T);
  print_list(T);
}
Exemplo n.º 4
0
int main(){
    Node *root = createNode(1);
    Node *n1 = createNode(2);
    Node *n2 = createNode(3);
    Node *n3 = createNode(4);
    Node *n4 = createNode(5);
    Node *n5 = createNode(6);
    Node *n6 = createNode(7);
    root->next = n1;
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    n5->next = n6;
    n6->next = NULL;
    print_list(root);
    reverse_list_recursive(&root);
    print_list(root);
    //Node *rev = reverse_list_iter(root);
    //print_list(rev);

    return 0;
}
Exemplo n.º 5
0
int main (void)
{
    system ("clear");
    node_t *head = NULL;
    int choice = 0;

    int n;

    while (TRUE)
    {
	print_menu();
	scanf("%d", &choice);

	switch(choice)
	{
	    case 1:
		head = insert_rear(head);
		view_list(head);
		break;
	    case 2:
		head = insert_front(head);
		view_list(head);
		break;
	    case 3:
		head = delete_rear(head);
		view_list(head);
		break;
	    case 4:
		head = delete_front(head);
		view_list(head);
		break;
	    case 5:
		print_reverse(head);
		printf("\n");
		break;
		//	    case 6: 
		//		link_sort1(head);
		//		view_list(head);
		//		break;
	    case 7:
		head = reverse_list_iterative(head);
		view_list(head);
		break;
	    case 8:
		head = reverse_list_recursive(head);
		view_list(head);
		break;
	    case 9:
		head = swap_alternate(head);
		view_list(head);
		break;
	    case 10:
		head = insert_sorted(head);
		view_list(head);
		break;
	    case 11:
		printf ("Enter the Value of n: ");
		scanf ("%d", &n);
		head = reverse_n_list(head, n);
		view_list(head);
		break;

	    case 100:
		view_list(head);
		break;
	    default : printf("Invalid Choice\n");
	}
    }
    return 0;
}