Good Evening! I am trying to create a C++ Linked List that will create a random number & store randoms in 100 nodes. I haven't gotten any errors in the code I created but when I run the program, the output loops the number "42" to the point where I have to terminate the program. Please help. The code is below.
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node{
int xdata;
Node* next;
};
struct Node *head;
void insert_node(int y)
{
Node* temp = new Node;
temp-> xdata = y;
temp-> next = NULL;
if(head==NULL)
{
head=temp;
}
else{
temp->next=head;
head=temp;
}
};
int main(){
int z =rand()%100 + 1;
for(int i=0; i<100; i++)
{
insert_node(z);
}
while(head!=NULL)
{
cout<<head->xdata<<" "<<endl;
}
return 0;
}
You need to advance your head pointer.
while(head!=NULL)
{
cout<<head->xdata<<" "<<endl;
head = head->next;
}
Related
We were tasked with creating a linked list with the following output:
Enter number of nodes: 5
12 4 5 44 45
The linked list:
12 4 5 44 45
I am very confused with linked lists, with my code being the following
#include <iostream>
using namespace std;
struct Node{
int data;
int nodeNumber;
Node *next;
}*nodePtr=NULL,*nodeTemp=NULL,*nodeHead=NULL;
void addNode (int num, int nodeSize);
void displayNode();
int main(){
int size, value;
cout<<"Enter number of Nodes: ";
cin>>size;
for (int i=0; i<size; i++){
cin>>value;
addNode(value,size);
//These are just to check if the data is being stored correctly in the structs
cout<<nodeTemp->data;
cout<<nodePtr->data;
cout<<nodeHead->data;
}
displayNode();
system("pause>0");
}
void addNode(int num, int nodeSize){
int i = 0;
nodePtr = new Node;
nodePtr->data = num;
nodePtr->next = NULL;
if (nodePtr->next == NULL){
nodeHead = nodePtr;
nodeTemp = nodePtr;
nodePtr->next = nodeTemp;
}
else
while(i<nodeSize){
nodeTemp->next = nodeTemp;
}
nodeTemp->next = NULL;
}
void displayNode(){
nodeTemp = new Node;
nodeTemp = nodeHead;
cout<<"The linked list: ";
while(nodeTemp != NULL){
cout<<nodeTemp->data<<" ";
nodeTemp = nodeTemp->next;
}
}
The code however, only outputs the last value inputted. When I checked the data (see line 22,23,24), it seems that the node pointer, temp, and the head always have the same values. I am confused with what my mistakes might be, and any help would be very nice.
Thank you.
You do not have to pass the size variable to the addNode Function. Just take values as much you desire in main funtion and pass them to addNode funtion by looping till the size, then add Nodes to the list as usual.
Your code will look something like this:
#include <iostream>
using namespace std;
struct Node{
int data;
int nodeNumber;
Node *next;
};
Node *nodeHead=NULL;
void addNode (int num);
void displayNode();
int main(){
int size, value;
cout<<"Enter number of Nodes: ";
cin>>size;
for (int i=0; i<size; i++){
cin>>value;
addNode(value);
}
displayNode();
system("pause>0");
}
void addNode(int num){
Node *tail = nodeHead;
Node *nodePtr = new Node;
nodePtr->data = num;
nodePtr->next = NULL;
if (nodeHead == NULL){
nodeHead = nodePtr;
}
else{
while(tail->next != NULL){
tail = tail->next;
}
tail->next = nodePtr;
}
}
void displayNode(){
Node *nodeTemp = new Node;
nodeTemp = nodeHead;
cout<<"The linked list: ";
while(nodeTemp != NULL){
cout<<nodeTemp->data<<" ";
nodeTemp = nodeTemp->next;
}
}
there is no need for size variable while you are inserting a node at the end of the list.
change your function like below:
void addNode(int num){
nodePtr = new Node;
nodePtr->data = num;
nodePtr->next = NULL;
/* if head is null, or list is empty! */
if (nodeHead == NULL){
nodeHead = nodePtr;
}
else{
/* traverse the list to the end and add node at the NULL position */
/* create a chain of nodeHead to nodeTemp */
nodeTemp = nodeHead;
while(nodeTemp-> next != NULL){
nodeTemp = nodeTemp->next;
}
nodeTemp->next = nodePtr;
}
}
this is the code
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
Node *head;
`insert function takes a number as argument`
void insert(int a)
{
Node *temp = new Node();
temp -> data= a;
temp->next = head;
head = temp;
}
void print(void)
{
Node *temp1;
temp1 = head;
while(head != NULL)
{
cout<<temp1->data<<endl;
temp1 = temp1->next;
}
}
**main function**
int main()
{
head = NULL;
int a;
int b;
cout<<"how many elements do you want to insert";
cin>>b;
for (int i = 0;i < b; i++)
{
cout<<"enter a number"<<endl;
cin>>a;
insert(a);
print();
}
return 0;
}
when I enter the number to insert it shows program has stopped working. I am trying to insert numbers to linked list and print it ever time I add a number.I have checked for many other errors but it has none.
Looks like it might be due to an infinite loop like one of the comments says. Try updating the loop in the print function like this:
void print() {
Node *temp1;
temp1 = head;
// Here, I switched `head` with `temp1`
while (temp1 != nullptr) {
std::cout << temp1->data << std::endl;
temp1 = temp1->next;
}
}
this is my code for inserting a node at the head of the linked list. there is no error but my printllist function is not working I don't know why. I think I have done silly somewhere. help me out I am stuck here for 3 hours.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Node
{ public:
int data;
Node* next;
};
void insert(int y,Node* head){
Node *temp=new Node();
temp->data=y;
temp->next=head;
head=temp;
}
void printllist(Node *head){
if(head=NULL)
{
cout<<"list is empty"<<endl;
}
else
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
}
}
int main(){
Node *head;
head=NULL;
int x,y,z;
cout<<"hey can you tell me how many number you want to insert"<<endl;
cin>>x;
for(int i=0;i<x;i++)
{
cout<<"enter the data that you want to insert"<<endl;
cin>>y;
insert(y,head);
printllist(head);
}
return 0;
}
You need to pass the head of the linked list by reference so that the changes which are made in the insert function actually remain preserved between the function calls. Also, keep in mind that all the new nodes are inserted at the beginning of the linked list. So, for example if you enter 1,2,3 as the three elements of the linked list, the print function will print 3 2 1.
Try this:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Node
{ public:
int data;
Node* next;
};
void insert(int y,Node** head){
Node* temp = new Node();
temp->data = y;
temp->next = *head;
*head = temp;
}
void printllist(Node* head)
{
if(head==NULL)
cout<<"List is empty";
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
}
int main(){
Node *head;
head=NULL;
int x,y,z;
cout<<"hey can you tell me how many number you want to insert"<<endl;
cin>>x;
for(int i=0;i<x;i++)
{ cout<<"enter the data that you want to insert"<<endl;
cin>>y;
insert(y,&head);
}
printllist(head);
return 0;
}
I want to know what is the problem in creat_list2 function..
I have a problem in this code and as i knew it's in the creat_list2 cause the program run Successfully
when i stop this function it ask the user to enter a numbers that will be stored in the list 1 then print them but the problem as i saw is in the second fuction that creat the second list...,I have to submit my assignment tomorrow so I wish if any body can help me to solve this problem.
#include<iostream>
using namespace std;
struct node{
int x;
node *next;
};
struct snode{
int y;
snode *next;
};
creat_list1(node *&head, node *&tail)
{
int num;
cout<<"enter number\n";
cin>>num;
while(num!=0)
{
node *np=new node;
np->x=num;
if(head==nullptr)
head=np;
else
tail->next=np;
tail=np;
tail->next=nullptr;
cout<<"enter number again\n";
cin>>num;
}
}
creat_list2(node *&head, snode *shead, snode *stail)
{
int sum=0;
while(head!=nullptr)
{
for(int i=0;i<head->x;i++)
sum+=i;
snode *np= new snode;
np->y=sum;
if(head==0)
shead=np;
else
stail->next=np;
stail=np;
stail->next=nullptr;
}
head=head->next;
}
void print_list1 (node *head)
{
while(head!=nullptr)
{
cout<<head->x<<"\t";
head=head->next;
}
}
void print_list2(snode *shead)
{
while(shead!=nullptr)
{
cout<<shead->y<<"\t";
shead=shead->next;
}
}
main()
{
node *head=nullptr, *tail=nullptr;
snode *shead=nullptr, *stail=nullptr;
creat_list1(head,tail);
creat_list2(head,shead,stail);`enter code here`
print_list1(head);
print_list2(shead);
}
There are couple of issues with yours code,
creat_list2(node *&head, snode *shead, snode *stail) if u updated
head like head=head->next; it will reflect at the main.
shead and stail are just a local pointer and any update to just a pointer like stail=np;will not have any changes at the `main
There seems to be typo if(head==0) shead=np; instead it should be
if(shead==0) shead=np; which is causing null pointer exception.
I tried to fix the error at creat_list2 but functionality is still ambiguous to me,
void creat_list2(const node *head, node *&shead, node *&stail) //updated in argument
{
int sum = 0;
while (head != nullptr)
{
//for (int i = 0; i < head->x; i++)
// sum += i;
node *np = new node;
np->x = sum;
if (shead == 0) //error : head instead shead
shead = np;
else
stail->next = np;
stail = np;
stail->next = nullptr;
head = head->next;
}
}
I am beginner and I have created link list, but my code is not working correctly. plz help thx in advance
#include <iostream>
using namespace ::std;
class node{
public:
int data;
node *link;
};
class linklist{
private:
node *head=NULL;
node *tail;
node *temp;
public:
// I think there is some issue but it seems perfect to me plz help
void insert(int n)
{
if(head==NULL)
{
tail=new node;
tail->data=n;
tail->link=NULL;
head=tail;
temp=tail;
}
else
{
tail=new node;
tail->data=n;
tail->link=NULL;
temp->link=tail;
}
}
void print()
{
while(head->link==NULL)
{
cout<<head->data<<endl;
head=head->link;
}
}
};
int main() {
linklist s;
cout<<"how many numbers you want to enter"<<endl;
int n,l;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"enter nummber";
cin>>l;
s.insert(l);
s.print();
}
}
After printing section printing is not doing and it keep printing current element
There are some mistakes in your code.
print function changes head value every time, you need to use local variable. Also your condition for while loop is wrong.
void print()
{
node* t = head;
while(t->link!=NULL)
{
cout<<t->data<<endl;
t=t->link;
}
}
You don't change temp when adding new nodes.
else
{
tail=new node;
tail->data=n;
tail->link=NULL;
temp->link=tail;
temp = tail; // here
}