struct node
{
int data;
struct node *next;
}*head,*temp1,*curr;
void insert();
void delete();
void count();
void display();
void insert()
{
struct node *temp;
int val;
temp=(struct node *)malloc(sizeof(struct node));
if (temp==NULL)
{
printf("\nMemory Full!");
return;
}
printf("Enter the number : ");
scanf("%d",&val);
temp->data=val;
temp->next=NULL;
if (head==NULL)
{
head=temp;
return;
}
if (head->next==NULL (head->next!=NULL && temp->data <>data))
{
if (temp->data <>data)
{
temp->next=head;
head=temp;
}
else
head->next=temp;
return;
}
for(curr=head->next,temp1=head;curr!=NULL;curr=curr->next,temp1-temp1->next)
{
if (curr->data > temp->data)
{
temp->next=curr;
temp1->next=temp;
return;
}
}
temp1->next=temp;
return;
}
void delete()
{
int x,found=0;
struct node *temp;
printf("Enter the node to delete : ");
scanf("%d",&x);
if (x==head->data)
{
temp=head;
head=head->next;
free(temp);
found=1;
return;
}
for(temp=head,curr=head->next;curr!=NULL;curr=curr->next,temp=temp->next)
{
if (curr->data==x && found==0)
{
found=1;
temp1=curr;
if (curr->next==NULL)
temp->next=NULL;
else
{
curr=curr->next;
temp->next=curr;
}
free(temp1);
}
}
if (!found)
printf("\n Element not found!");
}
void count()
{
int c=0;
struct node *temp;
for(temp=head;temp!=NULL;temp=temp->next,c++);
printf("\nNumber of nodes : %d",c);
}
void display()
{
struct node *temp;
for(temp=head;temp!=NULL;temp=temp->next)
printf("%d-->",temp->data);
}
main()
{
int wish=1,choice;
head=NULL;
do
{
printf("\n1.Insert\n2.Delete\n3.Count\n4.Display\n5.Exit\n");
printf("Enter Your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert(); break;
case 2: delete(); break;
case 3: count(); break;
case 4: display(); break;
}
}while(choice!=5);
}
No comments:
Post a Comment