#include <iostream>
usingnamespace std;
struct abc
{
int data;
abc *next;
};
abc *head1 = NULL;
abc *temp1 = head1;
void showdata()
{
abc *temp = head1;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
void makenodesONE()
{
abc *newnode = new abc;
cout << "Enter the data in node: " << endl;
cin >> newnode->data;
newnode->next = NULL;
if (head1 == NULL)
{
head1 = newnode;
}
else
{
abc *temp1 = head1;
while (temp1->next != NULL)
{
temp1 = temp1->next;
}
temp1->next = newnode;
}
}
void split()
{
while (temp1->next != NULL)
{
cout << temp1->data << "->";
temp1 = temp1->next->next;
}
cout << temp1->data << endl;
}
int main()
{
int x;
cout << " 1ST LINKED LIST:" << endl;
cout << "How many nodes you want to make:" << endl;
cin >> x;
for (int i = 0; i < x; i++)
{
makenodesONE();
}
cout << endl;
split();
system("pause");
}
The problem is that the temp1 will become null for either odd number of linked list nodes or even. What changes should I make so that it prints correct linked list in both cases? Thanks
Your split() function relies on temp1 being set to .... something upon entry. You should set it inside the function. Ideally the function should also take whatever parameters it needs.
Building (and deleting from) singly linked lists is easy if you use a pointer to a pointer. This points to the head pointer, or to a node's "next" pointer.
First, let's change showdata() so it can print any list. After all, we have several:
I have the same problem of you..,i tried the previous code there is a problem when you input a diffrent non arder numbers ,the code dosn't work in a right way..how i can fix this?