Created
March 9, 2019 16:16
-
-
Save luk0y/8a32e4f3fb2c980cf999609cd71bc6b3 to your computer and use it in GitHub Desktop.
Reversing of a given linked list elements
This file contains 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
//By luk0y... | |
#include<stdio.h> | |
#include<stdlib.h> | |
struct node | |
{ | |
int data; | |
struct node *adr; | |
}; | |
void main() | |
{ | |
struct node *start=NULL,*nn,*ptr; | |
int x,y,c=0; | |
printf("Enter how many nodes you want : "); | |
scanf("%d",&y); | |
int k=y; | |
int j=y; | |
int i; | |
printf("Enter the values : \n"); | |
while(y!=0) | |
{ | |
scanf("%d",&x); | |
nn=(struct node*) malloc(sizeof(struct node)); | |
if(nn==NULL) | |
{ | |
printf("no space"); | |
} | |
else | |
{ | |
if(start==NULL) | |
{ | |
start=nn; | |
nn->data=x; | |
nn->adr=NULL; | |
} | |
else | |
{ | |
ptr=start; | |
while(ptr->adr!=NULL) | |
{ | |
ptr=ptr->adr; | |
} | |
ptr->adr=nn; | |
nn->data=x; | |
nn->adr=NULL; | |
} | |
} | |
y--; | |
} | |
ptr=start; | |
printf("New linked list\n"); | |
for(i=1;i<=k;i++) | |
{ | |
if(i==j){ | |
printf("%d\n",ptr->data); | |
i=1; | |
j--; | |
ptr=start; | |
} | |
if(i==k && k!=1){printf("%d",start->data);} | |
ptr=ptr->adr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment