#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; struct node *head=NULL; void insbeg(); int condition(); void insLoc(); void print(); void main() { int choice; do { printf("\n__MENU__\n1.Insert at the Location\n2...EXIT...\n"); printf("\nENTER your choice\n"); scanf("%d",&choice); switch(choice) { case 1 :insLoc();break; case 2:printf("~~~~~~~THANK YOU~~~~~~~\n");break; default:printf("INVALID input !!!");break; } }while(choice!=2); } void insLoc() { int x,p,i; struct node* newnode,*temp1,*temp2,*temp; temp1=temp2=head; newnode=(struct node*)malloc(sizeof(struct node*)); printf("enter position\n"); scanf("%d",&p); if(p==1) { insbeg(); } else if (p<=(1+condition())) { printf("Enter element\n"); scanf("%d",&x); newnode->data=x; for(i=1;i<p;i++) { temp1=temp1->next; if(i<p-1) temp2=temp2->next; } newnode->next=temp1; temp2->next=newnode; print(); } else printf("location not found\n"); } void insbeg() { int x; struct node* newnode,*temp; temp=head; newnode=(struct node*)malloc(sizeof(struct node*)); printf("Enter element\n"); scanf("%d",&x); newnode->data=x; if(head==NULL) { head=newnode; newnode->next=NULL; } else { newnode->next=temp; head=newnode; } 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: Data Structure in C, Single Linked List