linked list error

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
 
A

Alf P. Steinbach

* 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

There's nothing C++-specific in this question.

It seems you're programming in C, which is another language.



Non-standard header.

#include <stdio.h>

In C++, as a novice, preferably use C++ iostreams.

#include <conio.h>

Non-standard header.

#include <string.h>

In C++, preferably use the std::string type, whether novice or not.

void append(struct node **, int );
void display(struct node *);

Don't use forward declarations unless you have circular dependency.

struct node
{
int data;
struct node *link;

That 'struct' is not necessary in C++ and should be avoided.

};
void main()

'main' must have return type 'int' in both C++ and C.

{
struct node *p;
p = NULL;
//printf("the number of elements in the linked list are: = %d",
count(p));

In C version before C99 that type of comment was not supported.

append(&p, 1);
append(&p, 5);
append(&p, 17);
display(p);
}
void append(struct node **q, int num)

In C++ preferentially use reference arguments.

{
struct node *temp, *r;
temp = *q;
if (*q == NULL)
{
temp = (struct node *)malloc(sizeof(struct node));

In C++ use operator 'new'.

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;
}

This loop is meaningless.

Try to execute it as the computer would, line by line.

Also, compare to the previous insertion code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,664
Latest member
RoseannBow

Latest Threads

Top