#include <iostream>
using namespace std;
int main()
{
struct node
{
int data;
node * next;
};
node * head;
node * n;
node * temp;
node * q;
int number;
cout << "Enter numbers";
cin >> number;
n = new node;
n->data = number;
head = n;
temp = n;
while (cin >> number)
{
while (number != -500)
{
n = new node;
n->data = number;
temp->next = n;
temp = n;
}
}
while (head != NULL)
{
cout << head->data;
head = head->next;
}
}
I don't understand why this would not work. The program creates a new node then sets whatever the user entered equivalent to the variable data of that new node then it makes head and temp point to the new node. Then it gets the users second input and compares it to -500 and if it evaluates as true it creates a new node puts the data of the second input into the variable data then it links the first node and second node together then it makes temp point to the second node. If the condition of the 2nd while loop is false it goes to the third which is where it is suppose to print the list.
Who sets the last node's next to NULL?
At n = new node; n->next is not NULL but undefined, in Debug versions usually it is 0xcccccccc or something similar value to make it visible that it is not initialized. If you try to dereference it, you will get an access violation.
while (cin >> number) { // this loop is endless, because you can always read the user input data (unless some exception happen)
while (number != -500)
{
n = new node; // you already have data allocated. no need to allocate it once more
n->data = number;
temp->next = n;
temp = n;
// as was already mentioned: set n->next to NULL
} }
if you want to break after you checked that the number is not -500, then you can do the following instead:
while (cin >> number) {
if (number != -500) { ...; // do your stuff
break;
}
}
And, by the way, you have a memory leak. If you use flat C pointers then consider delete operator to clear your memory. For that purpose, you need to know where your list starts(basically, head) and iterate through a whole list invoking delete node.
Please, also consider, that it is a bad code style to write something like:
while (cin >> number)
You can also try this-
This code makes a linked list on user input and printlinkedlist (function) print the linked list created.
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* next;
};
void printlinkedlist(node* node)
{
int c=0; //taken just for good looking output
while(node!=NULL)
{
if(c>0)
{
cout<<"->"<<node->data;
node = node->next;
}
else
{
cout<<node->data;
node = node->next;
c++;
}
}
}
int main() {
int n;
cout<<"Enter no. of nodes=";
cin>>n; //User enters number of nodes he want.
int num,c=0; //initialized c for setting head with second node..
node* head = new node; //initialized head node
node * temp = new node; //initialized temp node
cin>>num;
head->data=num;
for(int i=2;i<=n;i++)
{
if(c==0)
{
cin>>num;
temp->data=num;
head->next=temp; //head point to second node i.e. temp
c++;
}
else
{
cin>>num;
node * temp1 = new node; //initialize other temp node for every value
temp1->data=num;
temp->next=temp1; //point to temp1 to temp
temp=temp1; //set temp as temp1
}
}
printlinkedlist(head);
}
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
};
// a function to create linked list passing pointer to pinter
void create_list(node**head)
{
int num;//num is the data
cin>>num ;
node*new_node=new node();//
new_node->data=num;
new_node->next=NULL;
node*temp;
if(*head==NULL)
{
*head=temp=new_node;
}
else{
temp->next=new_node;
temp=new_node;
}
}
void print_list(node* head)
{
if(head == NULL)
{
cout<<"empty list"<<endl;
}
node *ptr = NULL;
ptr=head;
cout<<"data in the list: "<<endl;
while(ptr!=0)
{
cout<<ptr->data<<endl;
ptr=ptr->next;
}
}
int main()
{
int n,i; //
cout<<"enter the number of nodes:"<<endl;
cin>>n;
cout<<"enter data as num:"<<endl;
node* head=NULL;
for(i=0;i<n;i++)
{
create_list(&head);//passing the address of head
}
print_list(head);
return 0;
}
Related
I have no idea why display function is not displaying anything other than the first node's data. I've tried switching the While(p!=NULL) to while(p->next!= NULL but when I do that instead of only the first node's data displaying no data is being displayed.
#include <iostream>
using namespace std;
class Node {
public:
int no;
Node* next;
};
Node* createNode(int no1) {
Node* n = new Node();
n->no = no1;
n->next = NULL;
return n;
}
void addValue(int x, Node** head) {
//insert first node into linked list
Node* n = createNode(x),*p = *head;
if (*head == NULL) {
*head = n;
}
//insert second node onwards into linked list
else {
while (p->next!= NULL) {
p->next = n;
p = p->next;
}
}
}
void display(Node *head) {
Node* temp = head;
// temp is equal to head
while (temp->next!=NULL) {
cout << temp->no;
temp = temp->next;
}
}
int main() {
int num; char choice;
Node* head = NULL;
do {
cout << "Enter a number : ";
cin >> num;
addValue(num,&head);
cout << "Enter [Y] to add another number : ";
cin >> choice;
} while (choice == 'Y');
cout << "List of existing record : ";
display(head);
return 0;
}
I've tried changing the contents fo the else while loop in the addRecord function to p = p->next; p->next = n; in that order to no avail.
In the while loop, it should be
while (p->next!= NULL) {
p = p->next;
}
p->next = n;
Traverse until the end of linked list is reached and then, add the new entry.
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;
}
}
In the below code, the number stored inside the last element is only being displayed, not the rest of elements.
#include <stdio.h>
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
struct node * Start = NULL;
struct node * End = NULL;
void CreateList(int num)
{
struct node *temp = new struct node;
int data,i=0;
while(true)
{
cout<<"Enter data \n";
cin>>data;
temp->data = data;
temp->next = Start;
if(Start==NULL)
{
Start = End = temp;
}
else{
End->next = temp;
End = temp;
}
if(i==num-1)
break;
i++;
}
}
void Display()
{
struct node* temp1;
temp1 = Start;
do{
cout<<temp1->data<<endl;
temp1=temp1->next;
} while(temp1!=Start);
}
int main()
{
int num;
cout<<"How many elements you want to input?\n";
cin>>num;
CreateList(num);
Display();
return 0;
}
I have done it using structure not classes. I have used two non-dynamic pointers 'Start' and 'End'. The main problem is coming in Display() function. The Display() is printing the value of the last element.
Output:
(Click image to enlarge)
You are not creating the required number of nodes in CreateList. You are creating a node using
struct node *temp = new struct node;
and are reusing the same node in the loop.
Here's an updated version of the function.
void CreateList(int num)
{
int data,i=0;
while(true)
{
cout<<"Enter data \n";
cin>>data;
// Create a new node for every data
struct node *temp = new struct node;
temp->data = data;
temp->next = Start;
if(Start==NULL)
{
Start = End = temp;
}
else{
End->next = temp;
End = temp;
}
if(i==num-1)
break;
i++;
}
}
Suggestion for futher cleanup. In C++, you don't need to use struct node. Just node is sufficient.
node* Start = NULL;
node* End = NULL;
and
// Create a new node for every data
node *temp = new node;
I'm attempting to add items to the end of the list and remove them from the beginning of the list. The program compiles but crushes when I try to add items. I'm new to this concept an it's taking some time to settle in completely. Any help is appreciated..
Thanks!
#include<iostream>
using namespace std;
struct myNode
{
int val;
struct myNode *next;
};
class Cll
{
public:
myNode* head = new myNode;
myNode* tail = new myNode;
Cll()
{
head = NULL;
tail = NULL;
}
myNode* createAnode(int value)
{
myNode* temp;
temp = new myNode;
temp->val = value;
temp->next = NULL;
return temp;
}
void addingValues()
{
int numb;
cout<<"Enter the number to be added: ";
cin>>numb;
myNode *temp, *p;
temp = createAnode(numb);
p = head;
p = p-> next;
temp -> next = NULL ;
p -> next = temp;
}
void deletingValues()
{
myNode *s;
s = head;
head = s->next;
}
void showValues()
{
struct myNode *temp2;
temp2 = head;
while (temp2)
{
cout<<temp2->val<<"->";
temp2 = temp2->next;
}
cout<<"NULL"<<endl;
}
};
int main()
{
int pick ;
Cll cll;
int again;
do
{
cout<<"1.add"<<endl;
cout<<"2.delete"<<endl;
cout<<"3.show"<<endl;
cout<<"Enter choice : ";
cin>>pick;
switch(pick)
{
case 1:
cll.addingValues();
cout<<endl;
break;
case 2:
cll.deletingValues();
break;
case 3:
cll.showValues();
cout<<endl;
break;
}
cout << "Enter 1 to see again, enter 2 to quit"<< endl;
cin >> again;
} while (again == 1);
}
You are programming in c++, so I recommend to use std::list.
But if you like to do it yourself, and you like to ad a node at front of your single liked list, you first have to check if it is the first node or not.
If it is the first node your new node ist head and tail. If not the new node is the the head and its successor is the old head of the list. Adapt your code like this:
void addingValues()
{
int numb;
cout << "Enter the number to be added: ";
cin >> numb;
myNode *newNode = createAnode(numb);
if ( head == NULL ) // if list is empty the new node is the head and the tail
{
head = tail = newNode;
return;
}
tail->next = newNode; // successor of last node is new node and new node is tail
tail = newNode;
}
To delete a node from front of the list you have to check if the list is not empty and if it was the last node in the list:
void deletingValues()
{
if ( head == NULL )
return;
myNode *temp = head->next;
delete head;
head = temp;
if ( head == NULL )
tail == NULL;
}
I am trying to insert element at the end of linked list but the while loop doesn't terminate. I am not able to understand why is this happening. Here is my code.
I am calling this function inside my main() function.
struct node{
int data;
struct node* link;
};
struct node * head;
void insert_last(int element){
struct node * temp = (node*)malloc(sizeof(struct node));
temp->data = element;
temp->link = NULL;
if(head==NULL){
head = temp;
}
struct node * temp1 = head;
while(temp1->link!=NULL){
temp1 = temp1->link;
}
temp1->link = temp;
}
Here is the main method:
int main()
{
head = NULL;
printf("Enter the no. of nodes or elements you want to make linked list of. ");
int n;
scanf("%d",&n);
int element = 0;
for(int i = 0; i<n; i++){
printf("Enter the element\n");
scanf("%d",&element);
insert_last(element);
std::cout<<"Element inserted\n\n";
}
//print_recursive(head);
print();
}
That's easy.
if(head==NULL){
head = temp;
}
In that case, you are already done with what are you doing. If you continue, temp1 becomes the temp. Then temp1->link = temp; makes this node point to itself. Second insertion will never find end because your list is circular now and while(temp1->link!=NULL) will never end.
What you should do is simply put return;.
if(head==NULL){
head = temp;
return;
}