#include<stdio.h> #include<stdlib.h> struct node { int data; struct node * next; }; struct node *head=NULL; void display(); void create(); void main() { int choice; do { printf("\n__MENU__\n1.Create List\n2...EXIT...\n"); printf("\nENTER your choice\n"); scanf("%d",&choice); switch(choice) { case 1 :create();break; case 2:printf("~~~~~~~THANK YOU~~~~~~~\n");break; default:printf("INVALID input !!!");break; } }while(choice!=2); } 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); } } 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",&newnode->data); if(head==NULL) { head=newnode; newnode->next=head; } else { while(temp->next!=head) temp=temp->next; temp->next=newnode; newnode->next=head; } } display(); }
|
|
Labels: Circular Linked List