Created
May 8, 2018 12:47
-
-
Save gallirohik/1b167a628f2e52b440ee34e27f483d2e to your computer and use it in GitHub Desktop.
Reverse of a linked list without recursion created by Rohik - https://repl.it/@Rohik/Reverse-of-a-linked-list-without-recursion
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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| struct tnode | |
| { | |
| int data; | |
| struct tnode *link; | |
| }; | |
| typedef struct tnode *tptr; | |
| tptr insert(tptr first,int x) | |
| { | |
| tptr p,t; | |
| p=(tptr)malloc(sizeof(struct tnode)); | |
| p->data=x; | |
| p->link=NULL; | |
| if(first==NULL) | |
| { | |
| first=p; | |
| return first; | |
| } | |
| t=first; | |
| while(t->link!=NULL) | |
| t=t->link; | |
| t->link=p; | |
| return first; | |
| } | |
| void display(tptr first) | |
| { | |
| tptr t; | |
| t=first; | |
| while(t!=NULL) | |
| { | |
| printf("%d ",t->data); | |
| t=t->link; | |
| } | |
| } | |
| tptr reverse(tptr head) | |
| { | |
| tptr prev=NULL,next=NULL,current=NULL; | |
| current=head; | |
| while(current!=NULL) | |
| { | |
| next=current->link; | |
| current->link=prev; | |
| prev=current; | |
| current=next; | |
| } | |
| return prev; | |
| } | |
| int main() | |
| { | |
| int x; | |
| tptr first; | |
| first=NULL; | |
| scanf("%d",&x); | |
| while(x!=-1) | |
| { | |
| first=insert(first,x); | |
| scanf("%d",&x); | |
| } | |
| first=reverse(first); | |
| display(first); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment