C++ make list with certain values from doubly linked list - c++

I have problem with my doubly linked list.
#include <iostream>
#include <cstdlib>
using namespace std;
struct node {
int value;
node* next;
node* prev;
};
void printList(node* head);
void main(int args, char** argv) {
int x;
node* head;
node* tail;
node* n;
n = new node;
n->value = 1;
n->prev = NULL;
head = n;
tail = n;
cout << "Enter number of elements: ";
cin >> x;
for (int i = 0; i < x; i++) {
cout << "Enter element value: ";
n = new node;
cin >> n->value;
n->prev = tail;
tail->next = n;
tail = n;
}
tail->next = NULL;
system("pause");
}
void printList(node* head) {
node* temp = head;
while (temp != NULL) {
cout << temp->value << " ";
temp = temp->next;
}
cout << endl;
}
Q1 - How can i find index of first negative element in doubly linked list?
Q2 - How to make new list from all negative elements in doubly linked list, and than print new list(negative elements) and doubly linked list(without negative elements)?

This is untested code but it should give you good start.
class LinkedList {
public:
LinkedList();
void insertHead(int val) {
node* n;
n->value = val;
n->next = head;
n->prev = tail;
head->prev = n;
head = n;
}
void insertTail(int val) {
node* n;
n->value = val;
tail->next = n;
n->next = head;
n->prev = tail;
tail = n;
}
LinkedList makeNewList(int i) {
node* n = head;
LinkedList *newList = new LinkedList();
while(n->next != nullptr && n->next != head) {
if (n->value < i) {
newList->insertHead(n->value);
}
n = n->next;
}
}
private:
node* head = nullptr;
node* tail = head;
}

Related

SIGSEV in Singly Linked List

I am writing a code to rotate a singly linked list counter-clockwise by K number of nodes. I wrote the following code. For example for the input linked list, 1,2,3,4,5,6,7,8 the rotate function Node *rotate(Node *head, int k){} returns the head pointer to the linked list 5,6,7,8,1,2,3,4. I wrote the following code. In this code, If I call print(head) inside the rotate function then it gives correct output but once it returns the head pointer to main then it either throws SIGSEV or is producing 1,2,3,4.
#include <iostream>
using namespace std;
class List {
public:
int data;
List *next;
explicit List(int element) : data(element), next(nullptr){}
};
List *insert() {
int n, i, value;
List *temp = nullptr, *head = nullptr;
cin >> n;
for(i = 0; i < n; ++i) {
cin >> value;
if(i == 0) {
head = new List(value);
temp = head;
continue;
} else {
temp ->next = new List(value);
temp = temp->next;
}
}
return head;
}
void print(List *start) {
while(start != nullptr) {
cout << start ->data << " ";
start = start->next;
}
}
List* rotate(List* head, int k) {
List *traverse = head, *temp = head;
List *kth, *end;
int i = 0;
while(i < k - 1) {
traverse = traverse ->next;
++i;
}
kth = traverse;
while(traverse->next != nullptr) {
traverse = traverse->next;
}
end = traverse;
head = kth->next;
kth->next = nullptr;
end->next = temp;
print(head);
return head;
}
int main() {
int k;
List *head = insert();
cin >> k;
print(head);
cout << endl;
rotate(head, k);
print(head);
return 0;
}
PS: I am only allowed to change the rotate function.

Print function doesn't terminate

I don't know why PrintList() doesn't terminate. It is a LinkedList, so when I go to next, that should terminate.
When I do AddNode once and then print, that terminates, when I do addNode twice, print doesn't terminate.
The reason why in constructor I create 5 empty spots is because I'm required to create those 5 empty spots when program starts.
Moreover, if for example I add twice , how I can assign pointer to that second value?
#pragma once
class LinkedList
{
private:
typedef struct node {
int data;
node* next;
}* nodePtr;
nodePtr n;
nodePtr head;
nodePtr curr;
nodePtr temp;
public:
LinkedList();
void AddNode(int addData);
void PrintList();
~LinkedList();
};
#include "LinkedList.h"
#include<cstdlib>
#include<iostream>
using namespace std;
LinkedList::LinkedList()
{
head = NULL;
curr = NULL;
temp = NULL;
n = new node;
for (int x = 1; x <= 5; x++) {
//cout << n<<endl;
n->next = n;
}
}
void LinkedList::AddNode(int addData) {
//nodePtr n = new node;
n->next = NULL;
n->data = addData;
cout << n <<endl;
if (head != NULL) {
curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = n;
}
else {
head = n;
}
}
void LinkedList::PrintList() {
curr = head;
while (curr != NULL) {
cout << curr->data << endl;
curr = curr->next;
}
}
LinkedList::~LinkedList()
{
head = NULL;
curr = NULL;
temp = NULL;
delete n;
}
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "LinkedList.h"
using namespace std;
int main() {
LinkedList *l = new LinkedList();
l->AddNode(5);
l->AddNode(8);
l->PrintList();
system("pause");
return 0;
}
The node n is always the same since you are not setting n to a different node
So when you do n->next and n->data , the same node is being modified each time
void LinkedList::AddNode(int addData) {
//nodePtr n = new node; // you need to uncomment this
n->next = NULL;
n->data = addData;
cout << n <<endl;
if (head != NULL) {
curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = n;
}
else {
head = n;
}
}
After your first addNode(5), lets examine the values.
head = n
head->data = 5
head->next = null
After your second addNode(8)
head = n
head->data = 8
head->next = n // set by "curr->next = n" .
So you have a problem here. When you try to loop through your linked list, it will become head->next->head->next->head->next->head->next ..... causing an infinite loop

