Hi! I am trying to create a simple program to create a linked list and delete one of the nodes. When I run my program the print out for the "old list" is 4 and the "new list" is nothing. I am not sure if my problem is in the way I print the lists or in the way I make them or both. From some examples I have found on the web I suspect the problem is in how I print the lists, but I don't know for sure. My code is below:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct myllist
{
int info;
struct myllist * link;
} myllist;
void GetNewNode (int newInfo, myllist * pNode);
void disposeOfNode(myllist *change, myllist *remove);
main(){
//setup nodes
myllist * pNode;
pNode = (myllist *) malloc(sizeof(*pNode));
GetNewNode(4, pNode);
myllist * pNode2;
pNode2 = (myllist *) malloc(sizeof(*pNode));
GetNewNode(5, pNode2);
myllist * pNode3;
pNode3 = (myllist *) malloc(sizeof(*pNode));
GetNewNode(6, pNode3);
//print values to check
printf("%d\n%d\n%d\n", pNode->info, pNode2->info, pNode3->info);
//link nodes together
pNode->link = pNode2;
//print list
printf("Old List:\n");
myllist * tmp;
tmp = pNode;
while(tmp->link != NULL){
printf("%d", tmp->info);
tmp = tmp->link;
}
//get rid of pNode2
disposeOfNode(pNode, pNode2);
//print new list
printf("\nNew List:\n");
tmp = pNode;
while(tmp->link != NULL){
printf("%d", tmp->info);
tmp = tmp->link;
}
//end
getchar();
}
void disposeOfNode(myllist *change, myllist *remove){
change->link = remove->link;
free(remove);
return;
}
void GetNewNode (int newInfo, myllist * pNode){
/*myllist * pNode;
pNode = (myllist *) malloc(sizeof(*pNode));*/
if(pNode == NULL){
printf("error: malloc failed");
}else{
pNode->info = newInfo;
pNode->link = NULL;
}
return;
}
Could someone tell me what I did wrong? I have been trying to figure this out for weeks without luck. Thanks!