// i am trying to make array of
//pointer class node (table) and print its
// function print_val() but it keeps giving me an error i don't
// understand
==================================================================
#include<iostream>
using namespace std;
class node {
int val;
node *next;
public:
node(int x){
val=x;
next=NULL;
}
void print_val(){
cout<<val;
}
};
int main(){
//node *table;
node *object=new node(5);
node **table=new node*[7];
for(int i=0;i<7;i++){
int x;
cin>>x;
node *temp=new node(x);
table[i]=temp;
}
for (int i=0;i<7;++i){
cout<<table[i]->print_val();
}
object->print_val();
//table=new node[7];
return 0;
}
for (int i=0;i<7;++i){
cout<<table[i]->print_val();
In above statement table[i]->print_val(), cout expecting one integer value but print_val() not returning. Modify the print_val() function as
int print_val(){
cout<<val;
return val;
}
This is wrong
cout << table[i]->print_val();
you're passing to cout void which is the return value of your function
it should be
table[i]->print_val();
Related
I have been trying to create a linked list in c++. But only the last element of the linked list is displayed. I have searched for the error but i cannot find it. I have implemented the logic that I learned form c language. All the nodes are connecting properly. But still i cannot find the error.
This logic works on c language.
Please help.
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
}*head,*newnode,*temp;
node* getnode();
node* create(int);
void display(node*);
int main()
{
int n;
head=getnode();
cout<<"Enter the no of nodes: ";
cin>>n;
head=create(n);
display(head);
return 0;
}
node *getnode()
{
head=new node();
head->next=nullptr;
return(head);
}
node *create(int n)
{
head=getnode();
cout<<"Enter the value of node 1: ";
cin>>head->data;
temp=getnode();
temp=head;
for(int i=1;i<n;i++)
{
newnode=getnode();
cout<<"Enter the value of node "<<i+1<<": ";
cin>>newnode->data;
newnode->next=nullptr;
temp->next=newnode;
temp=newnode;
}
return(head);
}
void display(node *head)
{
while(head!=nullptr)
{
cout<<"->"<<head->data;
head=head->next;
}
}
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
node(int x)
{
data=x;
next=nullptr;
}
}*head,*newnode,*temp;
node* create(int);
void display(node*);
int main()
{
int n;
cout<<"Enter the no of nodes: ";
cin>>n;
head=create(n);
display(head);
return 0;
}
node *create(int n)
{
for(int i=0;i<n;i++)
{
int x;
cout<<"Enter the value of node "<<i+1<<": ";
cin>>x;
newnode=new node(x);
if(i==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
return(head);
}
void display(node *head)
{
while(head!=nullptr)
{
cout<<"->"<<head->data;
head=head->next;
}
}
Ive just created a constructor for creating a new node and used a temp pointer for keeping track of the last inserted element in the list. Do keep in mind it is always best to have your head pointer fixed and use another pointer for traversals. The problem with your code was that your head pointer points to the last inserted element always.
Use local variables
*head,*newnode,*temp are globals. Everytime you call a function, you're overwriting them. Make them local variables.
Memory leaks
You also leak memory in main() with:
head=getnode();
And in create() with:
temp=getnode();
Put it all together
https://repl.it/repls/MedicalEquatorialFlashmemory#main.cpp
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next=NULL;
};
class list{
Node *head,*tail;
public:
void insert(int data){
Node *node =new Node;
node->data=data;
node->next=NULL;
if(head==NULL){
head=node;
tail=node;
}else{
tail->next=node;
tail=tail->next;
}
}
void show(){
Node *n=head;
while(n->next!=NULL){
cout<<n->data<<" ";
n=n->next;
}
cout<<n->data<<endl;
}
};
int main(){
list x;
int n;
for(int i=0;i<10;i++){
cin>>n;
x.insert(n);
}
x.show();
return 0;
}
the program is compiling perfectly but while running it stops and its not working if i put the insert function in loop only then the problem arises but otherwise it is running fine
When you declare a pointer, it doesn't have a default value of NULL necessarily.
related question
Initialize head and tail with NULL and the program will be run successfully.
I'm new to data structure.
I'm making a C++ program for insertion of an element in binary search tree .
The program compiles without any error but when I'm running the program , after giving the first input n , the program stops working.
Kindly help me in making this program work properly.
My code follows up as:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
};
struct node *root=NULL ,*par=NULL ,*pos=NULL,*save=NULL , *ptr=NULL ;
struct node* newNode(int data)
{
struct node* newnode= (struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->right=NULL;
newnode->left=NULL;
return newnode;
}
void findpos(int data)
{
if(root==NULL)
{
par=NULL,pos=NULL;
return;
}
if(root->data==data)
{
par=NULL , pos=root;
return;
}
if(data<root->data)
{
save=root; ptr=root->left;
}
else{
save=root; ptr=root->right;
}
while(ptr!=NULL)
{
if(ptr->data==data)
{
par=save; pos=ptr;
return;
}
if(data<ptr->data)
{
save=ptr;
ptr=ptr->left;
}
else{
save=ptr;
ptr=ptr->right;
}
}
pos=NULL; par=save;
return;
}
void insert(int data)
{
findpos(data);
if(pos!=NULL)
{
return;
}
pos=newNode(data);
if(data<par->data)
par->left=pos;
else
par->right=pos;
return;
}
int main()
{
struct node *root= newNode(4);
root->left=newNode(3);
root->left->left=newNode(2);
root->right=newNode(6);
int n;
cin>>n;
insert(n);
cout<<pos->data; //just trying to see if it works
return 0;
}
The root pointer declared in the main() override the global root pointer. Then at the first findpos(), the root (global) is still NULL.
So, simply replace that code:
int main()
{
struct node *root= newNode(4);
root->left=newNode(3);
root->left->left=newNode(2);
root->right=newNode(6);
int n;
cin>>n;
insert(n);
cout<<pos->data; //just trying to see if it works
return 0;
}
By this one:
int main()
{
root= newNode(4);
root->left=newNode(3);
root->left->left=newNode(2);
root->right=newNode(6);
int n;
cin>>n;
insert(n);
cout<<pos->data; //just trying to see if it works
return 0;
}
Changed the main method so that it modifies root in global namespace; before you were declaring a new node called root localy within main.
int main() {
// struct node *root = newNode(4); // you made this root a member of main.
// not a global member anymore.
root = newNode(4); // use this
// Populate some nodes
root->left=newNode(3);
root->left->left=newNode(2);
root->right=newNode(6);
/* tree so far:
* 4
* / \
* 3 6
* /
* 2
*/
int n;
cin >> n;
cout << "user input " << n << "\n";
insert(n);
cout << pos->data; //just trying to see if it works
return 0;
}
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
}
I am trying this linked list using class in C++. I have implemented tree using the same approach before. But in the below code, it seems that the next pointer in the linked_list isn't working as example. The line in the main function has been commented, is where the main problem lies.
#include<iostream>
#include<cstdio>
using namespace std;
class node{
node* next;
char data;
public:
node(char x){
data=x;
next=NULL;
}
node(){
data='~';
next=NULL;
}
node* get_next_node(){
return next;
}
char get_data(){
return data;
}
void set_data(char x){
data=x;
}
};
class Linked_List{
node *Head;
public:
Linked_List(char v){
Head= new node(v);
}
Linked_List(){
Head= new node();
}
void append(char v){
node *Cur;
for(Cur=Head;Cur!=NULL;Cur=Cur->get_next_node()){
;
}
Cur= new node(v);
cout<<"appending"<<v<<"to"<<Head->get_data()<<endl;
}
node* get_Head(){
return Head;
}
void clear(){
Head=NULL;
}
void show(){
node *Cur;
for(Cur=Head;Cur!=NULL;Cur=Cur->get_next_node()){
cout<<Cur->get_data()<<" ";
}
}
};
class Graph{
int vertices;
public:
Linked_List *arr;
Graph(int v){
vertices=v;
arr=new Linked_List[v];
}
void addEdge(char x, char y){
int i;
bool flag;
bool flag2=false;
for(i=0;i<vertices;i++){
if(arr[i].get_Head()->get_data()==x){
arr[i].append(y);
flag=true;
break;
}
else if(arr[i].get_Head()->get_data()=='~'){
flag=false;
break;
}
}
if(flag==false){
arr[i].get_Head()->set_data(x);
arr[i].append(y);
}
/*int j;
for( j=0;j<vertices;j++){
if(arr[j].get_Head()->get_data()==y){
flag2= true;
break;
}
if(arr[j].get_Head()->get_data()=='~'){
break;
}
}
if(flag2==false){
arr[j].get_Head()->set_data(y);
}*/
}
void show(){
for(int i=0;i<vertices;i++){
arr[i].show();
cout<< endl;
}
}
};
int main(){
int v;
char x,y;
cin>>v;
Graph bfs(v);
int edge;
cin>>edge;
for(int i=0;i<edge;i++){
cin>>x>>y;
bfs.addEdge(x,y);
}
bfs.show();
/*Linked_List ll('4');
ll.append('5');
ll.append('6');
char a=ll.get_Head()->get_data();
cout<<a;
a=ll.get_Head()->get_next_node()->get_data();
cout<<a;*/
char a=bfs.arr[0].get_Head()->get_data();
cout<<a<<endl;
if(bfs.arr[0].get_Head()->get_next_node()){ //this condition should be true if there
//is other values. but it's not working.
a=bfs.arr[0].get_Head()->get_next_node()->get_data();
}
cout<<a;
return 0;
}
/*
4
5
0 1
0 2
1 3
1 2
2 1
*/
In class Linked_List, modify append():
void append(char v){
node *Cur;
for(Cur=Head;Cur->get_next_node()!=NULL;Cur=Cur->get_next_node()){
;
}
Cur->set_next_node(new node(v));
cout<<"appending"<<v<<"to"<<Head->get_data()<<endl;
}
In class node, add set_next_node() method:
void set_next_node(node *n)
{
this->next=n;
}
In a linked list, every next of a node should contain the next node. But what you've done is loop till Cur is NULL. If you do that, you cannot set the next of the last node to the new node.
To add a new node after the current node, the set_next_node() method is used.