C++ program to Reverse a linked list from position m to n

I am stuck on this simple question for hours...can someone plz help...where i am going wrong?
Question : Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note: 1 ≤ m ≤ n ≤ length of list
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
typedef struct node ListNode;
node *newNode(int key)
{
node *temp = new node;
temp->data = key;
temp->next = NULL;
return temp;
}
ListNode* reverseUpto(ListNode *head, int size)
{
if(size<=1)
return head;
ListNode *cur=head,*newhead=NULL,*temp;
for(int i=0;i<size;i++)
{
temp=cur;
cur=cur->next;
temp->next=newhead;
newhead=temp;
}
head->next=cur;
return newhead;
}
ListNode* reverseBetween(ListNode* A, int m, int n)
{
ListNode *head=A;
if(m==n)
return A;
ListNode *dummyhead=newNode(0);
dummyhead->next=head;
ListNode *prev=dummyhead;
ListNode *cur=head;
int counter=1;
while(counter<m)
{
printf("counter= %d prev=%d cur=%d\n",counter,prev->data,cur->data);
counter++;
prev=cur;
cur=cur->next;
}
prev->next=NULL;
ListNode * retPtr=reverseUpto(cur,m-n+1);
prev->next=retPtr;
return A;
}
void printlist(node *head)
{
while(head != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
int main()
{
node *head1 = newNode(1);
head1->next = newNode(2);
head1->next->next = newNode(3);
head1->next->next->next = newNode(4);
head1->next->next->next->next = newNode(5);
head1->next->next->next->next->next = newNode(6);
head1->next->next->next->next->next->next = newNode(7);
head1->next->next->next->next->next->next->next = newNode(8);
cout << "Given linked list\n";
printlist(head1);
head1=reverseBetween(head1,3,5);
cout << "\nReversed linked list\n";
printlist(head1);
return 0;
}
I am getting same output as my input!!....where is the pitfall?
ListNode * retPtr=reverseUpto(cur,m-n+1);
modify to
ListNode * retPtr=reverseUpto(cur,n-m+1);

adding a node at the end of a linked list

i am in a bit of a stand still with my program on adding a newnode to a linked it, its supposed to be added at the end of the list but i tried all i could but still having got a solution, was hoping if any of you guys could help me with it
Thanks in advance tho;
this is the bit of code
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* head;
void insert(int x);
void numInsert();
void numSearch();
int main()
{
head=NULL;
int x,n;
cout <<"How many number? \n";
cin >>n;
for (int i=0; i<n; i++){
cout <<"Enter Number \n";
cin >> x;
}
node* newNode;
newNode = new node();
newNode->data=x;
newNode->next=head;
head=newNode;
/*
NewNode->next=NULL;
if (head !=NULL){
NewNode->next=head;
}
else{
head=NewNode;
}*/
int num;
cout<<"what number do you want to insert in the list \n";
cin>>num;
node *nNode;
nNode = new node();
nNode->data=num;
nNode->next=NULL;
node *prevNode;
node *currNode;
prevNode=NULL;
currNode=NULL;
{
node* nNode= new node;
nNode->data= num;
nNode->next= NULL;
currNode=NULL; prevNode=NULL;
for(currNode=head; currNode != NULL; currNode= currNode->next)
{
if (newNode->data <= currNode->data)
{
break;
}
prevNode = currNode;
}
newNode->next=prevNode->next;
prevNode->next=newNode;
}
return 0;
}
anytime i run it , gets to a point and just stops working , pls help me out, the assignment is due today and i have no clue on how to solve it properly
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* head;
node* tail;
void insert(int x);
void numInsert();
void numSearch();
int main()
{
head=NULL;
tail = NULL;
int x,n;
cout <<"How many number? \n";
cin >>n;
for (int i=0; i<n; i++){
cout <<"Enter Number \n";
cin >> x;
if(head==NULL){
head = new node();
head->data = x;
head->next = NULL;
tail = head;
}else{
node* newNode;
newNode = new node();
newNode->data=x;
newNode->next = NULL;
tail->next = newNode;
tail = tail->next;
}
}
cout << "Linked List :" << "\n";
node* trav = head;
for (int i=0; i<n; i++){
cout << (trav->data) << '\n';
trav = trav->next;
}
int num;
cout<<"what number do you want to insert in the list \n";
cin>>num;
if(head==NULL){
head = new node();
head->data = num;
head->next = NULL;
tail = head;
}else{
node* newNode;
newNode = new node();
newNode->data=num;
newNode->next = NULL;
tail->next = newNode;
tail = tail->next;
}
cout << "Linked List :" << "\n";
trav = head;
while(trav != NULL){
cout << (trav->data) << '\n';
trav = trav->next;
}
return 0;
}

C++ doubly linked list

I got a problem with my doubly linked list. How can i make the input unique ( i don`t want it to be repeated )
for example i can input 1 and then again 1 i will have a list of 1 and 1. I need to forbid this somehow :) so the list can contain only not repeating numbers.
#include <cstdlib>
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
node* prev;
};
class Node
{
public:
Node();
~Node();
void setKopa();
void printForward();
private:
node* head;
node* tail;
node* n;
};
Node::Node()
{
setKopa();
}
Node::~Node()
{
delete n;
}
void Node::setKopa()
{
int lenght;
do
{
cout << "Input list lenght (how many elements): ";
cin >> lenght;
if(lenght<2)
cout << "Error list has to have atleast 2 elements!" <<endl;
}
while(lenght<2);
int fill;
cout << "Input "<< lenght <<" elements: "<<endl;
for (int i=0; i<lenght; i++)
{
cin>>fill;
n = new node;
n->data = fill;
if (i==0)
{
n->prev = NULL;
head = n;
tail = n;
}
else if (i+1==lenght)
{
n->prev = tail;
tail->next = n;
tail = n;
tail->next = NULL;
}
else
{
n->prev = tail;
tail->next = n;
tail = n;
}
}
}
void Node::printForward()
{
node* temp = head;
while(temp != NULL)
{
cout << temp->data << " ";
temp = temp-> next;
}
cout << endl;
}
int main()
{
Node a;
a.printForward();
system("pause");
return 0;
}
When you read input, go through the list to see if the input is already there.
With that (simple) answer out of the way, I would like to address some other things regarding your code. The first is that you have a memory leak in that you never delete the list. The second is that you don't need the class member variable n, it might as well be a local variable inside the setKopa loop.
Your way of adding new nodes is also, well, weird. It should, in my opinion, be more general instead of using the loop counter to check what to do. What I suggest is that you make a member function to add new nodes, taking the integer data as argument. This way you can call this function to add nodes anywhere, and not just in the setKopa function. In fact, I think the list should not handle that input at all, instead it should be a free-standing function called from main and which calls the addNode function.
Also the node structure doesn't need to be in the global namespace, it could be a private structure in the Node class. And speaking of the Node class, shouldn't it really be called List instead?
So if I may suggest, you might want to do something like this:
#include <iostream>
class List
{
public:
List()
: head(nullptr), tail(nullptr)
{}
~List();
void addNode(const int data);
void printAll() const;
private:
struct node
{
node()
: next(nullptr), prev(nullptr)
{}
node* next;
node* prev;
int data;
};
node* head;
node* tail;
};
List::~List()
{
for (node* next, *cur = head; cur; cur = next)
{
next = cur->next;
delete cur;
}
}
void List::addNode(const int data)
{
node* n = new node;
n->data = data;
if (tail == nullptr)
{
// First node in list
head = tail = n;
}
else
{
n->prev = tail;
tail->next = n;
tail = n;
}
}
void List::printAll() const
{
std::cout << "{ ";
for (node* cur = head; cur != nullptr; cur = cur->next)
std::cout << cur->data << ' ';
std::cout << "}\n";
}
int main()
{
List list;
for (int i = 0; i < 10; ++i)
list.addNode(i);
list.printAll();
}
The above code should print
{ 0 1 2 3 4 5 6 7 8 9 }
Replace the node-adding loop with your own.