Question

In: Computer Science

Given a doubly linked list in c++, how do I create a function that returns the...

Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList, we should return the very first instance where pattern is seen. The implementation must use doubly linked list, turning the patterns into strings and using find isn't allowed.

struct Node {

char value;

Node* next;

Node* prev;

}

Node* findList(Node* subList, Node* mainList) {

// need to do this

}

void printList(Node* node)
{
    while (node != nullptr)
    {
        printf("%c ", node->value);
        node = node->next;
    }
}
Node *newNode(char key)
{
    Node *temp = new Node;
    temp->value= key;
    temp->next = nullptr;
    return temp;
}
int main() {
    // this is the sublist
    Nucleotide *a = newNode('T');
    a->next = newNode('G');
    a->next->next = newNode('T');
    a->next->next->next = nullptr;

    // this is the main list
    Node *b = newNode('A');
    b->prev = nullptr;
    b->next = newNode('A');
    b->next->prev = b;
    b->next->next = newNode('C');
    b->next->next->prev = b->next;
    b->next->next->next = newNode('T');
    b->next->next->next->prev = b->next->next;
    b->next->next->next->next = newNode('G');
    b->next->next->next->next->prev = b->next->next->next;
    b->next->next->next->next->next = newNode('A');
    b->next->next->next->next->next->prev = b->next->next->next->next;
    b->next->next->next->next->next->next = nullptr;
    printList(a);
    cout << endl;
    printList(b);
    cout << endl;
    Node *c = findList(a, b);
    cout << c->value << endl;

    return 0;
}

Solutions

Expert Solution

So In the above problem statement we are given two doubly linked lists mainlist and dublist we are to check whether sublist is present in mainlist or not. if present then return Node pointer that points to first node of the sublist in the mainList else simply return NULL.

we stored mainlist and sublist in two string vaiable main and sub respectively. now our problem is converted to find first occurence of a string in other(sub in main).

we stored the first occurence in a variable named count ,now it's clearly visible that if we apply next operation on head of mainlist count number of times se will end up at desired first node of common occurence.

below is program ---->

#include <iostream>

using namespace std;

struct Node {

char value;

Node* next;

Node* prev;

};

Node* findList(Node* subList, Node* mainList) {
Node* head = NULL;
string main,sub;//main and sub are string representation of both lists
Node *head1=subList,*head2 = mainList;
while(head1!=NULL)
{
sub+=head1->value;
head1=head1->next;
}
while(head2!=NULL)
{
main+=head2->value;
head2=head2->next;
}

std::size_t count = main.find(sub);//to find first occurence of one string in other we used predefined find function
if (count==std::string::npos)//if sub is not substring of main return NULL
return NULL;
head = mainList;
for(int i=1;i<=count;i++)
head = head->next; //applying next operation count times
return head;
}

void printList(Node* node)
{
while (node != nullptr)
{
printf("%c ", node->value);
node = node->next;
}
}
Node *newNode(char key)
{
Node *temp = new Node;
temp->value= key;
temp->next = nullptr;
return temp;
}
int main() {
// this is the sublist
Node *a = newNode('A');
a->next = newNode('C');
a->next->next = newNode('T');
a->next->next->next = nullptr;

// this is the main list
Node *b = newNode('A');
b->prev = nullptr;
b->next = newNode('A');
b->next->prev = b;
b->next->next = newNode('C');
b->next->next->prev = b->next;
b->next->next->next = newNode('T');
b->next->next->next->prev = b->next->next;
b->next->next->next->next = newNode('G');
b->next->next->next->next->prev = b->next->next->next;
b->next->next->next->next->next = newNode('A');
b->next->next->next->next->next->prev = b->next->next->next->next;
b->next->next->next->next->next->next = nullptr;
printList(a);
cout << endl;
printList(b);
cout << endl;
Node *c = findList(a, b);
cout << c->value << endl;

return 0;
}

if this answer is helpful to you please give positive rating.if any doubts please provide comments i will clarify your doubts


Related Solutions

Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
I need an example of how to swap and index within a doubly linked list with...
I need an example of how to swap and index within a doubly linked list with index + 1. This is written in java. public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values   ...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT