Hi, I have also faced such type of problems so many time during prgramming so
I hope I will also get so many solutions for such type of problem. So thanks
for starting this thread. Thanks!
environmental sustainability software
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You need to keep a pointer to the first node and start the display with that
node.
You are starting the display with the updated root variable which contains the
last node.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
voidadd(structnode*root,inty){//whydoyoucreate"conductor"pointerrootisn't ok?? struct node *conductor; root = malloc(sizeof(struct node)); conductor=root; conductor->next=0; conductor->x=y;}void display() // no arguments!!! { // you create "conductor" a local pointer. Where is this pointer // points?? struct node *conductor; //while(conductor->next!=NULL) stops while(conductor->next!=NULL);//here because of ; { printf("%d\n",conductor->x); conductor=conductor->next; } // conductor->next is NULL because you don'tchangeitsvalue//anywhere....}intmain()//itsbetterintmain(void){//youcreateanewnodestructnoderoot;//youpass&rootasargument&roottoaddfunction//butinsideaddyoudontusethisnode,youcreateanew//nodewithroot=malloc(sizeof(structnode));command//thisrootyoucreateinsideaddfunction,isnotlinkedwith//therootthatyoupassasargumenthere.add(&root,5);add(&root,2);add(&root,8);//youpassrootbyvaluetodisplayfunctionbut,youdefined//displayfunction//asvoiddisplay()noarguments!!!display(root);system("\npause");return0;//thiscommandsay's to Operating System that programms execution // end normaly.}// You don'tlinknodeseachotheranywhere//Youcreateanodeinmain,thenyoupassitbyvalueinadd()//insideadd()youloseanyconectionwithmain's node because you// create a new with malloc and make next to point to NULL. So your// are not linked.// function() mean that function doesn'tmakecheckofthe//passingarguments./code][b]Thisprogrammcouldbelikethisdon'tjustcopyit....trytounderstantit[/b][code]#include<stdio.h>#include<stdlib.h>structnode{intx;structnode*next;};voidadd(structnode*node_ptr,inty){while(node_ptr->next!=NULL){node_ptr=node_ptr->next;}//goestothelastnodestructnode*new_node=malloc(sizeof(structnode));new_node->next=NULL;new_node->x=y;node_ptr->next=new_node;return;}voiddisplay(structnode*node_ptr){do{printf("%d\n",node_ptr->x);node_ptr=node_ptr->next;}while(node_ptr->next!=NULL);printf("%d\n",node_ptr->x);//printthelastonereturn;}intmain(){structnode*root=malloc(sizeof(structnode));//firstnoderoot->next=NULL;root->x=5;add(root,2);add(root,8);display(root);system("\n\npause");return0;}[/code]
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
here's my code I can't seem to print the other values(5 and 2 ) my code only
prints out the number 8, what seems to be the problem with my code?
include<stdio.h></stdio.h>
include<stdlib.h></stdlib.h>
struct node
{
int x;
struct node next;
};
void add(struct node root,int y)
{
struct node *conductor;
root = malloc(sizeof(struct node));
conductor=root;
conductor->next=0;
conductor->x=y;
}
void display()
{
struct node *conductor;
while(conductor->next!=NULL);
{
printf("%d\n",conductor->x);
conductor=conductor->next;
}
}
int main()
{
struct node root;
add(&root,5);
add(&root,2);
add(&root,8);
display(root);
system("\npause");
}
Hi, I have also faced such type of problems so many time during prgramming so
I hope I will also get so many solutions for such type of problem. So thanks
for starting this thread. Thanks!
environmental sustainability software
You need to keep a pointer to the first node and start the display with that
node.
You are starting the display with the updated root variable which contains the
last node.
i wrote int main()
is better
int main(void)