Created
November 20, 2016 14:07
-
-
Save mehmetakifakkus/648361299c1239b5230d8668ea79861d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example program | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
struct Node | |
{ | |
int data; | |
struct Node *next; | |
}; | |
void ReversePrint(Node *head) | |
{ | |
Node* actualhead=head; | |
int count; | |
while (head) { /*count=number of elements*/ | |
count=0; | |
head=head->next; | |
count++; | |
} | |
head=actualhead; | |
int arr[count]; | |
for (int i=0; i<count; i++) { //creating array in normal order | |
arr[i]=head->data; | |
head=head->next; | |
} | |
for (int i=count-1; i>=0; i--) { | |
cout<<arr[i]<<endl; | |
} | |
} | |
void print(Node *head){ | |
Node *p = head; | |
while(p){ | |
cout<<p->data<<endl; | |
p = p->next; | |
} | |
} | |
void reversePrint(Node *head){ // Here recursion is used for reverse ordering. | |
if(head == 0) | |
return; | |
else | |
reversePrint(head->next); | |
cout<<head->data << endl; | |
} | |
int main() | |
{ | |
Node a,b,c; | |
a.data = 3; | |
b.data = 4; | |
c.data = 5; | |
a.next = &b; | |
b.next = &c; | |
c.next = 0; | |
cout<<"Normal order:"<<endl; | |
print(&a); | |
cout<<"Reverse order:"<<endl; | |
reversePrint(&a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment