c program :
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
struct node *create(int item){
struct node *new,*ptr;
new=malloc(sizeof(struct node));
if(new==NULL)
printf("\nLIST OVERFLOW");
else
{
new->data=item;
new->next=NULL;
if(head==NULL){
head=new;
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=new;
}
}
return head;
}
void display(){
struct node *ptr;
if(head==NULL)
printf("\nLIST IS EMPTY");
else
{
ptr=head;
while(ptr!=NULL)
{
printf("%3d",ptr->data);
ptr=ptr->next;
}
}
}
struct node *insertspecific(int loc,int item)
{
struct node *new,*ptr;
int i;
new=malloc(sizeof(struct node));
if (new == NULL)
printf("\nLIST OVERFLOW");
else
{
new->data = item;
new->next = NULL;
if (head == NULL)
{
head = new;
}
if(loc==1)
{
new->next=head;
head=new;
}
else
{
ptr=head;
for(i=1;i<loc-1 && ptr!=NULL;i++)
{
ptr=ptr->next;
}
new->next=ptr->next;
ptr->next=new;
}
}
return head;
}
struct node *reverse()
{
struct node *curr,*pre;
if(head==NULL)
printf("\nLIST EMPTY");
else
{
pre=head;
curr=head->next;
head=head->next;
pre->next=NULL;
while(head!=NULL){
head=head->next;
curr->next=pre;
pre=curr;
curr=head;
}
head=pre;
}
return head;
}
int main(){
int choice,item,l;
do{
printf("\nSLL OPERATIONS");
printf("\n1.CREATE\n2.DISPLAY\n3.SPECIFIC INSERT\n4.EXIT");
printf("\nENTER CHOICE:");
scanf("%d",&choice);
switch(choice){
case 1:printf("\nENTER ITEM TO INSERT:");
scanf("%d",&item);
head=create(item);
printf("\nNODE INSERTED");
break;
case 2:display();
printf("\nREVERSE IS:\n");
head=reverse();
display();
break;
case 3:
printf("\nENTER ITEM TO INSERT:");
scanf("%d", &item);
printf("\nENTER LOC:");
scanf("%d",&l);
head = insertspecific(l,item);
break;
case 4:exit(0);
break;
default:printf("\nwrong choice");
}
}while(choice!=4);
return 0;
}
Comments