Program Linked List
Program Linked List
struct node
{
int data;
struct node *next;
};
struct node *head =NULL;
void deleteBeg();
void deleteEnd();
void deleteAtSpec(int pos);
void travese();
}
}
void insertAtSpec(int ele,int pos)
{
int i;
//node memoery allocation
}
if(temp==NULL)
{
printf("Given node is not found in the list!! insertion not possible\n");
}
else
{
newnode->data = ele;
newnode->next=temp->next;
temp->next=newnode;
printf("insertion successfully at location : %d\n",pos);
}
}
void deleteBeg()
{
struct node *temp;
temp = head;
if(head==NULL)
{
printf("list is empty!! delection not possible..\n");
}
else if(temp->next==NULL)
{
printf("deleted element is:%d\n",temp->data);
temp->next=NULL;
head=NULL;
}
else
{
printf("deleted element is:%d\n",temp->data);
head=temp->next;
free(temp);
}
}
void deleteEnd()
{
struct node *temp1,*temp2;
temp1 = head;
if(head==NULL)
{
printf("list is empty!! delection not possible..\n");
}
else if(temp1->next==NULL)
{
printf("deleted element is:%d\n",temp1->data);
temp1->next=NULL;
head=NULL;
}
else
{
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
printf("deleted element is:%d\n",temp1->data);
temp2->next=NULL;
}
}
void deleteAtSpec(int pos)
{
int i;
struct node *temp1,*temp2;
temp1 = head;
if(head==NULL)
{
printf("list is empty!! delection not possible..\n");
}
else
{
for(i=0;i<pos;++i)
{
temp2=temp1;
temp1=temp1->next;
}
if(temp1==NULL)
{
printf("Delection not possible..\n");
}
else
{
printf("Deleted element is : %d at location :%d\n",temp1->data,pos);
temp2->next=temp1->next;
}
}
}
void travese()
{
}
void main()
{
int choose,ele,pos;
while(1)
{
printf("************* Linked list operations*************\n");
printf(" 1.insert beg \n 2. insert end \n 3. insert at specific \n 4. Delete beg \n 5. Delete End \n 6.
delete at specific \n 7. travese \n 8.exit \n");
printf("Choose one (1-7)");
scanf("%d",&choose);
switch(choose)
{
case 1: printf("enter an element : ");
scanf("%d",&ele);
insertBeg(ele);
break;
case 2: printf("enter an element : ");
scanf("%d",&ele);
insertEnd(ele);
break;
case 3: printf("enter an element : ");
scanf("%d",&ele);
printf("Enter a specific location: ");
scanf("%d",&pos);
insertAtSpec(ele,pos);
break;
case 4: deleteBeg();
break;
case 5: deleteEnd();
break;
case 6:printf("Enter a specific location:");
scanf("%d",&pos);
deleteAtSpec(pos);
break;
case 7:travese();
break;
case 8:exit(0);
break;
default: printf("Invalid choose\n");
}
}
}