linked list using class in c++ - c++

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.

Related

pointer class function not printing output?

// 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();

Implementation of Queues using Linked lists

The queue is implemented using linked list: but the program isn't working, what could be the possible mistake?
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
node *next;
};
class queue
{
node *front, *rear;
public:
queue()
{
front=rear=NULL;
}
void insert_in_queue();
void delete_in_queue();
void display_queue();
};
void queue:: insert_in_queue()
{
node *ptr;
ptr= new node;
cout<<"\nInsert element\n";
cin>>ptr->data;
if(rear==NULL)
front=rear=ptr;
else
{ rear->next=ptr;
rear=ptr;
}
}
void queue:: delete_in_queue()
{
node *ptr;
ptr=front;
if(rear==NULL)
cout<<"\nUnderflow!!\n";
else if(front==rear)
front=rear=NULL;
else
front=front->next;
cout<<"\nThe deleted element is:: "<<ptr->data<<"\n";
delete ptr;
}
void queue:: display_queue()
{
node *ptr;
ptr=front;
cout<<"\nThe queue is:\n";
while(ptr!=NULL)
{
cout<<"|"<<ptr->data<<"|";
ptr=ptr->next;
}
}
void main()
{
queue q;
char ch;
int a;
ch='y';
cout<<"this is dynamic que progream\n\n\n";
do
{
cout<<"(1)->Insert / (2)->Delete\n";
cin>>a;
if(a==1)
q.insert_in_queue();
else
q.delete_in_queue();
q.display_queue();
cout<<"\nContinue?(y/n)\n";
cin>>ch;
}while(ch=='y');
cout<<"\nThe final queue is:\n";
q.display_queue();
system("pause");
}
The console screen just disappears after entering the first element, I can't figure out where is the mistake.
In the insert_in_queue() function, after allocating memory for ptr, make sure to use
ptr->next=NULL;

Inserting data in a singly link list by specifying the nth node position

So the logic goes like this:
Suppose the link list consists of (6,7,8) as data and I pass insert(1,5),so the list will be as (5,6,7,8).
Similarly on insert(3,2) link list is (6,7,2,8).
I tried compiling the below code but it gives me an error stating-
Undefined reference to main by '-start'
I tried debugging,even searching for answers but found no help.Kindly suggest a solution.Any further suggestions and bug fixes shall be welcomed.
(I have used codepad for compiling)
#include<iostream>
using namespace std;
class Link_no
{
struct node
{
int data;
node *next;
};
void insert(int n,int d,node *head)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
temp1=head;
{
for(int i=0;i<n-1;i++)
{
temp1=temp1->next;
}
temp->next=temp1;
temp1=temp;
}
}
void print(node *start)
{
node *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
int main()
{
node *head=NULL;
Link_no o1;
o1.insert(1,5,head);
o1.insert(2,7,head);
o1.insert(1,9,head);
o1.print(head);
return 0;
}
}
C++ isnt java, the main does not belong inside a class. The compiler complains because there is no int main() in your code only a int Link_no::main() but that is not the entry point of the program.
Take out int main() from class Link_no. Take out struct node from class Link_no. It should compile.
The following compiles
#include<iostream>
using namespace std;
class Link_no
{
private:
struct node
{
int data;
node *next;
};
node *head;
public:
Link_no(){
head = nullptr;
}
void insert(int n,int d)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
temp1=head;
{
for(int i=0;i<n-1;i++)
{
temp1=temp1->next;
}
temp->next=temp1;
temp1=temp;
}
}
void print()
{
node *temp=head;
while(temp!=NULL)
{
cout << "data is " << temp->data<<endl;
temp=temp->next;
}
}
};
int main()
{
Link_no o1;
o1.insert(1,5);
o1.insert(2,7);
o1.insert(1,9);
o1.print();
return 0;
}
It does not completely do what you want yet only prints out 5 and 9 as data so you need to debug some more.
Edit:
I suggest you take a paper and pen and manually try to do what you're doing in your else since there is something going wrong there.
If you can't find it out on your own the following works for me, I haven't tried testing for extreme cases yet.
#include<iostream>
using namespace std;
class Link_no
{
private:
struct node
{
int data;
node *next;
};
node *head;
public:
Link_no(){
head = nullptr;
}
void insert(int n,int d)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
{
cout << "foo" << endl;
temp1=head;
for(int i=1;i<n-1;i++)
{
temp1=temp1->next;
}
node *temp2 = temp1->next;
temp1->next = temp;
temp->next=temp2;
}
}
void print()
{
node *temp=head;
cout << "link" << endl;
while(temp!=NULL)
{
cout << "data is " << temp->data<<endl;
temp=temp->next;
}
}
};
int main()
{
Link_no o1;
o1.insert(1,5);
o1.print();
o1.insert(2,7);
o1.print();
o1.insert(1,9);
o1.insert(2,6);
o1.print();
return 0;
}

link list insert() print();

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
}

Solve - error C2440: 'initializing' : cannot convert from 'LinkedList *' to 'LinkedList'

Found the solution - posted in answers.
I get this error when I try to compile my LinkedList implementation and I cannot find where the problem lies. It points to this but I cannot find the fault.
LinkedList my =new LinkedList();
The class and the main function: -
struct Node {
int data;
Node* next;
};
class LinkedList {
private:
public:
void LinkedList::init_list(struct Node *&head);
bool LinkedList::is_list_empty(struct Node *head);
Node* LinkedList::search(struct Node *head,int value);
int LinkedList::deleteNode(struct Node *head,int value);
int LinkedList::deleteat(struct Node *&head,int i);
bool LinkedList::insert(struct Node *&head,int value);
bool LinkedList::insertat(struct Node *&head,int value, int i);
int LinkedList::list_length(struct Node *head);
void LinkedList:: display(struct Node *head);
};
int main(){
LinkedList my =new LinkedList();
Node *head;
Node *&ahead = *&head;
my.init_list(ahead);
my.display(head);
my.insert(ahead,1111);
//cout<<"1sr time";
cout<< "length- " <<my.list_length(head)<<endl;
//cout<< "deleting--- " <<deleteat(ahead,1)<<endl;
my.insert(ahead,34);
my.insert(ahead,32);
my.insert(ahead,44);
my.display(head);
cout<<endl;
my.insertat(ahead,4444,1);
my.display(head);
system("Pause");
return 0;
}
The complete code:-
#include <iostream>
#include <exception>
using namespace std;
struct Node {
int data;
Node* next;
};
class LinkedList {
private:
public:
void LinkedList::init_list(struct Node *&head);
bool LinkedList::is_list_empty(struct Node *head);
Node* LinkedList::search(struct Node *head,int value);
int LinkedList::deleteNode(struct Node *head,int value);
int LinkedList::deleteat(struct Node *&head,int i);
bool LinkedList::insert(struct Node *&head,int value);
bool LinkedList::insertat(struct Node *&head,int value, int i);
int LinkedList::list_length(struct Node *head);
void LinkedList:: display(struct Node *head);
};
void LinkedList:: init_list(struct Node*&head){
head=NULL;
}
bool LinkedList:: is_list_empty(struct Node*head){//returns true(1) if the list is empty
if (head==NULL){
return true;
}
return false;
}
Node* LinkedList:: search(struct Node*head, int value){
Node* pointer;
pointer=head;
while (pointer->next != NULL){
if (pointer->data==value){
cout<<"value found" <<endl;
return pointer;
}
pointer=pointer->next;
}
cout<<"value not found!!"<<endl;
return NULL;
}
int LinkedList:: deleteNode(struct Node *head, int value){
Node* pointer;
Node* pointer2;
pointer=head;
while (pointer != NULL){
pointer2 = pointer->next;
if(pointer2 !=NULL && pointer2->data==value){
pointer->next=pointer2->next;
return pointer2->data;
}
else if(pointer2==NULL){
cout<<"value not here - cannot delete";
return NULL;
}
pointer=pointer->next;
}
}
int LinkedList:: list_length(struct Node*head){
int count=0;
Node* p = head;
while (p != NULL)
{
++count;
p = p->next;
}
return count;
}
int LinkedList:: deleteat(struct Node *&head, int loc){
int i=2;
Node*pointer =head;
Node* pointer2;
if(list_length(head)<loc || loc<1){
cout<< "invalid location";
return NULL;
}
if (loc==1){
if(list_length(head)>1){
int val=pointer->data;
int tempval=pointer->next->data;
deleteat(head,2);
pointer->data=tempval;
return val;
}
else{
head=NULL;
return NULL;
}
}
while (pointer->next != NULL){
pointer2 = pointer->next;
if(i==loc){ //112 - 34 - 32 - 44
int val = pointer2->data;
pointer->next=pointer2->next;
return val;
}
pointer=pointer2;
i++;
}
}
bool LinkedList:: insert(struct Node *&head,int value)
{
//try{
Node *q = new Node;
q->data=value;
q->next=NULL;
if(head==NULL){
head=q;
return true;
}
Node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=q;
return true;
//}
//catch(NullReferanceException * e ){
// cout<< "insertion was not successful" <<endl;
// return false;
//}
}
bool LinkedList:: insertat(struct Node*&head, int value,int loc){
Node *q = new Node; //112 - 32- 44
q->data=value;
if(loc>list_length(head)){
insert(head,value);
return true;
}
else if(loc<=0){
cout<<"invalid location";
return false;
}
else if(loc==1){
q->next=head;
head=q;
return true;
}
else{
int i=1;
Node*p =head;
while( i<loc-1){
p=p->next;
i++;
}
q->next=p->next;
p->next=q;
return true;
}
}
void LinkedList:: display(struct Node *head){
Node*p = head;
while(p!=NULL){
cout << p->data<< " ";
p=p->next;
}
}
int main(){
LinkedList my =new LinkedList();
Node *head;
Node *&ahead = *&head;
my.init_list(ahead);
my.display(head);
my.insert(ahead,1111);
//cout<<"1sr time";
cout<< "length- " <<my.list_length(head)<<endl;
//cout<< "deleting--- " <<deleteat(ahead,1)<<endl;
my.insert(ahead,34);
my.insert(ahead,32);
my.insert(ahead,44);
my.display(head);
cout<<endl;
my.insertat(ahead,4444,1);
my.display(head);
system("Pause");
return 0;
}
new LinkedList(); gives you a pointer to a LinkedList (i.e. LinkedList*). You cannot assign a LinkedList* to a LinkedList since a LinkedList* is not a LinkedList.
What you should probably do is LinkedList* my = new LinkedList;.
So I found where the fault lied, and this is how it should have been -
int main(){
LinkedList* my = new LinkedList;
Node *head;
my->start = head;
Node *&ahead = *&head;
my->init_list(ahead);
my->display(head);
my->insert(ahead,1111);
//cout<<"1sr time";
cout<< "length- " <<my->list_length(head)<<endl;
//cout<< "deleting--- " <<deleteat(ahead,1)<<endl;
my->insert(ahead,34);
my->insert(ahead,32);
my->insert(ahead,44);
my->display(head);
cout<<endl;
my->insertat(ahead,4444,1);
my->display(head);
system("Pause");
return 0;
}