GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Placement Papers  >  Aztec Systems  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 5 of 8    Print  
Write a program to reverse a linked list

  
Total Answers and Comments: 3 Last Update: July 10, 2009   
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
July 18, 2006 15:08:11   #1  
chandan        

RE: Write a program to reverse a linked list

**************************

typedef struct nd {

int info;

struct nd*link;

} node;

/* AFTER CREATING LINK LIST */

node*reverse(node *h)

{

node *H NULL *p;

while(h! NULL)

{

p h; /* insert value of 1st node into p */

h h->next;

p->next H; /* insert p->next NULL because H is equals to NULL (1st time) */

H p; /* insert address of p into H */

}

return H;

}


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
August 03, 2008 06:05:38   #2  
pranjal5215 Member Since: July 2008   Contribution: 2    

RE: Write a program to reverse a linked list
this is a complete program to reverse a linkked list in C.
first node is allocated memory but dosent contain any data.

#include<stdio.h>
#include<stdlib.h>
struct list
{
int month;
struct list *next;
};
typedef struct list node;

void init(node* record)
{
record->next NULL;
}
void addnode(node* record int d)
{
node* fresh;
fresh (node *)malloc(sizeof(node));
fresh->month d;
fresh->next record->next;
record->next fresh;
}
void print(node *record)
{
node* temp;
temp (node *)malloc(sizeof(node));
for(temp record->next;temp;temp temp->next)
printf(" d" temp->month);
}
void reverse(node* record)
{
node* temp;node* temp1;node* temp2;
temp (node *)malloc(sizeof(node));
temp1 (node *)malloc(sizeof(node));
temp2 (node *)malloc(sizeof(node));
temp record;temp1 temp->next;temp2 temp1->next;
temp->next->next NULL;
while(temp2! NULL)
{
temp temp1;
temp1 temp2;
temp2 temp1->next;
temp1->next temp;
}
record->next temp1;
}
int main(void)
{
node* start;
node* start1;
start (node *) malloc(sizeof(node));
init(start);
int i 0;
for(i 10;i> 0;i--)
addnode(start i);
print(start);
reverse(start);
printf("n");
print(start);
return 0;
}

 
Is this answer useful? Yes | No
July 10, 2009 06:34:21   #3  
pranjal5215 Member Since: July 2008   Contribution: 2    

RE: Write a program to reverse a linked list
This is the recursive version of the program.


#include<stdio.h>
#include<stdlib.h>
struct list
{
int month;
struct list *next;
};
typedef struct list node;
void init(node* record)
{
record->next NULL;
}
void addnode(node* record int d)
{
node* fresh;
fresh (node *)malloc(sizeof(node));
fresh->month d;
fresh->next record->next;
record->next fresh;
}
void print(node *record)
{
node* temp;
temp (node *)malloc(sizeof(node));
for(temp record->next;temp;temp temp->next)
printf(" d" temp->month);
}

node* reverse_recurse(node* cur node* start)/*reverse linked list recursively*/
{
if(cur->next NULL)
{
start->next cur;
return cur;
}
else
{
reverse_recurse(cur->next start)->next cur;
}
return cur;
}
int main(void)
{
node* start;
start (node *)malloc(sizeof(node));
init(start);
int i 0;
for(i 20;i> 0;i--)
addnode(start i);
print(start);
reverse_recurse(start->next start)->next NULL;
print(start);
printf("n");
return 0;
}

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape