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;
}
Related
I am trying to implement Linked list in c++ and cannot seem to get that why I can't stop taking input from the terminal.
Node* take_input_better() {
int data;
cin >> data;
Node *head = NULL;
Node *tail = NULL;
while(data != -1) {
cout << "debug" << endl;
Node *newNode = new Node(data);
if(head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail -> next = newNode;
tail = tail -> next;
// OR
// tail = newNode;
}
cout << "Debug" << endl;
cin >> data;
}
return head;
}
This function just creates a linked list with the element until -1 is entered.
If I enter the first element as -1. it seems to work fine. But when it is not -1 after I have already entered some data the program seem to take infinite number of inputs and the flow isn't even inside the while statement as the words "debug" and "Debug" don't get printed.
Edit 1: Here's the Full Program
Node.cpp
class Node {
public:
int data;
Node *next;
Node(int data){
this->data = data;
next = NULL;
}
};
linked_list.cpp
#include <iostream>
using namespace std;
#include "Node.cpp"
Node* take_input_better() {
int data;
cin >> data;
Node *head = NULL;
Node *tail = NULL;
while(data != -1) {
cout << "debug" << endl;
Node *newNode = new Node(data);
if(head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail -> next = newNode;
tail = tail -> next;
// OR
// tail = newNode;
}
cout << "Debug" << endl;
cin >> data;
}
return head;
}
int length(Node *head){
Node *temp = head;
int count = 0;
while(temp != NULL){
count++;
}
return count;
}
int main(){
Node *head = take_input_better();
cout << "Length of the Linked List: " << length(head) << endl;
}
The error is in the length function. You are not moving the temp pointer forward. Do this:
int length(Node *head){
Node *temp = head;
int count = 0;
while(temp != NULL){
count++;
temp = temp->next;
}
return count;
}
Get familiar with the debugger. It is your friend.
You also need to make sure the input stream is in a readable state (e.g. cin.good()). If it goes into a fail state, cin >> data will keep going, but without waiting for new user input or putting any meaningful value into data.
when i am inserting node in the beginning of the linked list, node is inserted in the beginning and is displayed. if i call display separately then it does not work and for inserting node at specific loc and at the end, calling display function works well.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct node {
int data;
struct node* next;
} node;
node* create(int n)
{
node* temp = NULL;
node* head = NULL;
node* p;
int i;
for (i = 0; i < n; i++) {
temp = (node*)malloc(sizeof(node));
cout << "enter the data for node number " << i << endl;
cin >> temp->data;
temp->next = NULL;
if (head == NULL) {
head = temp;
}
else {
p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = temp;
}
}
return head;
}
node* insertatbeg(node* head)
{
node* temp = NULL;
temp = (node*)malloc(sizeof(node));
cout << "\nenter the data for first node" << endl;
cin >> temp->data;
temp->next = head;
head = temp;
return head;
}
void display(node* head)
{
node* t = NULL;
t = head;
while (t != NULL) {
cout << t->data << "->";
t = t->next;
}
}
node* insertatspecloc(node* head)
{
int n;
node* temp = NULL;
node* t = head;
temp = (node*)malloc(sizeof(node));
cout << "enter the data of node after which you want to insert the
node "<<endl;
cin
>> n;
cout << "\nenter the data for last node" << endl;
cin >> temp->data;
while (t->data != n) {
t = t->next;
}
temp->next = t->next;
t->next = temp;
return head;
}
node* insertatend(node* head)
{
node* temp = NULL;
temp = (node*)malloc(sizeof(node));
cout << "\nenter the data for last node" << endl;
cin >> temp->data;
temp->next = NULL;
node* q;
q = head;
while (q->next != NULL) {
q = q->next;
}
q->next = temp;
return head;
}
int main()
{
int n, a;
struct node* head = NULL;
cout << "enter the number of nodes u want to add";
cin >> n;
head = create(n);
display(head);
cout << "\npress 1 to add node at the beginning";
cout << "\npress 2 to add node at the specific location";
cout << "\npress 3 to add node at the end\n";
cin >> a;
if (a == 1) {
insertatbeg(head);
cout << "\nlinked list after insertion:\n";
display(head);
}
if (a == 2) {
insertatspecloc(head);
cout << "\nlinked list after insertion:\n";
display(head);
}
if (a == 3) {
insertatend(head);
cout << "\nLinked list after insertion:\n";
display(head);
}
}
When you are calling insertatbeg(head); the copy of head pointer is passed as the argument of your function, then in function you are modyfing local variable (copy of head)
node *insertatbeg(node *head)
{
node *temp=NULL;
temp=(node*)malloc(sizeof(node));
cout<<"\nenter the data for first node"<<endl;
cin>>temp->data;
temp->next=head;
head=temp; // assign to local variable <---
return head;
}
and for this reason after executing insertatbeg head is not updated. insertatbeg returns pointer so you can resolve your issue by calling
head = insertatbeg(head);
or you can call function in above line without assigning but then you should pass head by reference to be able to modify original passed object in function:
node *insertatbeg(node *& head) // pass pointer by reference
{
node *temp=NULL;
//...
head = temp; // now it works
my assignment is to find the beginning of a loop in a circular linked list. Since the list is not provided i decided to make a liat by getting the user input for the size of the list then run a for loop with that size. The very last input (last node) is going to point somewhere in the linked list to create a cycle. My function to create the linked list is working, if i cout the head->data while getting the input from the user it prints the right value but when i call the function in the main the head pointer points to NULL and i get a segmentation fault. Can someone take a look at my code and explain why something like that is happening?
#include <iostream>
using namespace std;
struct node{
int data;
node *next;
};
node *head = NULL;
node *tail = NULL;
node *slow = NULL;
node *fast = NULL;
int findLoop(node * head);
void getList(node * head, int listSize);
bool isEmpty(node * head);
int main(){
int listSize;
cout <<"\nEnter the size of the list: ";
cin >> listSize;
getList(head, listSize);
if(head != NULL){
cout << "\n\n\nprinting head " << head->data; //Seg Fault
}
else{
cout << "Head is NULL" << endl;
}
findLoop(head);
return 0;
}
int findLoop(node *head){
slow = head;
fast = head;
if(head == NULL){
cout << "\nThe list is empty\n";
}
bool isLoop = false;
while(slow != NULL && fast != NULL){
if(slow == fast && isLoop == false){
slow = head;
isLoop = true;
}
else if(slow == fast && isLoop == true){
cout <<"\nThe loop starts at: ";
return slow->data;
}
slow = slow->next;
fast = fast->next->next;
}
cout <<"\nThere is no loop\n";
return 0;
}
void getList(node * head, int listSize){
int userData;
for(int i=0; i<listSize; i++){
node *temp = new node;
cout <<"\nEnter a number: ";
int NodeValue = 0;
cin >> NodeValue;
temp->data = NodeValue;
if(head == NULL){
head = temp;
cout << head->data << endl; //Test for appropriate pointing.
}
if(tail != NULL){
tail->next = temp;// point to new node with old tail
}
tail = temp;// assign tail ptr to new tail
temp->next = tail;
if(i == listSize-1){
node *temp2;
temp2 = head;
int iNumber = rand() % i;
for(int j=0; j<iNumber; j++){
temp2 = temp2->next;
}
tail->next = temp2;
}
}
}
Minimal change to actually return new list would be passing pointer by reference:
void getList(node*&, int );
or better do define pointer type
using nodePtr = node*;
void getList(nodePtr&, int);
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;
}
Here's a program which takes student name and registration number from user and stores it in a doubly linkedlist. And I'm inserting nodes at the end . But there is a logical error in while loop. It takes Name and Reg. Number but then stops.Please Help
class dlist{
struct node{
string name;
string regno;
node *next;
node *prev;
};
public:
node *head,*tail;
void insert(string,string);
void search(string);
};
void dlist::insert(string nn, string r)
{
node *ptr,*newNode;
tail=head;
newNode= new node;
newNode->name=nn;
newNode->regno=r;
newNode->next=NULL;
newNode->prev=NULL;
if(!head)
{
head=newNode;
tail=head;
}
else
{
ptr = head;
while(ptr)
ptr=ptr->next;
newNode->next=tail;
newNode->prev=ptr;
ptr->next-newNode;
}
}
void dlist::search(string n){
node *ptr;
ptr=head;
while(ptr->next!=NULL)
{
if(ptr->name==n)
cout<<"Name found..." <<ptr->name<<endl;
ptr=ptr->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
dlist dl;
string nn;
string n;
string r;
for(int i=0;i<5;i++)
{
cout<<"Enter student's name: ";
cin>>nn;
cout<<"Enter student's Registration Number: ";
cin>>r;
dl.insert(nn,r);
}
cout<<"Enter a name to search: ";
cin>>n;
dl.search(n);
}
You are having problems using the tail pointer. I've improved your code below and added comments explaining what I did. If you have any more questions about what I did, ask in the comments and I'll be happy to clarify.
#include <tchar.h>
#include <string>
#include <iostream>
class dlist {
struct node {
string name;
string regno;
node* next;
node* prev;
};
public:
node* head, * tail;
void insert(string, string);
void search(string);
/**
* You're using classes, so go ahead and provide a definition for the
* constructor you're using. This one will initialize head and tail to
* be null
*/
dlist() : head(NULL), tail(NULL) {}
/**
* It's important to delete any memory you allocate so that you don't create a memory
* leak.
*/
virtual ~dlist() {
while (head) {
node* deleteMe = head;
head = head->next;
delete deleteMe;
}
}
};
void dlist::insert(string nn, string r) {
// there is no good reason to set tail=head at this time.
// and you don't need a ptr, that's why we have tail.
node* newNode = new node;
newNode->name = nn;
newNode->regno = r;
// no need to set newNode->prev now, as that will be taken care of later
newNode->next = NULL;
// if we don't have any nodes yet, we need to set head = newNode
// and make head point to tail, which is also head.
if (!head) {
head = newNode;
tail = head;
head->next = tail;
tail->prev = head;
// head points to itself both ways
} else {
// we already have some nodes, so go ahead and
// set newNode to go right after tail, then set tail = newNode
// this will work even when head == tail
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
void dlist::search(string n) {
// it's nice to assign variables when you declare them, when you can
node* ptr = head;
// basically the same as what you had, but we are checking in the case
// that tail has the node we want, which your code did not do.
while (ptr) {
if (ptr->name == n) {
cout << "Name found... " << ptr->name << endl;
}
ptr = ptr->next;
}
}
int _tmain(int argc, _TCHAR* argv[]) {
dlist dl;
string nn;
string n;
string r;
for (int i = 0; i < 5; i++) {
cout << "Enter student's name: ";
cin >> nn;
cout << nn << endl;
cout << "Enter student's Registration Number: ";
cin >> r;
cout << r << endl;
dl.insert(nn, r);
}
cout << "Enter a name to search: ";
cin >> n;
dl.search(n);
return 0;
}