I am not an affluent programmer. My code for this linked list is printing what seems to be not only a series of endlines, but also a zero that I have no idea where it is coming from. You might notice that I insert values into the linked list using a for loop. Even before the for loop, the linked list contains a zero. Why? I assumed that when the linked list was created it would be empty. I really need to remove the zero from the data.
#include <iostream>
#include <cstring>
using namespace std;
struct Node{
int key;
Node* next;
};
void insert(Node* & List, int k, int posn){
//assume 1 <= posn <= length of the List + 1
if (List == NULL){
List = new Node();
List->key = k;
List->next = NULL;
}
else if (posn == 1){
Node* temp = new Node();
temp->key = k;
temp->next = List;
List = temp;
}
else
insert(List->next, k, posn-1);
}
void print(Node* List){
if(List!=NULL){
cout << List->key<<",";
print(List->next);
}
cout << endl;
}
int main(){
Node* myList;
myList = new Node();
print(myList);
for(int j = 1; j<=10;j++)
insert(myList, j, j);
print(myList);
}