All,
I have tried to debug my code but could not find any error but I get error. This supposed to print out node 34 and node 48. For some reasons, I got 1 weird number and 34 and 48 plus error. Thanks a lot in advance.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
struct NODE
{
int nodevalue;
NODE *next;
};
NODE *addnode(int value, NODE *position) /*our algorithm starts here*/
{
NODE *newNodeAdded;
newNodeAdded = new NODE;
if(newNodeAdded==NULL)
printf("Memory shortage"
;
else
{
newNodeAdded->nodevalue = value;
newNodeAdded->next = position->next;
position->next=newNodeAdded;
}
return newNodeAdded;
}
void printnode(NODE *ptr )
{
printf("nodeValue ->%d\n", ptr->nodevalue );
}
void printlist(NODE *ptr )
{
while( ptr != NULL ) /* continue whilst there are nodes left */
{
printnode( ptr ); /* print out the current node */
ptr = ptr->next; /* goto the next node in the list */
}
}
main()
{
NODE *list;
list = new NODE;/*list initialized*/
addnode(48,list);
addnode(34,list);
printlist(list);
delete list;
return 0;
}
GH
I have tried to debug my code but could not find any error but I get error. This supposed to print out node 34 and node 48. For some reasons, I got 1 weird number and 34 and 48 plus error. Thanks a lot in advance.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
struct NODE
{
int nodevalue;
NODE *next;
};
NODE *addnode(int value, NODE *position) /*our algorithm starts here*/
{
NODE *newNodeAdded;
newNodeAdded = new NODE;
if(newNodeAdded==NULL)
printf("Memory shortage"
else
{
newNodeAdded->nodevalue = value;
newNodeAdded->next = position->next;
position->next=newNodeAdded;
}
return newNodeAdded;
}
void printnode(NODE *ptr )
{
printf("nodeValue ->%d\n", ptr->nodevalue );
}
void printlist(NODE *ptr )
{
while( ptr != NULL ) /* continue whilst there are nodes left */
{
printnode( ptr ); /* print out the current node */
ptr = ptr->next; /* goto the next node in the list */
}
}
main()
{
NODE *list;
list = new NODE;/*list initialized*/
addnode(48,list);
addnode(34,list);
printlist(list);
delete list;
return 0;
}
GH