9. Create a doubly linked list form the following list of items and delete an item from the middle and end of the doubly linked list.
10, 20, 30, 40,50, 60, 70
Implement the above operations using a C program
Answers
Program is given below.
Explanation:
Refer the attached files for program and its output.
Doubly linked list: Doubly linked list is a type of linked list in which each node contains three fields.
Function for creating the doubly linked list :
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
Function for deleting an item from the middle of the doubly linked list :
void DeleteMiddleNode(int pos)
{
struct node *curNode;
int i;
curNode = stnode;
for(i=1; i<pos && curNode!=NULL; i++)
{
curNode = curNode->nextptr;
}
if(curNode != NULL)
{
curNode->preptr->nextptr = curNode->nextptr;
curNode->nextptr->preptr = curNode->preptr;
free(curNode); //Delete the n node
}
else
{
printf(" The given position is invalid!\n");
}
}
Function for deleting an item from the end of the doubly linked list :
void last_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW\n");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nNode Deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nNode Deleted\n");
}
}