Last active
July 19, 2019 15:06
-
-
Save Kinjalrk2k/b69c72ac7697f1739d36754791b3e77c to your computer and use it in GitHub Desktop.
Single Circular Linked List (Week2) (C Lab)
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 <malloc.h> | |
typedef struct Node{ | |
int data; | |
struct Node* next; | |
}node; // nickname :) | |
node* createCircularList() | |
{ | |
node* head = NULL, *last; | |
char buff; | |
do{ | |
node* nw = (node*) malloc(sizeof(node)); | |
printf("Enter data: "); | |
scanf("%d", &nw->data); | |
fflush(stdin); | |
nw->next = NULL; | |
if(head == NULL){ | |
head = nw; | |
nw->next = head; | |
last = nw; | |
} | |
else{ | |
last->next = nw; | |
nw->next = head; | |
last = nw; | |
} | |
printf("Do you want to continue? "); | |
buff = getchar(); | |
}while(getchar() != 'n'); | |
return head; | |
} | |
void display(node* head) | |
{ | |
node* ptr = head; | |
do{ | |
printf("%d -> ", ptr->data); | |
ptr = ptr->next; | |
}while(ptr != head); | |
printf("\b\b\b\b \b\b\b\n"); | |
} | |
void display_reverse(node* head, node* curr){ | |
if(curr->next == head){ | |
printf("%d\n", curr->data); | |
return; | |
} | |
display_reverse(head, curr->next); | |
printf("%d\n", curr->data); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
node *head = NULL; | |
head = createCircularList(); | |
display_reverse(head, head); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment