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

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:" <<...
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
the purpose of the code is to implement a stack how can i convert this code...
the purpose of the code is to implement a stack how can i convert this code in a way that these changes apply to it? // simplify the logic of the main // getline(cin, line) needs to be changed to a normal cin >> line; //make a function for 'list' // not allowed temporary stack (stack s2) //merge the catches (errors) // introduce another function for main // avoid repetitive code here is the code: #include #include #include #include #include...
Convert the attached C++ code to working Java code. Be judicious in the change that you...
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment: #include <iostream> #include <string> using namespace std; class pizza { public: string ingrediants, address; pizza...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT