I wrote this programe for circular singly linked list but its not adding nodes to it
Code:
#include <iostream>
#include <conio.h>
using namespace std;
struct Node
{
int data;
Node *nextptr;
};
class CLLIST{
private:
Node*firstptr;
Node*lastptr;
public:
CLLIST(){
cout << "Constructor Called !";
firstptr=lastptr=NULL;
}
void insert_at_back(int val){
if(firstptr==NULL) //it means start of C.LIST
{
firstptr = new Node;
firstptr->data=val;
firstptr->nextptr=firstptr;
} else{
Node*temp1;
Node*temp2;
temp1 = firstptr;
temp2 = temp1->nextptr;
temp2->data = val; //inserted at back
temp2->nextptr=firstptr; //circle completed
}
}
void printit(){
// functiont o print all the circular link lists data
Node* temp3ptr;
temp3ptr = firstptr;
while(temp3ptr->nextptr!=firstptr)//traversing
{
cout << temp3ptr->data;
cout << endl;
}
}
// 1-return pointer to node give the index positio of node is list Null if node dosent exist
void pointer_to_node(int value){
Node*temp4ptr;
temp4ptr = firstptr;
int count = 0;
while(temp4ptr->nextptr != firstptr) //traversing
{
if(count == value){
cout << "Your Required Pointer is : \n";
cout << "Index No: " << value;
cout << temp4ptr;
} count++;
}
temp4ptr = NULL;
cout << "Can't You Think in Bounds";
cout <<temp4ptr;
}
//2-call the size of lise
int size_of_list(){
Node*temp5ptr;
temp5ptr = firstptr;
int size = 0; //size calculating variable
while(temp5ptr->nextptr !=firstptr)
{
size++;
}
cout << "Size if : ";
cout << size;
return(size);
}
// B-Value to be inserted at the end of node:
void insert_value(int value2){
if(firstptr==NULL) //it means starts of C.LIST
{
Node*temptr;
firstptr = temptr;
firstptr->data = value2;
firstptr->nextptr=firstptr;
} else {
Node*temptr1;
Node*temptr2;
temptr1 = firstptr;
while(temptr1->nextptr != firstptr) //traversing
{
temptr2 = temptr1->nextptr;//inserted
temptr2->nextptr = firstptr; //circle completed
}
}
}
};
int main()
{
CLLIST obj1;
obj1.insert_at_back(25);
obj1.insert_value(23);
obj1.size_of_list();
obj1.printit();
cout << " \n \n Done !";
getch();
}
Re: I wrote this programe for circular singly linked list but its not adding nodes to
OK, and what have you done to debug this program? Did you run your program under the debugger to see where it goes wrong?
Did you write this code? If so, then you need to debug this yourself first, then ask questions on what you've discovered in the code that takes the wrong path, variable not set correctly etc.
And even when you discover what is actually wrong, you more than likely would know how to fix it.
Regards,
Paul McKenzie
Re: I wrote this programe for circular singly linked list but its not adding nodes to
When people try to debug linked lists, my advice (along with stepping through with the debugger) is to get pencil and paper out and draw the sequence of node connections you expect to see when a certain action is applied. Step through with the debugger and see if it does the same. In drawing it out step by step you may even discover a fault with the original algorithm you were trying to implement.