Question

I was practicing program problems without answer.

Not sure on how to write this program and I need help. it needs to be in C language. Thank you!

Assume the node structure is as follows: struct node { int data; struct node next; struct node prev; please write a functio

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*

Following program implemented using DEV C++

you can try with any C compiler also

*/

#include<stdio.h>
//declaration of Node
struct Node {
//consist data data and next and prev address
    int data;
    Node *next, *prev;
};
void removeDuplicates(Node* head)
{
    Node* curNode = head;
    Node* next_next;
    if (curNode == NULL)
        return;

  
    while (curNode->next != NULL) {
        /* Compare curNode node with next node */
        if (curNode->data == curNode->next->data) {

            /* The sequence of steps is important*/
            next_next = curNode->next->next;
          
            curNode->next = next_next;
        }
        else /* This is tricky: only advance if no deletion */
        {
            curNode = curNode->next;
        }
    }
}

void AddNode(Node** addr_head, int data)
{
    //allocate memory for new born node
    Node* new_node = new Node;
// add data to it
    new_node->data = data;
//if list empty
    if (*addr_head == NULL) {
        new_node->next = new_node;
        new_node->prev = new_node;
    }
// add node at end
    else {
    
        Node* last = (*addr_head)->prev;
        new_node->next = *addr_head;
        new_node->prev = last;
        last->next = (*addr_head)->prev = new_node;
    }
    *addr_head = new_node;
}

//called merge recusrsivly till list end
Node* merge(Node* fnode, Node* snode)
{
    // first list is empty
    if (!fnode)
        return snode;

    // second list is empty
    if (!snode)
        return fnode;
//comparing data and calling merge function recurverly
    if (fnode->data < snode->data) {
        fnode->next = merge(fnode->next, snode);
        fnode->next->prev = fnode;
        fnode->prev = NULL;
        return fnode;
    }
  
//comparing data and calling merge function recurverly
    else {
        snode->next = merge(fnode, snode->next);
        snode->next->prev = snode;
        snode->prev = NULL;
        return snode;
    }
}

//mergingg list
Node* Merge(Node* FirstHead, Node* Secondhead)
{
   //fiesr list is empty
    if (!FirstHead)
        return Secondhead;

    //second list is empty
    if (!Secondhead)
        return FirstHead;

   //going till end of list
    Node* last_node;
    if (FirstHead->prev->data < Secondhead->prev->data)
        last_node = Secondhead->prev;
    
    else
        last_node = FirstHead->prev;
// adding null at end
    FirstHead->prev->next = Secondhead->prev->next = NULL;

    //merginng sorted list
    Node* newhead = merge(FirstHead, Secondhead);

    newhead->prev = last_node;
    last_node->next = NULL;
    // removing duplicate if any
    removeDuplicates(newhead);

    return newhead;
}

// method to print all node
void Display_List(Node* head)
{
    Node* nodetemp = head;

    while (nodetemp->next != NULL) {
        printf("%d ",nodetemp->data);
        nodetemp = nodetemp->next;
    }
     printf("%d ",nodetemp->data);
}

int main()
{
    Node *FirstHead = NULL, *Secondhead = NULL;

    // Linked List 1
    AddNode(&FirstHead, 2);
    AddNode(&FirstHead, 55);
    AddNode(&FirstHead, 33);
    AddNode(&FirstHead, 1);

    //Linked List 2
    AddNode(&Secondhead, 11);
    AddNode(&Secondhead, 4);
    AddNode(&Secondhead, 7);
    AddNode(&Secondhead, 2);

    Node* Head = Merge(FirstHead, Secondhead);

    printf("Final Sorted List: ");
    Display_List(Head);

    return 0;
}

/*

output

Final Sorted List: 1 2 7 4 11
--------------------------------
Process exited after 0.06974 seconds with return value 0
Press any key to continue . . .

*/

130 C:\Users\Harsh\Desktop\C++ Program\merge_doubly_linked_list.cpp - Dev-C++ 5.11 File Edit Search View Project Execute Tool

Add a comment
Know the answer?
Add Answer to:
I was practicing program problems without answer. Not sure on how to write this program and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • /* I'm trying to write this program here but my problem is getting the program to...

    /* I'm trying to write this program here but my problem is getting the program to save the user input to "int data" but cannot because it is part of the struct. What do I have to do in order to get this to work? (Language is C++) Write a program that prompts the user to enter numbers on the screen and keep adding those numbers to a linked list. The program stops when user enters -999. Hint: Use a...

  • hw3 Data Structure: Please write programs and finish the question in C language: thank you (You...

    hw3 Data Structure: Please write programs and finish the question in C language: thank you (You can do both 5 & 6 if you know both, or you can do any one of these 2 questions that you know, thank you!) 5. Generate a function for clockwise rotation each node (either char type or int type) in doubly linked list by N places, e.g. given list = NULL<-Head<=>c<=>i<=>v<=>i>=>c->NULL, from function call rotate DLL(list, 3), the output will be like this...

  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • Write a function to insert a name at the end of a linked list? (C) I...

    Write a function to insert a name at the end of a linked list? (C) I have a linked list: struct node { char person[100]; struct node *next; }; int main() { struct node *new_node; new_node=malloc(sizeof(struct node)); printf("Please enter a name:\n"); scanf("%s", ); } How do I get the name from the user and write a function to add it to the end of the linked list? I'm not supposed to use the insert() library function, which is why I'm...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...

    C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT