My else statement in the LinkedLists is crashing the program. I get EXC_ACCESS error on my compiler on head->num=temp->num;
Code:
void ListHandling::LinkedLists(NodePtr temp)
{
temp = NULL;
if (head!=NULL) {
current=head;
while (current->next != NULL) {
current=current->next;
}
current= new node;
current->num= temp->num;
current->next->next=nullptr;
}
else
head = new node;
head->num=temp->num;
head->next=nullptr;
}
Code:
void ListHandling::PrintLinkedLists()
{
current=head;
if (head==nullptr) {
std::cout<<"The list is empty"<<std::endl;
}
else
while (current!=NULL) {
std::cout<<current->num<<",";
current=current->next;
}
}
In main()
Code:
NodePtr temporary = new node;
for (int i=1; i<20; ++i) {
temporary->num = i;
Mylists.LinkedLists(temporary);
}
Mylists.PrintLinkedLists();
Code:
typedef struct node{
int num;
node* next;
}*NodePtr;
class ListHandling {
private:
std::vector<int> List3;
struct sorting{
bool operator () (int i, int j) {return (i<j);}
} ComparedTo;
NodePtr head;
NodePtr current;
public:
ListHandling();
ListHandling(std::vector<int>lst3)
:List3(lst3)
{
head=NULL;
current=NULL;
}
void MergeLists(std::vector<int>& ,std::vector<int>& );
void SortLists();
void PrintSortedList();
void MergeArray(std::array<int, 5>&,std::array<int, 5>&);
void LinkedLists(NodePtr);
void PrintLinkedLists();
};