Here is the linked lists code, so how to make two different lists and display the intersection between them?
For example there is two linked lists called L1 and L2, so how to implement a function that displays the intersection of these two lists, like L1 has 1,2,3,4,5 and L2 has 11,3,4
so the result would be 3,4.
#include <iostream>
using namespace std;
#include "Node.h"
class DoublyLinkedList{
private:
Node* head;
Node* tail;
Node* list;
int elementCount;
public:
DoublyLinkedList(){
head = tail = list = NULL;
elementCount = 0;
}
void insertAtFront(int item){
elementCount++;
Node* newNode;
newNode = new Node;
newNode->data = item;
list = head;
if(list == NULL) {
head = newNode;
tail = head;
return;
}
newNode->next = head;
head->prev = newNode;
head = newNode;
head->prev = tail;
tail->next = head;
}
void insertAtEnd(int item){
elementCount++;
Node* newNode;
newNode = new Node;
newNode->data = item;
list = head;
if(list == NULL){
head = newNode;
tail = head;
return;
}
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
head->prev = tail;
tail->next = head;
}
bool insertAtIndex(int item, int index){
if(index > elementCount){
cout << "Invalid index" << endl;
return false;
}
else if(index == 0){
insertAtFront(item);
}
else if(index == elementCount){
insertAtEnd(item);
}
else {
Node* newNode = new Node;
newNode->data = item;
list = getNodeAt(index - 1);
newNode->next = list->next;
list->next->prev = newNode;
list->next = newNode;
newNode->prev = list;
elementCount++;
}
return true;
}
};
Since this is a learning assignment, here is a general description of the approach:
Define a function bool contains(int value) const that takes an int value and returns true when the list contains the specified value; this function will have a loop in it.
Define a function DoublyLinkedList intersectWith(DoublyLinkedList& other) const that produces the intersection by following the algorithm below:
Create a new DoublyLinkedList result
For each item of this list, call other.contains(node.data)
If the function returned true, check that result.contains(node.data) returns false to eliminate duplicates
If other.contains(node.data) && !result.contains(node.data), add node.data to result
Continue to the next node until this list is exhausted
Return the result list.
Related
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node{
int data;
Node* next;
Node* prev;
};
Node* head;
void display(){
Node* temp = head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void insert(int value){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = value;
temp->prev = NULL;
temp->next = NULL;
if(head==NULL){
head = temp;
}
else{
Node* t = head;
while(t->next!=NULL){
t = t->next;
}
t->next = temp;
temp->next = NULL;
temp->prev = t;
}
}
void insertAtFirst(int value){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = value;
temp->prev = NULL;
temp->next = head;
head->prev = temp;
head = temp;
}
void sortedInsert(int value){
Node* curr = head;
while(curr->next!=NULL)
curr = curr->next;
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = value;
if(head==NULL || temp->data < head->data){
temp->prev = NULL;
temp->next = head;
head->prev = temp;
head = temp;
}
else if(head->next!= NULL && temp->data >= head->data){
Node* pred = head;
while(temp->data > pred->data){
pred = pred->next;
}
pred->prev->next = temp;
temp->prev = pred->prev;
temp->next = pred;
pred->prev = temp;
}
else if(curr->next==NULL){
curr->next = temp;
temp->prev = curr;
temp->next = NULL;
}
}
int main()
{
insert(10);
insert(20);
insert(25);
insert(35);
insertAtFirst(1);
insertAtFirst(29);
sortedInsert(30);
sortedInsert(40);
//1 10 20 25 29 30 35 40
display();
return 0;
}
I think I have implemented the doubly linked list probably. However, the problem I am facing is that when I try to input a node that has a greater data than the existing data of the last node, it doesn't save. There is probably something wrong with the logic I have employed - looked up some previous answers but failed to fix the problem.
As you try to insert sorted elements in your linked list, you cannot avoid using pointer of pointers in order to keep trace of the current and previous element.
Consider the following function:
void sortedInsert(int value)
{
Node **curr = nullptr;
Node **prev = nullptr;
int cc = 0;
for (curr = &head;
*curr && ((cc = ((*curr)->data < value)));
prev=curr, curr = &(*curr)->next);
Node* newElem = (Node*)malloc(sizeof(Node));
newElem->data = value;
newElem->next = *curr;
newElem->prev = *prev;
*curr = newElem;
}
Try to draw a diagram of your elements with their address...it helps a lot to understand how pointers and super-pointers work.
Z.
I want to generate a random doubly linked list with a fixed length that is chosen by the user.
The list would insert the first value at the head and then all of the other values at the end of the list but it doesn't correctly adjust the pointers.
Head and tail nodes are both initialized in main to NULL.
Random values work, the problem is with the pointers since at the second iteration the tail stays the same as the head, meaning the list is not increasing.
void GenRandSeq(struct Node* &head, struct Node* &tail, int len){
int i = 0;
std::mt19937 rng;
std::uniform_int_distribution<uint32_t> uint_dist(0,10000);
while (i < len){
Node* newNode = new Node();
int new_el = uint_dist(rng);
newNode->key = new_el;
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL){
tail = newNode;
head = newNode;
}
else{
if (tail != NULL){
newNode->next = NULL;
newNode->prev = tail;
tail->next = newNode;
}
else
tail = newNode;
}
i++;
}
}
I don't see what I'm missing in the code.
You are not setting tail correctly.
When head and tail are both not null (which should always be true when the list is not empty), you are not updating the tail to point at the newly created node. The assignment of tail needs to be moved out of the else statement. You are appending new nodes to the end of the list, so the tail must be updated on every loop iteration.
Try something more like this instead:
void GenRandSeq(Node* &head, Node* &tail, int len){
std::mt19937 rng;
std::uniform_int_distribution<uint32_t> uint_dist(0,10000);
while (len > 0){
Node* newNode = new Node;
newNode->key = uint_dist(rng);
newNode->prev = tail;
newNode->next = nullptr;
if (!head){
head = newNode;
}
if (tail){
tail->next = newNode;
}
tail = newNode;
--len;
}
}
Which can then be streamlined a little bit further by eliminating the if statements inside the loop:
void GenRandSeq(Node* &head, Node* &tail, int len){
std::mt19937 rng;
std::uniform_int_distribution<uint32_t> uint_dist(0,10000);
Node **next = (tail) ? &(tail->next) : &head;
while (len > 0){
Node *newNode = new Node;
newNode->key = uint_dist(rng);
newNode->prev = tail;
newNode->next = nullptr;
*next = newNode;
tail = newNode;
next = &(newNode->next);
--len;
}
}
I was trying out Linked List and I have a bit of problem with my insertNode1 function. This function is supposed to insert a new node after the inputted node by the user. The result doesn't show what I wanted to show. I wanted the number 5 to be after 3, but it doesn't show how I wanted it.
#include <iostream>
using namespace std;
class List {
struct node {
float data;
node* next;
};
node* head;
public:
void appendNode(float);
void insertNode(float);
void insertNode1(float,float);
void deleteNode(float);
void displayList();
List(){
head = NULL;
}
~List(){
node *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != NULL){
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
};
void List::appendNode(float d)
{
node *newNode, *nodePtr;
newNode = new node;
newNode->data = d;
newNode->next = NULL;
if (!head){
head = newNode;
}
else {
nodePtr = head;
while (nodePtr->next){
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
}
}
void List::insertNode(float d){
node *newNode, *nodePtr, *prevNode=NULL;
newNode = new node;
newNode->data = d;
if (head== NULL)
{
head = newNode;
newNode->next = NULL;
}
else {
nodePtr = head;
while (nodePtr != NULL && nodePtr->data < d){
prevNode = nodePtr;
nodePtr = nodePtr->next;
}
if (prevNode == NULL){
head = newNode;
newNode->next = nodePtr;
}
else {
prevNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void List::insertNode1(float p, float d){
node *newNode, *nodePtr, *selectedNode=NULL;
newNode = new node;
newNode->data = d;
nodePtr = head;
selectedNode = nodePtr;
selectedNode->data = p;
if (head != NULL){
head = newNode;
newNode->next = NULL;
}
else {
while (nodePtr->next != NULL && nodePtr->data != p){
nodePtr = nodePtr->next;
}
if (selectedNode == NULL){
head = newNode;
selectedNode->next = NULL;
}
else {
selectedNode->next = newNode;
}
}
}
void List::deleteNode(float da){
node *nodePtr, *previousNode=NULL;
if (head == NULL){
return;
}
if (head ->data == da){
nodePtr = head->next;
delete head;
head = nodePtr;
}
else{
nodePtr = head;
while (nodePtr != NULL && nodePtr->data != da){
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
void List::displayList(){
node *nodePtr;
nodePtr = head;
while (nodePtr!= NULL){
cout << nodePtr->data << endl;
nodePtr = nodePtr->next;
}
}
int main(){
List lobj;
lobj.appendNode(3);
lobj.appendNode(6.4);
lobj.appendNode(4.5);
lobj.appendNode(7.8);
lobj.insertNode(1.5);
lobj.displayList();
cout << "after deleting and adding a new node" << endl;
lobj.deleteNode(6.4);
lobj.displayList();
lobj.insertNode1(3, 5);
lobj.displayList();
system("pause");
}
At the beginning of your void List::insertNode1(float p, float d) function:
nodePtr = head;
selectedNode = nodePtr;
selectedNode->data = p;
Here, selectedNode points at the head of the list and you seem to overwrite the data of that node with the value of p, which I cannot help but feel is not what you meant to do.
There are a few issues inside List::insertNode1.
nodePtr = head;
selectedNode = nodePtr;
selectedNode->data = p;
Here selectedNode points at head but then you set the first data value to p.
if (head != NULL){
head = newNode;
newNode->next = NULL;
}
This part only executes when head is not null. This means you get rid of the entire list every time this is run. This is why the last call to list::displayList only prints 5.
if (selectedNode == NULL){
head = newNode;
selectedNode->next = NULL;
}
Here selectedNode is NULL. first you set head to be a new node but this doesn't change selectedNode. If you ran this method on an empty list your program would segfault here. You should be setting the next of head or newNode to NULL here, not selectedNode.
else {
selectedNode->next = newNode;
}
Here selectedNode still points to head. You change nodePtr but this doesn't also change selectedNode.
Hi could you please help me change this linked list into a doubly linked list ?
I would be very grateful for help :)
#include <iostream>
using namespace std;
template <class T>
struct node
{
T data;
node<T> *next;
node<T> *prev;
};
template <class T>
class Container
{
public:
//constructs a new empty Kontener
Container()
{
head = new node<T>;
head->next = head;
head->prev = head;
};
//constructs a new jp_list that is a copy of an existing list
Container(const Container<T>& rt_side)
{
head = new node<T>;
head->next = head;
head->prev = head;
node<T> *crt_ptr = rt_side.head->next;
while(crt_ptr != rt_side.head)
{
push_back(crt_ptr->data);
crt_ptr = crt_ptr->next;
}
};
//adds a data node to the front of the list
void push_front(T nw_data)
{
node<T> *temp = new node<T>;
temp->data = nw_data;
temp->next = head->next;
head->next->prev = temp;
temp->prev = head;
head->next = temp;
};
//adds a data node to the end of the list
void push_back(T nw_data)
{
node<T> *temp = new node<T>;
temp->data = nw_data;
head->prev->next = temp;
temp->prev = head->prev;
temp->next = head;
head->prev = temp;
};
//removes the first node and returns the data
T pop_front()
{
node<T> *temp = head->next;
T temp_data = head->next->data;
head->next = temp->next;
temp->next->prev = head;
delete temp;
return temp_data;
};
//removes the last node and returns the data
T pop_back()
{
node<T> *temp = head->prev;
T temp_data = head->prev->data;
head->prev = temp->prev;
temp->prev->next = head;
delete temp;
return temp_data;
};
//resturns the size of the list
int size()
{
int size = 0;
node<T> *crt_ptr; //pointer to current node
crt_ptr = head->next;
while(crt_ptr != head)
{
size += 1;
crt_ptr = crt_ptr->next; //advance to the next node then loop
}
return size;
};
//prints out all the data in the list
void display_all()
{
node<T> *crt_ptr = head->next;
for(int i = 0; crt_ptr != head; i++)
{
cout << "Node " << (i+1) << ": " << crt_ptr->data << endl;
crt_ptr = crt_ptr->next;
}
};
Container& operator= (const Container& rt_side)
{
if(this == &rt_side)
return *this;
node<T> *crt_ptr = head->next;
//empty this list so the rt_side can be coppied in
while(crt_ptr != head)
{
crt_ptr = crt_ptr->next;
pop_front();
}
crt_ptr = rt_side.head->next;
while(crt_ptr != rt_side.head)
{
push_back(crt_ptr->data);
crt_ptr = crt_ptr->next;
}
return *this;
};
virtual ~Container()
{
int list_size = size();
for(int i = 0; i < list_size; i++)
{
pop_front();
}
delete head;
};
private:
node<T> *head;
};
#endif
I am just a beginner so please help me :)
The tail would always point to the last item that was inserted into the list.
However, I do not think that having a tail pointer makes it necessarily a doubly linked list. A singly linked list can also have a tail pointer (however useless it might be). I believe you are asking to create a double ended doubly linked list.
You already have the next and previous pointers to enable the double link. All you have to do, is when you push something into the list, you need to make the tail pointer point to the node that is being added. Similarly, when removing a node, you need the tail pointer to point to tail's previous BEFORE deleting the last node.
* UPDATE *
Here is some code. I am assuming a double ended doubly linked list with two ends.
void push_front(T nw_data)
{
node<T> *temp = new node<T>;
temp->data = nw_data;
if(head == nullptr)
{
head = temp;
tail = temp;
}
else if(head == tail)
{
head->next = temp;
temp->prev = head;
tail = temp;
}
else
{
temp->next = head->next;
head->next->prev = temp;
temp->prev = head;
head->next = temp;
}
};
//adds a data node to the end of the list
void push_back(T nw_data)
{
node<T> *temp = new node<T>;
temp->data = nw_data;
if(head == nullptr)
{
head = temp;
tail = temp;
}
else if(head == tail)
{
head->next = temp;
temp->prev = head;
tail = temp;
}
else
{
temp->prev = tail;
tail->next = temp;
tail = temp;
}
};
T pop_back()
{
node<T> *temp = tail;
T temp_data = tail->data;
tail = tail->prev;
tail->next = null;
delete temp;
return temp_data;
};
* UPDATE * Copy Constructor
In your copy constructor, if the push_back sets the tail, then all you need to do is to push_back the nodes like you are doing. head->next = head and head->prev = head makes the linked list cyclical.
Container(const Container<T>& rt_side)
{
this->head = rt_side.head;
node<T> * crt_ptr = rt_side.head->next;
while (crt_ptr != null)
{
push_back(crt_ptr->data);
crt_ptr = crt_ptr->next;
}
};
I am not able to figure out, why is my code to insert into a sorted doubly linked list failing on some test cases.Please let me know. I dont know of the test cases, they are system generated.
Node* SortedInsert(Node *head,int data)
{
// Complete this function
// Do not write the main method.
Node * temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL)
{
head = temp;
return head;
}
if (temp->data <= head->data)
{
temp->next = head;
head->prev = temp;
head = temp;
return head;
}
Node *curr = head;
while (curr->next!=NULL)
{
if (temp->data <= curr->data)
{
curr->prev->next = temp;
temp->prev = curr->prev;
temp->next = curr;
curr->prev = temp;
return head;
}
curr = curr->next;
}
curr->next = temp;
temp->prev = curr;
return head;
}
Thanks
Once you reach the last node, you should again compare its data with the new node and insert accordingly.
curr->next = temp;
temp->prev = curr;
return head;
}
If execution reaches this part, then at present curr is pointing to the last node. Now you should again compare its data like the following:
if (temp->data <= curr->data)
{ // insert before last node
curr->prev->next = temp;
temp->prev = curr->prev;
temp->next = curr;
curr->prev = temp;
return head;
}
// else insert at the end.
curr->next = temp;
temp->prev = curr;
return head;
}
Alternatively, you can write an if condition for node at the end of the list
Node* SortedInsert(Node *head,int data)
{
struct Node* p = head;
struct Node* q = NULL;
struct Node* r = new Node;
r->data=data;
r->prev=NULL;
r->next=NULL;
if(p==NULL){
p=r;
head=p;
}
else if(p!=NULL&&p->data>r->data){
p->prev=r;
r->next=p;
p->next=NULL;
head = r;
}
else{
p=head;
while(p!=NULL) {
if(p!=NULL&&p->data>r->data){ //If node is not at the end of list and smaller than some node
(p->prev)->next = r;
r->next = p;
r->prev = p->prev;
p->prev=r;
return head;
}
else if(p->next==NULL) //If node is at the end of list
{
p->next = r;
r->prev = p;
r->next = NULL;
return head;
}
else{}
p=p->next;
}
}
return head;
}
/*
* For reference:
*
* link to the problem :-
* https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list
*
* DoublyLinkedListNode {
* int data;
* DoublyLinkedListNode* next;
* DoublyLinkedListNode* prev;
* };
*
* Function to insert a node in sorted DLL with given data & return head
*/
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
DoublyLinkedListNode** prev = &head;
DoublyLinkedListNode* newNode = new DoublyLinkedListNode(data);
while ( *prev && ((*prev)->data <= data) ) {
prev = &(*prev)->next;
}
newNode->prev = *prev;
newNode->next = *prev;
*prev = newNode;
return head;
}
This solution works in all cases (insert in between, insert at the head, insert at the end etc). I was just trying to write a code without any conditions. While searching for some error, I ended up on this SO thread. Do correct me if this solution doesn't work for some test cases.
This is my code that passed all the test cases.
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
DoublyLinkedListNode *temp = head;
if (!temp) {
temp = new DoublyLinkedListNode(data);
head = temp;
return head;
}
while (temp->data < data && temp->next != nullptr) {
temp = temp->next;
}
if (temp->data > data) {
DoublyLinkedListNode *t = new DoublyLinkedListNode(data);
t->next = temp;
if (temp == head) {
head = t;
temp->prev = t;
return head;
}
temp->prev->next = t;
t->next = temp;
temp->prev = t;
} else {
DoublyLinkedListNode *t = new DoublyLinkedListNode(data);
t->next = temp->next;
temp->next = t;
temp->next->prev = temp;
if (t->next != nullptr) t->next->prev = t;
}
return head;
}
Here is some code in order to Insert a Node in Sorted Doubly Linked List
Node* SortedInsert(Node *head,int data)
{
struct Node *temp;
struct Node *newNode = new Node();
newNode->data=data;
newNode->next=NULL;
newNode->prev=NULL;
temp=head;
if(head==NULL)
{
head = newNode;
}
else
{
while(temp!=NULL)
{
if(temp->data<data&&temp->next!=NULL)
{
temp=temp->next;
}
else
{
if(temp->next==NULL&&temp->data<data)
{
temp->next = newNode;
newNode->prev = temp;
break;
}
else if(temp->prev==NULL&&temp->data>data)
{
newNode->next=temp;
temp->prev = newNode;
head=newNode;
break;
}
else
{
newNode->next = temp;
newNode->prev = temp->prev;
temp->prev->next =newNode;
temp->prev=newNode;
break;
}
}
}
}
return head;
}
Here is the complete C++ program for inserting a node at appropriate position in a sorted doubly linked list:
void InsertInSortedDoublyLinkedList()
{
Node *head;
head = CreateDoublyLinkList(3);
PrintDoublyLinkedList(head);
head = SortedInsert(head, 6);
printf("\n Linked list after insertion in sorted order\n");
PrintDoublyLinkedList(head);
}
void PrintDoublyLinkedList(Node *head)
{
Node *temp = head;
printf("NULL -> ");
while (temp)
{
printf("%d ->", temp->data);
temp = temp->next;
}
printf("NULL");
}
Node* CreateDoublyLinkList(int numberOfNodes)
{
Node *head = NULL, *temp, *temp1;
int startingValue = 3;
if (numberOfNodes == 0)
{
return head;
}
temp = (Node*)malloc(sizeof(Node));
temp->data = startingValue;
temp->next = NULL;
temp->prev = NULL;
head = temp;
startingValue += 2;
numberOfNodes--;
for (; numberOfNodes > 0; numberOfNodes--, startingValue += 2, temp = temp->next)
{
temp1 = (Node*)malloc(sizeof(Node));
temp1->data = startingValue;
temp1->next = NULL;
temp1->prev = temp;
temp->next = temp1;
}
return head;
}
Node* SortedInsert(Node *head,int data)
{
Node *temp = NULL, *temp1,*newNode;
int nodeInserted = 0;
if (head == NULL)
{
head = (Node*) malloc(sizeof(Node));
head->data = data;
head->next = NULL;
head->prev = NULL;
}
else
{
if (head->data > data)
{
//insertion need to take place before head itself.
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = head;
newNode->prev = NULL;
head->prev = newNode;
head = newNode;
}
else
{
temp1 = head;
temp = head ->next;
while (temp)
{
if (temp->data > data)
{
//we need to insert the node before temp
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = temp1;
newNode->next = temp;
temp1->next = newNode;
temp->prev = newNode;
nodeInserted = 1;
break;
}
temp1 = temp;
temp = temp->next;
}
if (!nodeInserted)
{
//node insertion need to take place at tail.
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = temp1;
newNode->next = NULL;
temp1->next = newNode;
}
}
}
return head;
}
struct Node
{
int data;
Node *next;
Node *prev;
}
Node* SortedInsert(Node *head,int data)
{
Node* p1,*p2;
int n=10;
p2=(Node*)malloc(sizeof(struct Node));
p2->next=NULL;
p2->prev=NULL;
p2->data=data;
p1=head;
if(p1==NULL)
return p2;
while(p1->next!=NULL)
{
if(p1->data<data)
p1=p1->next;
else
break;
}
/*Three cases arise when p1->next == NUll i.e. we are end of list
case 1: insert after the end
case 2: insert in between last and second last node
case 3: insert node at the beginning of the list
*/
if(p1->next==NULL)
{
if(p1->data<data)
{
p2->prev=p1;
p1->next=p2;
}
else if(p1->data > data && p1->prev!=NULL)
{
p2->next=p1;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
p1->prev=p2;
}
else
{
p2->next=p1;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
p1->prev=p2;
head=p2;
}
}
// Here we have only one case where new node is inserted between two nodes of the list
else
{
p2->next=p1;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
p1->prev=p2;
}
return head;
}