By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

linked list error

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
PRadyut wrote:

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>
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 */

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #2

P: n/a


PRadyut wrote:
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 <stdlib.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()
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;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3

P: n/a
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;

Nov 14 '05 #4

P: n/a


PRadyut wrote:
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;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.