Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.1 kB
1
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
struct node
{
	int x;
	struct node *next;
};
typedef struct node node;
node *insert_beg(node *);
node *insert_end(node *);
node *insert_spec(node *);
void display(node *);
node *dlt_beg(node *);
node *dlt_end(node *);
node *dlt_target(node *);
void search(node *);
int count(node *);
int main()
{
	int n,c;
	node *start=NULL;
	do{
	printf("\n 1)Insert Node\n 2)Delete Node\n 3)Searching Node\n 4)count number of nodes\n 5)Display\n 6)Exit");
	printf("\n Enter your choice (1-6) :");
	scanf("%d",&n);
	switch(n)
	{
		case 1:printf("\n 1)Insert at begining\n 2)Insert at End\n 3)Insert node at specific location");
		       printf("\n Enter your choice (1-3) :");
		       scanf("%d",&c);
		       switch(c)
		       {
		       	    case 1:start=insert_beg(start); break;
		       	    case 2:start=insert_end(start); break;
		       	    case 3:start=insert_spec(start); break;
		       	    default :printf("\n inavlid option");
			   }
			   break;
	    case 2:printf("\n 1)Deletion from start\n 2)Deletion from End\n 3)Delete node of a specific location");
	           printf("\n Enter your choice (1-3) :");
	           scanf("%d",&c);
	           switch(c)
	           {
	           	    case 1:start=dlt_beg(start); break;
	           	    case 2:start=dlt_end(start); break;
	           	    case 3:start=dlt_target(start); break;
	           	    default :printf("\n Invalid option");
			   }
			   break;
		case 3:search(start); break;
		case 4:printf("\n Number of node=%d",count(start)); break;
		case 5:display(start); break;
		case 6:printf("\n ...Thankyou!..."); break;
		default :printf("\n Invalid choice");
	}
   }while(n!=6);
    return 0;
}
node *insert_beg(node *start)
{
	node *ptr;
	int n;
	ptr=(node *)malloc(sizeof(node));
	printf("\n Enter node value=");
	scanf("%d",&n);
	if(start==NULL)
	{
		ptr->x=n;
		start=ptr;
		ptr->next=NULL;
		return start;
	}
	ptr->x=n;
	ptr->next=start;
	start=ptr;
	return start;
}
node *insert_end(node *start)
{
	node *ptr,*fpt=start;
	int n;
	ptr=(node *)malloc(sizeof(node));
	printf("\n Enter node value=");
	scanf("%d",&n);
	if(start==NULL)
	{
		ptr->x=n;
		start=ptr;
		ptr->next=NULL;
		return start; 
	}
	while(fpt->next!=NULL)
	fpt=fpt->next;
	ptr->x=n;
	fpt->next=ptr;
	ptr->next=NULL;
	return start;
}
node *insert_spec(node *start)
{
	int c=count(start);
	int n,x=1,t;
	node *ptr,*fpt=start,*p=start;
	printf("\n Enter node number you want to insert=");
	scanf("%d",&n);
	if(n>c+1)
	{
		printf("\n %dth node is not avialabe.plz enter valid node number");
		return start;
	}
	printf("\n Enter node value=");
	scanf("%d",&t);
	ptr=(node *)malloc(sizeof(node));
	while(p!=NULL)
	{
		if(x==n)
		{
			ptr->x=t;
			if(x==1)
			{
				ptr->next=start;
				start=ptr;
			}
			else if(x==c+1)
			{
				p->next=ptr;
				ptr->next=NULL;
			}
			else
			{
				ptr->next=fpt;
				p->next=ptr;
			}
			break;
		}
		x++;
		p=fpt;
		fpt=fpt->next;  
	}
	return start;
}
void display(node *start)
{
	node *ptr=start;
	if(start==NULL)
	{
		printf("\n List is Empty");
		return;
	}
	printf("\n List is :");
	while(ptr!=NULL)
	{
		printf("%d ",ptr->x);
		ptr=ptr->next;
	}
	return ;
}
int count(node *start)
{
	node *ptr=start;
	int c=0;
	while(ptr!=NULL)
	{
		c++;
		ptr=ptr->next;
	}
	return c;
}
void search(node *start)
{
	node *ptr=start;
	int n,flag=0;
	if(start==NULL)
	{
		printf("\n List is empty");
		return;
	}
	printf("\n Enter searching node value=");
	scanf("%d",&n);
	while(ptr!=NULL)
	{
		if(ptr->x==n)
		{
			flag=1;
			break;
		}
		else
		ptr=ptr->next;
	}
	if(flag==1)
	printf("\n %d node is found",n);
	else if(flag==0)
	printf("\n node not found");
	return ;
}
node *dlt_beg(node *start)
{
	if(start==NULL)
	{
		printf("\n List is empty");
		return NULL;
	}
	node *ptr=start;
	start=ptr->next;
	free(ptr);
	printf("\n first node is deleted");
	return start;
}
node *dlt_end(node *start)
{
	node *fpt,*ptr=start;
	if(start==NULL)
	{
		printf("\n List is empty");
		return NULL;
	}
	if(ptr->next==NULL)
	{
		free(ptr);
		return NULL;
	}
	while(ptr->next!=NULL)
	{
		fpt=ptr;
		ptr=ptr->next;
	}
	fpt->next=NULL;
	free(ptr);
	printf("\n last node is deleted");
	return start;
}
node *dlt_target(node *start)
{
	node *fpt=start,*ptr=start;
	int n,x=1,flag=0;
	int c=count(start);
	if(start==NULL)
	{
		printf("\n List is empty");
		return NULL;
	}
	printf("\n enter node number that you want to delete=");
	scanf("%d",&n);
	if(n>c)
		{
			printf("\n invalid node number");
			return start;
		}
	while(ptr!=NULL)
	{
	    if(x==n)
		{
			flag=1;
			if(x==1)
			start=ptr->next;
			else if(x==c)
			fpt->next=NULL;
			else
			fpt->next=ptr->next;
			break;
		}
		x++;
		fpt=ptr;
		ptr=ptr->next;
	}
	if(flag==0)
	printf("\n target node is not found");
	else if(flag==1)
	printf("\n %dth node is deleted",n);
	free(ptr);
	return start;
}