In: Computer Science
Answers will be given extra reward
Convert the following SINGLYLINKEDLIST code into DOUBLYLINKEDLIST code. Such that
struct Node {
int element;
structNode*next;
structNode*prev;
}
struct Node *head = NULL;
structNode*tail =NULL;
The SINGLYLINKEDLIST code is as follows:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int element;
struct Node *next;
};
void get(struct Node *head, int pos)
{
struct Node *temp;
temp = head;
if(head == NULL)
{
printf("List is empty!\n");
}
for(int i=0; i < pos; i++)
temp = temp->next;
printf("The element is %d\n", temp->element);
}
void printList(struct Node *head)
{
struct Node *temp = head;
if(head == NULL)
printf("The list is empty\n");
else
{
printf("\nList: ");
while(temp != NULL)
{
printf("%d ", temp->element);
temp = temp->next;
}
}
}
struct Node * removeElement(struct Node *head, int
position)
{
struct Node *temp, *prev;
temp = head;
prev = NULL;
if(head == NULL)
printf("The list is empty!\n");
else
{
for(int i = 0; i < position; i++)
{
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
free(temp);
}
return head;
}
struct Node *reverseList(struct Node *head)
{
struct Node *temp, *prev, *next;
temp = head;
prev = NULL;
next = NULL;
while(temp != NULL)
{
next = temp->next;
temp->next = prev;
prev = temp;
temp = next;
}
head = prev;
return head;
}
struct Node * insertLast(struct Node *head, int element)
{
struct Node *newNode, *temp;
temp = head;
newNode = (struct Node *) malloc(sizeof(struct Node));
newNode->element = element;
newNode->next = NULL;
if(head == NULL)
head= newNode;
else
{
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
return head;
}
int sizeList(struct Node *head)
{
struct Node *temp = head;
int sizeL = 1;
if(head == NULL)
return 0;
else
{
while(temp->next != NULL)
{
temp = temp->next;
sizeL++;
}
}
return sizeL;;
}
int main(int argc, const char * argv[]) {
struct Node *head = NULL;
head = insertLast(head, 1);
head = insertLast(head, 2);
head = insertLast(head, 3);
head = insertLast(head, 4);
head = insertLast(head, 5);
head = insertLast(head, 6);
printList(head);
get(head, 2);
head = removeElement(head, 2);
printList(head);
get(head, 2);
printf("\nThe size of the list is %d\n", sizeList(head));
head = reverseList(head);
printList(head);
return 0;
}
//comment has been added at all the places where the code was modified
#include <stdio.h>
#include <stdlib.h>
struct Node{
int element;
struct Node *next;
struct Node *prev; //prev was added
};
void get(struct Node *head, int pos)
{
struct Node *temp;
temp = head;
if(head == NULL)
{
printf("List is empty!\n");
}
for(int i=0; i < pos; i++)
temp = temp->next;
printf("The element is %d\n", temp->element);
}
void printList(struct Node *head)
{
struct Node *temp = head;
if(head == NULL)
printf("The list is empty\n");
else
{
printf("\nList: ");
while(temp != NULL)
{
printf("%d ", temp->element);
temp = temp->next;
}
}
}
struct Node * removeElement(struct Node *head, int position)
{
struct Node *temp, *prev;
temp = head;
prev = NULL;
if(head == NULL)
printf("The list is empty!\n");
else
{
for(int i = 0; i < position; i++)
{
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
temp->next->prev=prev; //prev of node which is next to removed node will point to prev(node befor the node which is removed)
free(temp);
}
return head;
}
struct Node *reverseList(struct Node *head)
{
struct Node *temp, *temp1;
temp = head;
temp1 = NULL;
while(temp != NULL) // this code was modified(we are swapping two nodes )
{
temp1 = temp->prev;
temp->prev = temp->next;
temp->next = temp1;
temp = temp->prev;
}
if(temp1!=NULL) //checking if list is empty or has only one node
head = temp1->prev;
return head;
}
struct Node * insertLast(struct Node *head, int element)
{
struct Node *newNode, *temp;
temp = head;
newNode = (struct Node *) malloc(sizeof(struct Node));
newNode->element = element;
newNode->next = NULL;
if(head == NULL)
head= newNode;
else
{
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev=temp; //prev of last node will point to second last node
}
return head;
}
int sizeList(struct Node *head)
{
struct Node *temp = head;
int sizeL = 1;
if(head == NULL)
return 0;
else
{
while(temp->next != NULL)
{
temp = temp->next;
sizeL++;
}
}
return sizeL;;
}
int main(int argc, const char * argv[]) {
struct Node *head = NULL;
head = insertLast(head, 1);
head = insertLast(head, 2);
head = insertLast(head, 3);
head = insertLast(head, 4);
head = insertLast(head, 5);
head = insertLast(head, 6);
printList(head);
get(head, 2);
head = removeElement(head, 2);
printList(head);
get(head, 2);
printf("\nThe size of the list is %d\n", sizeList(head));
head = reverseList(head);
printList(head);
return 0;
}