In: Computer Science
For the following two questions you need to provide a code in C 1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node with 3 Delete the node with 10 3. Print the resulting list forward and backwards.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head = NULL;
struct node *last = NULL;
struct node *current = NULL;
bool isEmpty()
{
return head == NULL;
}
void display1()
{
struct node *ptr = head;
while(ptr != NULL)
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
}
void display2()
{
struct node *ptr = last;
while(ptr != NULL)
{
printf("%d ",ptr->data);
ptr = ptr ->prev;
}
}
void insertFirst( int data)
{
struct node *link = (struct node*) malloc(sizeof(struct node));
link->data = data;
if(isEmpty())
{
last = link;
}
else
{
head->prev = link;
}
link->next = head;
head = link;
}
void insertLast( int data)
{
struct node *link = (struct node*) malloc(sizeof(struct node));
link->data = data;
if(isEmpty())
{
last = link;
}
else
{
last->next = link;
link->prev = last;
}
last = link;
}
struct node* deleteFirst()
{
struct node *tempLink = head;
if(head->next == NULL)
{
last = NULL;
}
else
{
head->next->prev = NULL;
}
head = head->next;
return tempLink;
}
struct node* deleteLast()
{
struct node *tempLink = last;
if(head->next == NULL)
{
head = NULL;
}
else
{
last->prev->next = NULL;
}
last = last->prev;
return tempLink;
}
struct node* delete(int key)
{
struct node* current = head;
struct node* previous = NULL;
if(head == NULL)
{
return NULL;
}
while(current->data != key)
{
if(current->next == NULL)
{
return NULL;
}
else
{
previous = current;
current = current->next;
}
}
if(current == head)
{
head = head->next;
}
else
{
current->prev->next = current->next;
}
if(current == last)
{
last = current->prev;
}
else
{
current->next->prev = current->prev;
}
return current;
}
bool insertAfter(int data, int key)
{
struct node *current = head;
if(head == NULL)
{
return false;
}
while(current->data != key)
{
if(current->next == NULL)
{
return false;
}
else
{
current = current->next;
}
}
struct node *newLink = (struct node*) malloc(sizeof(struct node));
newLink->data = data;
if(current == last)
{
newLink->next = NULL;
last = newLink;
}
else
{
newLink->next = current->next;
current->next->prev = newLink;
}
newLink->prev = current;
current->next = newLink;
return true;
}
int main()
{
insertFirst(5);
/*display1();
printf("\n");
display2();
printf("\n");*/
insertFirst(3);
/*display1();
printf("\n");
display2();
printf("\n");*/
insertLast(10);
/*display1();
printf("\n");
display2();
printf("\n");*/
insertAfter(7,5);
/*display1();
printf("\n");
display2();
printf("\n");*/
delete(7);
/*display1();
printf("\n");
display2();
printf("\n");*/
delete(3);
/*display1();
printf("\n");
display2();
printf("\n");*/
delete(10);
display1();
printf("\n");
display2();
}
Stepwise output of code:
Desired output: