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 <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
 
C

CBFalconer

PRadyut said:
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

No such standard include
#include <stdio.h>
#include <string.h>

void append(struct node **, int );
void display(struct node *);
struct node
{
int data;
struct node *link;
};
void main()

illegal. main returns int. Say so.

Since you already have some fatal errors, snip code. Here is some
code that works as is. Go ahead and study it and/or modify it to
taste.

/* A simplified demo of creating a linked list */
/* EOF or a non-numeric entry ends entry phase */

#include <stdio.h>
#include <stdlib.h>

typedef int datatype;

struct node {
struct node *next;
datatype datum;
};

/* ----------------- */

/* add value to head of list */
struct node *addtolist(struct node *root, datatype value)
{
struct node *newnode;

if ((newnode = malloc(sizeof *newnode))) {
newnode->next = root;
newnode->datum = value;
}
return newnode;
} /* addtolist */

/* ----------------- */

void showlist(struct node *root)
{
while (root) {
printf("%d\n", root->datum);
root = root->next;
}
} /* showlist */

/* ----------------- */

/* believed necessary and sufficient for NULL terminations */
/* Reverse a singly linked list. Reentrant (pure) code */
struct node *revlist(struct node *root)
{
struct node *curr, *nxt;

if (root) { /* non-empty list */
curr = root->next;
root->next = NULL; /* terminate new list */
while (curr) {
nxt = curr->next; /* save for walk */
curr->next = root; /* relink */
root = curr; /* save for next relink */
curr = nxt; /* walk onward */
}
}
/* else empty list is its own reverse; */
return root;
} /* revlist */

/* ----------------- */

int main(void)
{
struct node *root, *tmp;
datatype num;

root = NULL;
while (1 == scanf("%d", &num)) {
if ((tmp = addtolist(root, num))) root = tmp;
else break;
}
showlist(root);
root = revlist(root);
showlist(root);
return 0;
} /* main linklist.c */
 
A

Al Bowers

PRadyut said:
> 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 append function is badly flawed. You need to rethink the
logic and make it simple. Your goal is to append a new node
at the tail of the list. As you traverse to the tail, for one,
you do not want to malloc memory each time in the loop. See
one possible definition of function append below.
> The code
> -------------------------------------------------------------
> #include <alloc.h>

Make this
#include said:
> #include <stdio.h>
> #include <string.h>
>
>
> void append(struct node **, int );
> void display(struct node *);
> struct node
> {
> int data;
> struct node *link;
> };
> void main()

int main(void)
> {
> struct node *p;
> p = NULL;
> append(&p, 1);
> append(&p, 5);
> append(&p, 17);
> display(p);


Write a function to free the allocated space once you
are finished with it.

return 0;
> }
> void append(struct node **q, int num)

This function is redefined below.
> {
> 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);
printf("%d\n",q->data);

> q = q->link;
> }
> }


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

struct node
{
int data;
struct node *link;
};
int main(void)
{
struct node *p;
p = NULL;
append(&p, 1);
append(&p, 5);
append(&p, 17);
display(p);
freenodes(p);
return 0;
}

struct node *append(struct node **q, int num)
{
struct node **temp, *newnode;

if((newnode = malloc(sizeof *newnode)) != NULL)
{
newnode->data = num;
newnode->link = NULL;
for(temp = q; *temp; temp = &(*temp)->link) ;
*temp = newnode;
}
return newnode;
}

void display(struct node *q)
{
while(q!=NULL)
{
printf("%d\n", q->data);
q = q->link;
}
return;
}

void freenodes(struct node *q)
{
struct node *temp;

for( ; (q) ; q = temp)
{
temp = q->link;
free(q);
}
return;
}
 
P

PRadyut

just a little change, i would be happy
thanks all for your help

in the append function there should be

while(temp->link!=NULL)
temp = temp->link;
r = (struct node *)malloc(sizeof(struct node));
r->data =num;
r->link = NULL;
temp->link = r;
 
A

Al Bowers

PRadyut said:
just a little change, i would be happy
thanks all for your help

in the append function there should be

while(temp->link!=NULL)
temp = temp->link;
r = (struct node *)malloc(sizeof(struct node));
r->data =num;
r->link = NULL;
temp->link = r;


I assume this represents a change in the 'else' branch.
You must take into account that the function malloc may fail
and return a value of NULL which indicates space was not
allocated. You need to check for this before you assign.
If space is allocated, assign and place the node in the list.
If not, you will need to exit gracefully from the function
giving some indication that a new new node was not added.
this is done by checking the return value of function append.

struct node *append(struct node **q, int num)
{
struct node *temp, *r;
temp = *q;

if((r = malloc(sizeof *r)) != NULL)
{
r->data = num;
r->link = NULL;
if (*q == NULL) *q = r;
else
{
while(temp->link!=NULL)
temp = temp->link;
temp->link = r;
}
}
return r;
}
 

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

Similar Threads

Infinite loop problem 1
runtime error 4
linked list error 1
Queue in C 25
please check 10
merging list problem 15
A car garage simulation using de-queue (link list implementation) 0
double linked list delete problem 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top