[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


Write a non-recursive function in ’C’ programming language...

<< Back to general questions

Question by shukoor
Submitted on 10/30/2003
Related FAQ: N/A
Rating: Rate this question: Vote
Write a non-recursive function in ’C’ programming language to reverse a singly linked list In O(N) time using constant extra space.


Answer by Sapan kumar Dutta
Submitted on 3/30/2004
Rating:  Rate this answer: Vote
/* REVERSING A LINKED LIST */
/* LINK_REV.C */

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

struct link
{
   int data;
   struct link *next;
};

int i, number;
struct link *start, *node, *previous, *current1, *counter;

void display(struct link *);
void create_list(struct link *);
struct link * reverse(struct link *);

/* Reversing the list */

struct link * reverse(struct link *start)
{
   current1 = start;
   previous = NULL ;

   while( current1 != NULL )
   {
      counter = (struct link *)malloc(sizeof(struct link));
      counter = current1->next ;
      current1->next = previous ;
      previous = current1 ;
      current1 = counter;
   }

   start = previous;
   return(start);
}

/* Definition of the function */

void  display(struct link *node)
{
   while (node != NULL)
   {
      printf(" %d", node->data);
      node = node->next;
   }
}

/* Definition of the function */

void create_list(struct link *node)
{
   int i;
   int number;

   printf("\n Input the number of nodes you want to create:");
   scanf("%d", &number);

   /* CREATE A LINKED LIST */

   for (i = 0; i < number ; i++)
   {
      printf("\n Input the node: %d: ", i+1);
      scanf("%d", &node->data);
      node->next = (struct link* ) malloc(sizeof(struct link));
      if( i == number - 1)
         node->next = NULL;
      else
         node = node->next;
   }
   node->next = NULL;
}

/* End of function creation */

/* Function main */

void main()
{
   struct link *node;
   struct link *p;

   node = (struct link *) malloc(sizeof(struct link));
   create_list(node);
   printf("\n Original List is as follows:\n");
   display(node);
   p = ( struct link *)malloc(sizeof(struct link));
   p = reverse(node);
   printf("\n After reverse operation list is as follows:\n");
   display(p);
}


 

Answer by gblues
Submitted on 4/4/2004
Rating:  Rate this answer: Vote
#include <stdio.h>

typedef struct link LINK;

struct link {
  int data;
  LINK *next;
};

LINK *reverse(LINK *start)
{
  LINK *l, *l_next, *result = NULL;

  for(l = start; l != NULL; l = l_next)
  {
    l_next = l->next;
    l->next = result;
    result = l;
  }

  return result;
}

 

Answer by lucky
Submitted on 10/8/2004
Rating: Not yet rated Rate this answer: Vote
Why is the memory being allocated in the while loop every time for Counter for list-reversal ?? Is it needed ???

 

Answer by don_semaphore
Submitted on 10/17/2004
Rating: Not yet rated Rate this answer: Vote
template<class T>
void chain<T>::rev()
{
   chainNode<T> *temp=first;
   chain<T> ch;

   for(int i=1;i<=len;i++)
   {
      for(int j=1;j<=len-i;j++)
      {
         temp=temp->link;
      }
      ch.ins(i-1,temp->data);
      temp=first;
   }

   *this = ch;

}

 

Answer by Sridhar
Submitted on 12/2/2005
Rating: Not yet rated Rate this answer: Vote
#include <stdio.h>

typedef struct _tag_node_st node_st;

typedef struct _tag_node_st {
    int data;
    node_st *next;
}node_st;

node_st *reverse_list(node_st *head_node)
{
    node_st *temp_node = NULL;
    node_st *temp_head = NULL;
    
    while(head_node != NULL){
        temp_node = head_node->next;        
        head_node->next = temp_head;
        temp_head = head_node;        
        head_node = temp_node;
    }

    return temp_head;
}

 

Answer by PPP
Submitted on 4/2/2007
Rating: Not yet rated Rate this answer: Vote
TAHI

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to general questions


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.