#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; struct node *head=NULL,*head1=NULL,*head2=NULL; void create(); void split(); void print(); void main() { int choice; do { printf("\n__MENU__\n1.Create List\n2.Split List\n3...EXIT...\n"); printf("\nENTER your choice\n"); scanf("%d",&choice); switch(choice) { case 1: create();break; case 2 :split();break; case 3:printf("~~~~~~~THANK YOU~~~~~~~\n");break; default:printf("INVALID input !!!");break; } }while(choice!=3); } void split() { struct node* temp; int pos,i,e=1; temp=head; if(head==NULL) printf("List is empty\n\n"); else { if(temp->next==NULL) printf("list cannot be splitted as only 1 element is present\n"); else { printf("Enter splitting position\n"); scanf("%d",&pos); if(pos>condition()) { e=0; printf("Location Not Found !!!\n"); } else{ for(i=1;i<pos;i++) temp=temp->next; head2=temp->next; temp->next=NULL; } } } if(e!=0) { printf("list 1 :\t"); temp=head; while(temp!=NULL) { printf("%d\t",temp->data); temp=temp->next; } printf("\nlist 2 :\t"); temp=head2; while(temp!=NULL) { printf("%d\t",temp->data); temp=temp->next; } } } 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=NULL; } else { while(temp->next!=NULL) temp=temp->next; temp->next=newnode; newnode->next=NULL; } } print(); } int condition() { int i=0; struct node* temp; temp=head; while(temp!=NULL) { temp=temp->next; i++; } return(i); } void print() { struct node* temp; temp=head; if(head==NULL) printf("list is empty !!!\n\n"); else { printf("\nLIST elements are\t"); while(temp!=NULL) { printf("%d\t",temp->data); temp=temp->next; } } }
|
|
Labels: Single Linked List