Jan 22, 2013 at 12:49am UTC
Hi, I am implementing a simple linked list....Any idea why my print function is not working?
#include <iostream>
#include<stdio.h>
using namespace std;
struct litem {
int data;
litem* next;
}*head=NULL;
void insert_item(int val)
{
litem *temp,*tmp;
if(head==NULL)
{
head = new litem;
head->data=val;
head->next=NULL;
}
temp=head;
if (temp!=NULL){
while(temp->next!=NULL)
{temp=temp->next;}
temp=new litem;
temp->data=val;
temp->next=NULL;
cout<<temp->data;
}
}
void print(){
litem *temp2;
temp2=head;
while(temp2->next!=NULL){
cout<<temp2->data;
temp2=temp2->next;
}
}
int main()
{
insert_item(3);
insert_item(2);
insert_item(1);
print();
return 0;
getchar();
}
Jan 22, 2013 at 1:20am UTC
Hi,
I copied your code into a code blocks project and I am seeing that you get to print() and you are successfully setting temp2 to head.
The problem is that temp2->next is NULL (and therefore so is head->next.
There must be an issue with your insert function.
I'll take a look now.
-Mike
Jan 22, 2013 at 1:29am UTC
This function
void insert_item(int val)
{
litem *temp,*tmp;
if(head==NULL)
{
head = new litem;
head->data=val;
head->next=NULL;
}
temp=head;
if (temp!=NULL){
while(temp->next!=NULL)
{temp=temp->next;}
temp=new litem;
temp->data=val;
temp->next=NULL;
cout<<temp->data;
}
is invalid. Consider the case when head == NULL. You always will have only one element pointed to by header because you always overwrite temp..
EDIT: the correct code can look the following way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
void insert_item( int val )
{
l if ( head == NULL )
{
head = new litem;
head->data = val;
head->next=NULL;
return ;
}
item *temp = head;
while ( temp->next != NULL )
{
temp=temp->next;
}
temp->next = new litem;
temp->next->data = val;
temp->next->next = NULL;
}
Though it would be better to define a constructor in class Item. For example
1 2 3 4 5 6
struct litem
{
litem( int val ) : data( val ), next( 0 ) {}
int data;
litem* next;
} *head=NULL
Last edited on Jan 22, 2013 at 1:37am UTC
Jan 22, 2013 at 1:34am UTC
ok, so what do i change to get it to work? do i reassign temp to head later on in the code...thanks for replying
Jan 22, 2013 at 1:36am UTC
It appears to me that in line 20 below, you are allocating a new struct and assigning it to temp. Unfortunately, temp is not linked to head.
One way to fix this is to allocate the new struct to temp->next.
And in the future, please use the <> brackets under 'Format:' and enter your code in-between the
delimiters for easier reading. ;-)
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
void insert_item(int val)
{
litem *temp,*tmp;
if (head==NULL)
{
head = new litem;
head->data=val;
head->next=NULL;
}
temp=head;
if (temp!=NULL)
{
while (temp->next!=NULL)
{
temp=temp->next;
}
temp=new litem;
temp->data=val;
temp->next=NULL;
cout<<temp->data;
}
}
Last edited on Jan 22, 2013 at 1:38am UTC
Jan 22, 2013 at 1:41am UTC
Vlad actually gave you the fix. In line 18 he is assigning the new item to temp->next.
Jan 22, 2013 at 1:52am UTC
Thanks for the help guys, I really appreciate it..