#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; struct node *head=NULL; void create(); void delLoc(); void delBeg(); void print(); int condition(); void main() { int choice; do { printf("\n__MENU__\n1.Create List\n2.Delete from the Location.\n3...EXIT...\n"); printf("\nENTER your choice\n"); scanf("%d",&choice); switch(choice) { case 1: create();break; case 2 :delLoc();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=NULL; } else { while(temp->next!=NULL) temp=temp->next; temp->next=newnode; newnode->next=NULL; } } print(); } void delLoc() { int p,i,counting; struct node*temp1,*temp2; temp1=temp2=head; printf("enter location no. to be deleted\n"); scanf("%d",&p); if(p==1 && head==NULL) { printf("List is empty\n"); } else if(p==1) { delBeg(); } counting=condition(); if(p!=1&&p<=counting) { temp1=temp1->next; for(i=1;i<p-1;i++) { temp1=temp1->next; temp2=temp2->next; } temp2->next=temp1->next; free(temp1); print(); } else if(p!=1) printf("\nlocation NOT FOUND !!!\n"); } int condition() { int i=0; struct node* temp; temp=head; while(temp!=NULL) { temp=temp->next; i++; } return(i); } void delBeg() { struct node*temp; temp=head; if(head==NULL) printf("LIST is empty\n"); else { head=temp->next; free(temp); } if(head!=NULL) print(); } 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: Data Structure in C, Single Linked List