C Program to Reverse LinkedList

LinkedList
Linked list is a data structure, contains the reference(link) of next record in a sequence

  1. LinkedList is a Dynamic Datastructre, It grows and shrinks during runtime
  2. Insertion and deletion operations are easier and faster
  3. Efficent, Memory Utilization, i,e No need to preallocate Memory
  4. Linear Data Structure such as Stack, Queue can be implemented using LinkedList

C Program to Reverse Single Linked List

reverse(struct node **x)
{
   Struct node *q, *r, *s;
   *q=*x;
   r=NULL;
   while(q!=NULL)
   {
      s=r;
      r=q;
      q=q->link;
      r->link=s;
   }
*x=r;
}

 C Program to Reverse Double LinkedList


void reverse(struct node **head_ref)
{
   struct node *temp = NULL;
   struct node *current = *head_ref;
   while (current != NULL) {
      temp = current->prev;
      current->prev = current->next;
      current->next = temp;
      current = current->prev;
   }
if(temp != NULL )
*head_ref = temp->prev;
}

Related posts:

  1. How to allocate memory for structure pointer using new operator in C++
  2. String Reverse in C

Comments are closed.