#include<stdio.h> #include<stdlib.h> struct node { int data; struct node * next; }; struct node *head1=NULL,*head2=NULL,*head=NULL; void create(); void display(); void reverse(); void main() { int choice; do { printf("\n__MENU__\n1.Create List\n2.Reverse List\n3...EXIT...\n"); printf("\nENTER your choice\n"); scanf("%d",&choice); switch(choice) { case 1:create();break; case 2:reverse();break; case 3:printf("~~~~~~~THANK YOU~~~~~~~\n");break; default:printf("INVALID input !!!");break; } }while(choice!=3); } void create() { { int x, i,n; head=NULL; struct node* newnode,*temp; temp=head; printf("Enter no. of elements in the LIST \n"); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { temp=head; newnode=(struct node*)malloc(sizeof(struct node)); scanf("%d",&x); newnode->data=x; if(head==NULL) { head=newnode; newnode->next=head; } else { while(temp->next!=head) temp=temp->next; temp->next=newnode; newnode->next=head; } } } } void reverse() { struct node *temp,*newnode, *temp1; if(head==NULL) printf("List is Empty. Create a list to reverse it.!\n"); else { head1=NULL; temp=head; while(temp->next!=head) { temp1=head1; newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=temp->data; if(head1==NULL) { head1=newnode; newnode->next=head1; } else { while(temp1->next!=head1) temp1=temp1->next; newnode->next=temp1->next; temp1->next=newnode; head1=newnode; } temp=temp->next; } newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=temp->data; temp1=head1; while(temp1->next!=head1) temp1=temp1->next; head=head1; temp1->next=newnode; newnode->next=head; head=newnode; printf("\nList reversed !!!!\n"); display(); } } void display() { struct node* temp; temp=head; if(head==NULL) printf("List is empty !!!"); else { printf("List elements are :\t"); while(temp->next!=head) { printf("%d\t",temp->data); temp=temp->next; } printf("%d\n",temp->data); } }
|
|
Labels: Circular Single Linked List