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.

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

program snapshot

output snapshot


Related Solutions

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,...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
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 was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
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...
HI i will write user's manual for a doubly/singly linked list , How can i write...
HI i will write user's manual for a doubly/singly linked list , How can i write User's manual ?
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT