techpreparation-homepage

Home  Interview Questions  Certifications  Aptitude Questions  Tutorials  Placement Papers  Search  Resume  Soft Skills  Video  Forum  Blog


Technical Interview Questions

Visual Basic Interview Question
ASP .NET Interview Questions
C++ Interview Questions
C Interview Questions
                              .........More

Programming Source Codes
C/C++ Source Codes
C# Source Codes
                              .........More

Aptitude Interview Questions
C/C++ Aptitude Questions
C Aptitude Questions
                              .........More

Tutorials
C Tutorial
C++ Tutorial
                              .........More

Soft Skills
Communication Skills
Leadership Skills
                              .........More

 

 

  

C/C++ Source Codes

link list reversal/ to reverse a link list using iteration as well as recursion

this program first generates the singular link list, and can
1.) reverse the linked list using iteration method
2.) reverse the linked list using recursion method
3.) create link list
4.) display the link list.
*/

#define null NULL
#include<stdio.h>

//-------------------------------------------------------------
// definition of the node
//-------------------------------------------------------------
typedef struct link {
int i;
struct link *next;
}node;

//-------------------------------------------------------------
// link list creation module
//-------------------------------------------------------------
node* create(node* head, int n) {

node* temp, *q;
int i = 0;

// if head is empty
if(!head) {
printf("head is null");
temp = (node*)malloc(sizeof(node));
head = temp;
temp->next = null;
temp->i = 0;
q = head;
}
else { // head is not empty
printf("\nhead is not null");
q = head;
while(q->next != null)
q = q->next;
}

for(i = n; i < n + 5; i++) {
temp = (node*)malloc(sizeof(node));
temp->next = null;
temp->i = i+1;
q->next = temp;
q = temp;
}

return head;
}

//-------------------------------------------------------------
// module to show the given link list head
//-------------------------------------------------------------

void show(node* head) {
while(head != null) {
printf("%d ",head->i);
head = head->next;
}

}
//-------------------------------------------------------------
// reverse the link list using the loop (iteration)
//-------------------------------------------------------------
node* reverse_loop(node* head) {
node *curr, *frwd, *rev;
if(!head)
return null;
rev = curr = null;
frwd = head;
while (frwd != null) {
rev = curr;
curr = frwd;
frwd = frwd->next;
curr->next = rev;
}
return curr;

}

//-------------------------------------------------------------
// reverse the link list using the recursion
//-------------------------------------------------------------
node* reverse_rec(node* head) {

node* temp = null;
if(head->next) {
temp = reverse_rec(head->next);
head->next->next = head;
head->next = null;
return temp;
}
else
return head;
}

void free_linklist(node* head) {
node* temp = null;
while(!head->next) {
temp = head;
head = head->next;
free(temp);
}
}
//-------------------------------------------------------------
// main fxn
//-------------------------------------------------------------
int main() {

node *head = null;

// creation of the link list when head is null(empty)
head = create(head, 0);
// appending to the link list
head = create(head, 10);
printf("\nlink list contains: \n");
show(head);

// reverse the link list using iterations/ loops
head = reverse_loop(head);
printf("\nlink list after reversion contains: \n");
show(head);

// reverse the link list by using recursion
head = reverse_rec(head);
printf("\nlink list after reversion contains: \n");
show(head);

printf("\nfree the link list...");
free_linklist(head);

puts("");
return 0;
}

//-------------------------------------------------------------
// End of the Program..
//-------------------------------------------------------------


<<<----- Return to C/C++ Source Code Questions Page.


Have a Question ? post your questions here. It will be answered as soon as possible.

Check Object Oriented Interview Questions for more Object Oriented Interview Questions with answers

Check Data Structure Interview Questions for more data structure interview questions with answers