Difference between pages "3.39" and "3.41"

From The Algorithm Design Manual Solution Wiki
(Difference between pages)
Jump to navigation Jump to search
(Created page with "== Iterative version with three variables == <pre> Node* ReverseList( Node * List ) { Node *temp1 = *List; Node * temp2 = NULL; Node * temp3 = NULL; while ( temp...")
 
(Created page with "Create a count sort in magazine until you find all the characters of given string. --Max 06:49, 25 June 2010 (EDT) Back to Chapter 3")
 
Line 1: Line 1:
== Iterative version with three variables ==
+
Create a count sort in magazine until you find all the characters of given string.
<pre>
+
--[[User:Max|Max]] 06:49, 25 June 2010 (EDT)
Node* ReverseList( Node * List )     
 
{
 
  Node *temp1 = *List;
 
  Node * temp2 = NULL;
 
  Node * temp3 = NULL;
 
  while ( temp1 )
 
  {
 
        *List = temp1; //set the head to last node         
 
        temp2= temp1->pNext; // save the next ptr in temp2
 
        temp1->pNext = temp3; // change next to privous
 
        temp3 = temp1;
 
        temp1 = temp2;
 
  }
 
  return *List;
 
}
 
</pre >
 
 
 
== Iterative version with one variable ==
 
 
 
<pre>
 
Node* reverse (node* head)
 
{
 
    node *temp=NULL;
 
    while(true)
 
    {
 
        if(head->next==NULL)
 
        {
 
            head->next=temp;
 
            break;
 
        }
 
        // Swap(head->next, temp)
 
        head->next = (node *)((unsigned long) head->next ^(unsigned long) temp);
 
        temp = (node *)((unsigned long) head->next ^(unsigned long) temp);
 
        head->next =(node *) ((unsigned long) head->next ^ (unsigned long)temp);
 
       
 
        // Swap(head, temp)
 
        head = (node *)((unsigned long) head ^(unsigned long) temp);
 
        temp =(node *) ((unsigned long) head ^(unsigned long) temp);
 
        head =(node *) ((unsigned long) head ^ (unsigned long)temp);
 
       
 
    }
 
}
 
</pre>
 
 
 
 
 
== Recursive Version ==
 
 
 
<pre>
 
Node* Reverse(Node*head)
 
{
 
  Node* temp=NULL;
 
  if(head==NULL)
 
  {
 
      return NULL;
 
  }
 
  if(head->next==NULL)
 
      return head;
 
  temp=Reverse(head->next);
 
  head->next->next=head;
 
  head->next=NULL;
 
  return temp;
 
}
 
</pre>
 
--[[User:Max|Max]] 07:36, 16 June 2010 (EDT)
 
 
 
== Recursive Version 2 (Java) ==
 
 
 
<pre>
 
void reverseList(){
 
head.reverse(null);
 
Node temp=head;
 
head=tail;
 
tail=temp;
 
}
 
 
 
public void reverse(Node previousNode){
 
if(next!=null)
 
next.reverse(this);
 
next=previousNode;
 
}
 
</pre>
 
 
 
== Recursive Version 3 (C++) ==
 
 
 
<pre>
 
struct node{
 
  int data;
 
  node* next;
 
};
 
 
 
node* new_head; // store the new head after calling reverse_sll function
 
 
 
void reverse_sll(node* current_node, node* prev_node=NULL){
 
  if(current_node==NULL)
 
      new_head = prev_node;
 
  else{
 
      reverse_sll(current_node->next, current_node);
 
      current_node->next=prev_node;
 
  }
 
}
 
</pre>
 
  
  
 
Back to [[Chapter 3]]
 
Back to [[Chapter 3]]

Latest revision as of 18:14, 20 September 2020

Create a count sort in magazine until you find all the characters of given string. --Max 06:49, 25 June 2010 (EDT)


Back to Chapter 3