-
Okay okay
Code:
void append(struct node *q, int num)
{
struct node *temp;
printf("\n in append");
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
temp->link = NULL;
q->link = temp;
temp = q;
printf("\n Before while");
while(temp->link != NULL)
{
printf("%d",temp->data);
temp = temp->link;
}
}
:)
-
I tried with this function. For some reason, void with append is not working. I tried using an int return type. Well this works but now I am receiving "SIGSEGV" at the statement: q->link = temp; in append.
I mean common adding an node at the end of the linked list should not be that challenging, right?
Here is the sinppet I am working on
Code:
/* Porgram appends a node at the end of the linked list*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
int main(int argc, char **argv)
{
struct node *p;
int num, j;
p= (struct node *)malloc(sizeof(struct node));
if(p->link != NULL) //To make sure you ahve reached the end of the existing list
{
p= p->link;
}
printf("\n Enter num:");
scanf("%d", &num);
j = append(p,num);
//printf("\n hello world\n");
return 0;
}
int append(struct node *q, int num)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
temp->link = NULL;
q->link = temp;
temp = q;
while(temp->link!=NULL)
{
printf("%d",temp->data);
temp = temp->link;
}
return(1);
}
-
Code:
p= (struct node *)malloc(sizeof(struct node));
if(p->link != NULL) //To make sure you ahve reached the end of the existing list
{
p= p->link;
}
What do you suppose this does?
*p is uninitialized.
-
Thanks for your help so far...
Using *p I am trying to reach the end of the linked list. So should I construct an linked list and then try to append it?
-
But you don't have a linked list yet. You're initially creating it, so how can you walk it when it's empty?
What's worse is that you don't actually initialize the list. You put it in a zombie state, so to speak. Create the list. Initialize its member properly.
-
Here is the snippet which works fine.
My question now is the function append does not work with void return type. It works fine with a dummy return variable of an int. Any clue why is this happening?
Code:
/* Porgram appends a node at the end of the linked list*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
int append(struct node *,int);
int main(int argc, char **argv)
{
struct node *p, *head, *curr, *temp;
int num,i;
head = (struct node*)malloc(sizeof(struct node));
curr = head;
for(i=1;i<=5;i++)
{
curr ->link = (struct node*)malloc(sizeof(struct node));
curr ->data= i;
temp=curr; // temp is required becoz curr will point to 6th member and 5th element pointer should be pointing to NULL
curr = curr->link;
}
temp->link =NULL;
//Display existing linked list
printf("Existing List is:");
curr = head;
while(curr->link != NULL)
{
printf("\n %d", curr->data);
curr = curr->link;
}
printf("\n %d", curr->data);
printf("\n Enter num to be appended:");
scanf("%d", &num);
i = append(head,num);
//Display list
curr = head;
printf("\n Appended list:");
while(curr->link!=NULL)
{
printf("\n %d", curr->data);
curr = curr->link;
}
printf("\n %d", curr->data);
//printf("\n hello world\n");
return 0;
}
int append(struct node *head, int num)
{
struct node *temp,*q ;
temp = head;
do{
temp = temp->link;
}while(temp->link!=NULL);
//printf("\n temp->data = %d \n temp->link =%d",temp->data, temp->link);
q = (struct node *)malloc(sizeof(struct node));
q->data = num;
q->link = NULL;
temp->link = q;
return(1);
}
-
All functions need a return type specified, or need to be void. Your append function won't work if you pass it an empty list:
Code:
struct node *n = NULL;
append( n, 4 );
n will still not point to anything once the function ends, because the only way to update a variable's value inside a function is to pass a pointer to that variable, which you aren't doing, if you're trying to modify the value of n.
Quzah.
-
append(head, num). So append has head of an initialized linked list and num which would scaned and passed to append. So all arguments are initialized. And I have tried using void and list is not appended then.
-
The return type isn't what's making your function break.
Code:
void append( struct node *n, int v )
{
while( node && node->next )
node = node->next;
if( node )
{
struct node *t = malloc( sizeof *t );
if( t )
{
t->value = v;
t->next = NULL;
}
node->next = t;
}
}
You have to actually have a node to pass for this to work, which is basically what you're trying to do anyway.
Quzah.