In: Computer Science
I am having trouble with printing the linked list from user input. Can someone tell me what I am doing wrong.
#include <iostream>
using namespace std;
struct node
{
int info;
node*link;
node*current;
node*prev;
node * head;
node * last;
}
;
void Insert(node*&head,node*&last,int n);
int SplitList(node*&head);
void print(node*&head);
int main()
{
int num;
node*h;
node*l;
cout << "Enter numbers ending with -999"
<< endl;
cin >> num;
Insert(h, l, num);
print(h);
}
void Insert(node *&head,node*&last,int n)
{
int count = 0;
if (head == NULL)
{
head = new node;
head->info = n;
head->link = NULL;
last = head;
count++;
}
else {
last->link = new node;
last->link->info = n;
last->link->link =
NULL;
last = last->link;
count++;
}
}
void print(node*&head)
{
node *current; //pointer to traverse the list
current = head; //set current so that it points
to
//the first node
while (current->link!= NULL) //while more data to
print
{
cout << current->info
<< " ";
current = current->link;
}
}//end print
#include <iostream>
using namespace std;
struct node
{
int info;
node*link;
node*current;
node*prev;
node * head;
node * last;
}
;
void Insert(node *&head,node *&last,int n);
int SplitList(node* head);
void print(node* head);
int main()
{
int num;
node*h=NULL;
node*l=NULL;
cout << "Enter numbers ending with -999" << endl;
while(1)//added loop to run until user enters -999
{
cin >> num;
if(num==-999)//if -999 is entered
break;
Insert(h, l, num);
}
print(h);
}
void Insert(node *&head,node *&last,int n)
{
int count = 0;
if (head == NULL)
{
head = new node;
head->info = n;
head->link = NULL;
last = head;
count++;
}
else {
last->link = new node;
last->link->info = n;
last->link->link = NULL;
last = last->link;
count++;
}
}
void print(node *head)
{
node *current; //pointer to traverse the list
current = head; //set current so that it points to
//the first node
while (current!= NULL) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print
output:
Enter numbers ending with -999
1
2
3
-999
1 2 3
Process exited normally.
Press any key to continue . . .