I want C program code for : Reverse the links of a linked list by traversing only once Input:The input consists of the information in each node of the linked list. Output:The program displays the information in the linked list after the links are reversed. Sample Input:Enter the information in the linked list (Enter -1 to exit): 1020304050-1Sample Output:After the links are reversedInformation in the linked list:5040302010

Questions by souji1425

Showing Answers 1 - 8 of 8 Answers

Ravi

  • Jul 11th, 2006
 

#include <stdio.h>
#include <malloc.h>
#include <conio.h>


struct Node
{
 int a;
 Node *next;
};


Node* ReverseList( Node ** List );

void main()
{

 struct Node *root,*n,*p;

 root=(Node*)malloc(sizeof(Node));
 root->a=10;
 root->next=NULL;
 n=root;
 p=(Node*)malloc(sizeof(Node));
 p->a=20;
 p->next=NULL;
 n->next=p;
 n=p;
 p=(Node*)malloc(sizeof(Node));
 p->a=30;
 p->next=NULL;
 n->next=p;
 n=p;
 p=(Node*)malloc(sizeof(Node));
 p->a=40;
 p->next=NULL;
 n->next=p;
 n=p;
 p=(Node*)malloc(sizeof(Node));
 p->a=50;
 p->next=NULL;
 n->next=p;
 n=p;
 Node *rlist = ReverseList(&root);
 int i=1;
 while(rlist!=NULL){
  printf("element %d =%dn",i++,rlist->a);
  rlist=rlist->next;
 }
 n=root;
 while(n!=NULL){
  p=n->next;
  free(n);
  n=p;
 }
}


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->next; // save the next ptr in temp2

            temp1->next = temp3; // change next to privous

            temp3 = temp1;

            temp1 = temp2;

      }

      return *List;

}

  Was this answer useful?  Yes

bmlal

  • Jul 17th, 2006
 

struct list{ int data; struct list *next;};struct list *mylist=NULL;..........int reverseList(){ struct list *newlist, *temp; if (mylist==NULL) { printf("Please create a listn"); exit (-1); } newlist=mylist; /* Store the address of first node from original list*/ mylist=mylist->next; /* point the list to second node */ newlist->next=NULL; /* To make first node as last node, store NULL */ while (mylist!=NULL) { temp=mylist; /*store the address of next node */ mylist=mylist->next; /* move the list forward */ temp->next=newlist; /* add the node to head of the newlist */ newlist=temp; /* reset new list pointer */ } mylist=newlist; /* reassign the newlist to original list */}........WRITE A main function to SIMULATE ABOVE PROGRAM.

  Was this answer useful?  Yes

chandan

  • Jul 18th, 2006
 

typedef struct nd{

       int info;

     struct nd *node;

                       } node;

/* After creating Linkedlist */

node *reverse(node * h) /* (node * h) means address of the created linkedlist

{

   node *H=NULL,*p;

   while(h!=NULL)

   {

       p=h;

      h=h->next;

     p->next=H;

     H=p;

   }

  return H;

}

  Was this answer useful?  Yes

Navneet

  • Aug 1st, 2006
 

links lists can be reversed in a more elegant way. using recursionnode * reverse ( node * head ) { node *temp; if ( head -> next == NULL ) return head; temp = reverse (head -> next ); head -> next -> next = head; head -> next = NULL; return temp; }

  Was this answer useful?  Yes

Avinash Nishant

  • Sep 8th, 2006
 

void ReverseList()
{
 node *prev, *curr, *next;
 prev=NULL;
 curr=head;
 next=head->next;

 while( NULL != curr )
 {
  curr->next=prev;
  prev=curr;
  curr=next;
  if ( curr != NULL ) next=curr->next;
 }
 head=prev;
}

  Was this answer useful?  Yes

Utkarsh

  • Sep 5th, 2007
 

struct st
{
  int a;
  st *next;
};

rev ( st *ptr)
{

  if(ptr->next !=NULL)
   rev(ptr-next);
  else
  printf("%d",ptr->a);
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions