P
Pradyut
hi,
i have written a function in adding a node to the linked list
but this only adds "1" to the list as i get in the output
I'm using borland c++ compiler
The code
-------------------------------------------------------------
#include <alloc.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
void append(struct node **, int );
void display(struct node *);
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *p;
p = NULL;
//printf("the number of elements in the linked list are: = %d",
count(p));
append(&p, 1);
append(&p, 5);
append(&p, 17);
display(p);
}
void append(struct node **q, int num)
{
struct node *temp, *r;
temp = *q;
if (*q == NULL)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
temp->link = NULL;
*q = temp;
}
else
{
//temp = *q;
while(temp->link!=NULL)
{
r = (struct node *)malloc(sizeof(struct node));
r->data =num;
r->link = NULL;
temp->link = r;
}
}
}
void display(struct node *q)
{
while(q!=NULL)
{
printf("%d", q->data);
q = q->link;
}
}
-------------------------------------------------------
Thanks
Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India
i have written a function in adding a node to the linked list
but this only adds "1" to the list as i get in the output
I'm using borland c++ compiler
The code
-------------------------------------------------------------
#include <alloc.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
void append(struct node **, int );
void display(struct node *);
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *p;
p = NULL;
//printf("the number of elements in the linked list are: = %d",
count(p));
append(&p, 1);
append(&p, 5);
append(&p, 17);
display(p);
}
void append(struct node **q, int num)
{
struct node *temp, *r;
temp = *q;
if (*q == NULL)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
temp->link = NULL;
*q = temp;
}
else
{
//temp = *q;
while(temp->link!=NULL)
{
r = (struct node *)malloc(sizeof(struct node));
r->data =num;
r->link = NULL;
temp->link = r;
}
}
}
void display(struct node *q)
{
while(q!=NULL)
{
printf("%d", q->data);
q = q->link;
}
}
-------------------------------------------------------
Thanks
Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India