Question

In: Computer Science

Hi! I 'm writing a code for a doubly linked list and this is the header...

Hi!

I 'm writing a code for a doubly linked list and this is the header file

#include<iostream>
#include <string>
using namespace std;

struct node
{
int data;
node *next,*prev;
node(int d,node *p=0,node *n=0)
{
data=d; prev=p; next=n;
}
};

class list
{
node *head,*tail;
public:
list();
bool is_empty();
int size();
void print();
void search();
int search2(int el);
void add_last(int el);
void add_first(int el);
bool add_pos();
bool delete_first();
bool delete_last();
void delete_pos(int pos);
void delete_el();
void add_sorted();
};

i want to write a function that split the list into 2 list from a specific number

ex. 5 4 7 2 8

if I spilt it from number 4 it will be 5 in the first list and 7 2 8 in the second list and number 4 will go to the List that contains fewer element so first list will have 5 4 and second list 7 2 8 if the have the same number of element the number that you were separated from it goes to any list it does not matter

Solutions

Expert Solution

// header file

#include<iostream>
#include <string>
using namespace std;

struct node
{
   int data;
   node *next,*prev;
   node(int d,node *p=0,node *n=0)
   {
       data=d; prev=p; next=n;
   }
};

class list
{
   node *head,*tail;
public:
   list();
   bool is_empty();
   int size();
   void print();
   void search();
   int search2(int el);
   void add_last(int el);
   void add_first(int el);
   bool add_pos();
   bool delete_first();
   bool delete_last();
   void delete_pos(int pos);
   void delete_el();
   void add_sorted();
  
   // function that splits this list into 2 lists, containing all data from start to first occurrence of elem
   // and other containing all data after elem to end of list
   // elem is inserted in the list with fewer elements. In case of both lists having same number of elements,
   // elem can be inserted in either of list
   // the second list is returned by the function
   list split(int elem); // add it to the class declaration
};

// end of header file

// function that splits this list into 2 lists, containing all data from start to first occurrence of elem
// and other containing all data after elem to end of list
// elem is inserted in the list with fewer elements. In case of both lists having same number of elements,
// elem can be inserted in either of list
// the second list is returned by the function
// add the below function to the implementation file
list list::split(int elem)
{
   list second; // create an empty second list
  
   // list is not empty
   if(!is_empty())
   {
       // set curr to head
       node* curr = head;
       node* pre = NULL; // set pre to node previous to curr
      
       // loop to get the first occurrence of elem in list in curr
       while(curr->data != elem)
       {
           pre = curr;
           curr = curr->next;
       }
      
       if(pre == NULL) // elem is the head node
       {
           // set head of second list to node next to head
           second.head = head->next;
           // list contains more than 1 node
           if(second.head != NULL)
           {
               // update prev of head to null
               second.head->prev = NULL;
               second.tail = tail; // update tail of second to tail of this list
              
               // second list contains only 1 node
               if(second.tail == second.head)
                   second.tail->prev = NULL;
           }  
          
           // make this list an empty list
           head = NULL;
           tail = NULL;
       }
       else if(curr != NULL) // elem in list
       {
           // set next of pre to null
           pre->next = NULL;
          
           // set head of second list to node next to curr
           second.head = curr->next;
          
           // second list is not empty
           if(second.head != NULL)
           {
               second.head->prev = NULL; // update prev of head of second list to null
               second.tail = tail; // update tail of second list to tail of this list
              
               // second list contains only 1 node
               if(second.tail == second.head)
                   second.tail->prev = NULL;
           }  
          
           tail = pre; // update tail of this list to pre
       }
      
       // insert curr's data
       if(curr != NULL)
       {
           if(size() <= second.size()) // size of this list <= size of second list
           {
               add_last(curr->data); // add curr's data at the end of this list
           }
           else // else add curr's data at the end of second list
           {
               second.add_last(curr->data);
           }
       }

       // if curr is null, then no change to this list as elem is not found in list
   }
   return second; // return the second list
}


Related Solutions

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...
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 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;...
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...
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...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
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...
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   ...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT