Linked list head
Oct 16, 2014 at 11:56am UTC
I cannot figure out how to update node *head. I know realize it carries the NULL node from main to the insert program, but it doesn't come out as the new value, so when it reaches display it is still NULL and therefore doesn't have a value to display. Where am I missing adding the value to node *head? Am I using the linked list incorrectly?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#include <iostream>
#include <iomanip>
using namespace std;
struct node
{
int trackNo;
mutable char animalName[50];
double boardCharge;
node *next;
};
//***********************
//Function prototypes
//***********************
void insert(node *head, node *curr);
void showList(node *head);
int main()
{
node *head = new node;
head = NULL;
//**********************************************
//Variable to deteermine whether to repeat or not
//***********************************************
char ContinuationFlag = 'Y' ;
char NextChar = '\0' ;
node*temp;
//********************
//input of information
//********************
while (toupper(ContinuationFlag) == 'Y' )
{
temp = new node;
cout<<"Input the tracking number of the animal" <<endl;
cin>>temp->trackNo;
cout<<"Input the name of the animal" <<endl;
NextChar = cin.peek();
if (NextChar == '\n' )
{
cin.ignore();
}
cin.get(temp->animalName, 50);
cout<<"Input the charge for boarding the animal" <<endl;
cin>>temp->boardCharge;
//*******************************
//send to function that captures list
//**********************************
insert(head,temp);
//*******************
//ask user if they want to continue
//******************************
cout<<endl<<"Do you wish to enter another item? " <<endl;
cout<<"Enter 'Y' or 'N' " <<endl;
cin>>ContinuationFlag;
}
//*********************************
//when user is finish, displays list
//*********************************
showList(head);
return 0;
}
void insert(node *head, node *curr)
{
if (head == NULL)
{
head = curr;
}
else
{
node *temp = new node;
temp =NULL;
temp=head;
temp=temp->next;
curr = temp->next;
head=curr;
}
}
void showList(node *head)
{
char Separator[50]="---------------------------" ;
cout<<endl;
cout<<setw(12)<<setfill(' ' )<<" Tracking Number"
<<setw(20)<<setfill(' ' )<<" Type of Animal "
<<setw(20)<<setfill(' ' )<<" Daily Boarding Charge"
<<endl;
node *curr = new node;
curr=NULL;
curr=head;
while (curr != NULL)
{
cout<<right;
cout<<endl<<setw(9)<<setfill(' ' )<<curr->trackNo;
cout<<setw(18)<<setfill(' ' )<< curr->animalName;
cout<<fixed<<showpoint<<setprecision(2)
<<setw(12)<<setfill(' ' )<<curr->boardCharge
<<endl;
curr=curr->next;
}
}
Topic archived. No new replies allowed.