LL
LL
h>
typedef struct node
{
int info;
struct node *next;
}node;
node *first=NULL;
void insertend()
{
int x;
node *newnode,*temp1;
newnode=(node *)malloc(sizeof(node));
temp1=(node *)malloc(sizeof(node));
printf("\nenter the element to insert/");
scanf("%d",&x);
newnode->info=x;
newnode->next=NULL;
temp1=first;
while((temp1->next)!=NULL) //searching for last node
{
temp1=temp1->next;
}
temp1->next=newnode; //connecting the newnode
}
void insertbegin()
{
int x;
node *newnode;
newnode=(node *)malloc(sizeof(node)); //allocate space for node
printf("\nenter the element to be inserted");
scanf("%d",&x);
newnode->info=x;
newnode->next=first;
first=newnode;
}
void insertloc()
{
int loc,x,i;
node *temp1,*newnode;
temp1=(node *)malloc(sizeof(node));
newnode=(node *)malloc(sizeof(node));
printf("enter the element to insert");
scanf("%d",&x);
printf("\nenter the location to insert");
scanf("%d",&loc);
newnode->info=x;
if(loc==1)
{
newnode->next=first;
first=newnode;
}
else
{
temp1=first;
for(i=1;i<(loc-1);i++)
{
temp1=temp1->next;
}
if(temp1==NULL)
{
printf("list is empty");
}
else
{
newnode->next=temp1->next;
temp1->next=newnode;
}
}
}
void deletebegin()
{
if(first==NULL)
printf("\n no nodes are available");
else if(first->next==NULL) //if only one is existing
{
printf("\nThe deleted eleemnt is %d",first->info);
first=NULL;
}
else
{
printf("\n The deleted element is %d",first->info);
first=first->next; ///just changing first pointer ,so that it points to next
node
}
}
void deleteend()
{
node *temp1,*pred;
temp1=(node *) malloc(sizeof(node));
pred=(node *) malloc(sizeof(node));
if(first==NULL)
printf("\n Deletion is not possible"); //list is empty
else if(first->next ==NULL)// only one node is there in the list
{
printf("\n the deleted elemnt is %d",first->info);
first=NULL;
}
else
{
temp1=first;
while(temp1->next !=NULL)
{
pred=temp1;
temp1=temp1->next;
}
pred->next=NULL;
free(temp1);
}
void deleteatloc()
{
node *temp1,*pred;
int loc,i;
temp1=(node *)malloc(sizeof(node));
pred=(node *)malloc(sizeof(node));
if(first==NULL)
printf("\nlist is empty deleetion is not possible");
else
{
printf("\n enter the location of node to be deleted");
scanf("%d",&loc);
if(loc==1)
{
printf("\n deleted element id %d",first->info);
first=first->next;
}
else
{
temp1=first;
for(i=1;i<loc;i++)
{
pred=temp1;
temp1=temp1->next;
}
printf("\nDeleted node info is %d",temp1->info);
pred->next=temp1->next;
free(temp1);
}
}
}
void count()
{
node *temp;
int count;
temp=(node *) malloc(sizeof(node));
temp=first;
if (first==NULL)
printf("\nlist is empty") ;
else
{
count=1;
while(temp->next!=NULL)
{
count++;
temp=temp->next;
}
printf("\nNumber of nodes in the list are %d",count);
}
}
void search(int srch)
{
node *temp;
int pos=0,found=0;
temp=(node * )malloc(sizeof(node));
if(first==NULL)
printf("list is empty");
else
{
temp=first;
while(temp->info)
{
pos++;
if(temp->info==srch)
{
found=1;
break;
}
temp=temp->next;
}
if (found==1)
printf("element %d found at position %d",srch,pos);
}
void display()
{
node *temp1;
temp1=(node *)malloc(sizeof(node));
temp1=first;
if (first==NULL )
printf("list is empty");
else
{
printf("\nthe contents of list are:");
while(temp1)
{
printf("%d\t",temp1->info);
temp1=temp1->next;
}
}
}
void main()
{
int ch,s,srch;
clrscr();
while( ch<10)
{
printf("\n 1--insertbegin\n");
printf("\n 2--insertend\n");
printf("\n 3--insert at given location");
printf("\n 4--delete from beginning");
printf("\n 5--delete from end" );
printf("\n 6--delete a node at given location");
printf("\n 7--count number of nodes ");
printf("\n 8--searching the node ");
printf("\n 9--display");
printf("\n enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : insertbegin();
break;
case 2:
insertend();
break;
case 3:insertloc();
break;
case 4: deletebegin();
break;
case 5: deleteend();
break;
case 6: deleteatloc();
break;
case 7 : count();
break;
case 8 :
printf("\n entter the element to serach in the list");
scanf("%d",&srch);
search(srch);
// printf("\n position returned%d",s);
// if(s>0)
//printf("eleemnt is found at %d",s );
//else
//printf("element is not found");
break;
case 9:
display();
break;
}
} }