Question

In: Computer Science

Answers will be given extra reward Convert the following SINGLYLINKEDLIST code into DOUBLYLINKEDLIST code. Such that...

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;
}

Solutions

Expert Solution

//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;

}



Related Solutions

The following SinglyLinkedList class is available: class SinglyLinkedList: class _Node: """Lightweight, nonpublic class for storing a...
The following SinglyLinkedList class is available: class SinglyLinkedList: class _Node: """Lightweight, nonpublic class for storing a singly linked node.""" __slots__ = 'element', 'next' # streamline memory usage def __init__(self, element, next): # initialize node's fields self.element = element # reference to user's element self.next = next # reference to next node def __init__(self): # initialize list's fields self._head = None # head references to None def printList(self, label): print(label, end=' ') curr = self._head while curr != None: print(curr.element, end="...
Using C++ code, write a program to convert a distance d given in inches (in) to...
Using C++ code, write a program to convert a distance d given in inches (in) to centimeters (cm), where d is input by a user via the keyboard and 1in = 2.54cm. Your submission should include screenshots of the execution of the program using the values 1.5, 4 and 6.75.
Develop the following code in such a manner that it shoulde be having extra 3 attempts(for...
Develop the following code in such a manner that it shoulde be having extra 3 attempts(for wrong answrs) for all questions if the user entered wrong answer · for ex: If the user entered correct answer for first question #then 3 attempts will be carried to next questions. If the user entered 3 times wrong answer in 1st question itself means it sholud display as no more attempts and you got o out of 3 code.................... score=0 total_attempts=3 def question1(total_attempts,score):...
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of characters using Caesar cipher please attach samples run of the code #include <iostream> #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string> #define MAX_SIZE 200 void encrypt_str(char xyz[], int key); // Function prototype for the function to encrypt the input string. void decrypt_str(char xyz[], int key); // Function prototype for the function to decrypt the encrypted string. using namespace std; int main() { char input_str[MAX_SIZE];...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
Convert the following text into a code format: The program reads an unspecified number of integers...
Convert the following text into a code format: The program reads an unspecified number of integers until a zero is entered. While the program reads each number it counts the number of positive numbers and the number of negative numbers that have been entered and sum up the values of the numbers entered. After the user enters zero, the program computes the average of the input values, not counting the zero. At the end, the program displays the average, and...
Convert the following C++ code into MIPS assembely. For testing I will be change the values...
Convert the following C++ code into MIPS assembely. For testing I will be change the values for q,y,x with few different values. //q -> $s0 //y -> $s1 //x -> $s2 int main(){ int q = 5; int y = 17; int x = 77; if ( q < 10){ cout << "inside if"; } elseif ( x > 0 || y < 10) { cout << "inside elseif"; } else { cout << "inside else"; } }
Convert the following to machine code and then back to MIPS instructions: 1) a. addi $s0,...
Convert the following to machine code and then back to MIPS instructions: 1) a. addi $s0, $zero, -15 b. slt $t0, $s0, $s1 c. beq $t0, $zero, LEEQ d. j GRT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT