#include
#include
struct list
{
int data;
struct list *next;
}*root = NULL;
int count = 0;
void create()
{
int item;
struct list *temp,*node;
temp=(struct list *)malloc(sizeof(struct list));
printf("Enter Value :\n");
scanf("%d",&item);
temp->data = item;
if(root == NULL)
{
root = temp;
root->next = NULL;
count++;
return;
}
node = root;
while(node->next != NULL)
{
node = node->next;
}
node->next = temp;
temp->next = NULL;
count++;
}
void display()
{
struct list *temp;
temp = root;
if(root == NULL)
{
printf("list is empty\n");
return;
}
while(temp!=NULL)
{
printf("\t%d ",temp->data);
temp= temp->next;
}
printf("\n");
}
void insertatbeg()
{
struct list *temp;
temp = (struct list *)malloc(sizeof(struct list));
printf("Enter Value : \n");
scanf("%d",&temp->data);
if(root == NULL)
{
root = temp;
temp->next =NULL;
return;
}
temp->next = root;
root = temp;
count++;
}
void insertatmid()
{
struct list *temp,*node;
int pos,i=0;
temp= (struct list *)malloc(sizeof(struct list));
printf("Enter value: \n");
scanf("%d",&temp->data);
if(root==NULL)
{
printf("list is Empty");
return;
}
node=root;
printf("Enter the position to enter node:");
scanf("%d",&pos);
if(pos>count || pos<0)
{
printf("Enter correct position:\n");
}
while(i<=count)
{
if(i==pos-1)
{
temp->next=node->next;
node->next=temp;
count++;
return;
}
node=node->next;
i++;
}
}
void insertlast()
{
struct list *temp,*node;
temp=(struct list *)malloc(sizeof(struct list));
printf("Enter Value:\n");
scanf("%d",&temp->data);
if(root==NULL)
{
printf("list is empty:\n");
}
node=root;
while(root!=NULL)
{
if(node->next==NULL)
{
node->next=temp;
temp->next=NULL;
count++;
return;
}
else
{
node=node->next;
}
}
}
void deleteatbeg()
{
if(root==NULL)
{
printf("List is Empty");
}
printf("deleted value:%d",root->data);
root=root->next;
count--;
}
void deleteatmid()
{
struct list *temp,*node;
int pos,i=0;
if(root==NULL)
{
printf("list is Empty");
return;
}
node=root;
printf("Enter the position to delete node:");
scanf("%d",&pos);
if(pos>count || pos<0)
{
printf("Enter correct position:\n");
}
while(i<=count)
{
if(i==pos-1)
{
temp=node->next->next;
node->next=temp;
count--;
return;
}
node=node->next;
i++;
}
}
void deletelast()
{
struct list *temp,*node;
if(root==NULL)
{
printf("List is empty:");
return;
}
temp=root;
if(root->next==NULL)
{
printf("deleted item is:%d",node->data);
root=NULL;
return;
}
while(temp->next->next!=NULL)
{
temp=temp->next;
}
printf("deleted data is:%d",temp->next->data);
temp->next=NULL;
}
int main()
{
int ch;
do{
printf("\n1.insert\t2.delete\t3.Display\t4.Exit\t5.insertatbeg\t6.deleteatbeg\t7.insertatmid\t8.deleteatmid\t9.insertlast\t10.deletelast\n");
printf("Enter u r choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
//delete();
break;
case 3:
display();
break;
case 4:
exit(0);
case 5:
insertatbeg();
break;
case 6:
deleteatbeg();
break;
case 7:
insertatmid();
break;
case 8:
deleteatmid();
break;
case 9:
insertlast();
break;
case 10:
deletelast();
break;
default:
printf("Enter Correct vale\n");
}
}while(ch!=4);
}
No comments:
Post a Comment