void addToEnd()
{
newnode = (struct node*)malloc(sizeof(struct node));
printf ("Enter the customer name :");
scanf ("%s", newnode->cName);
printf ("\nEnter customer number :");
scanf ("%d", &newnode->cNumber);
printf ("\nEnter transaction description :");
scanf ("%s", newnode->tDescrip);
newnode->next = NULL;
if(list==NULL)
list = newnode;
else if (list != NULL && newnode < list)
{
newnode->next = list;
list = newnode;
}
else
{
temp = list;
while (temp != NULL)
{
if (newnode > temp)
{
prev = temp;
temp = temp->next;
}
}
newnode->next = prev->next;
prev->next = newnode;
}
}
我试过这段代码,但是tis代码只是添加开始但不是结束,我想如何将节点添加到最后?
答案 0 :(得分:2)
让我们看看,以下是否不容易理解。
只需更改指针:
struct node** tail = &list;
while (*tail != NULL) {
tail = &((*tail)->next);
}
*tail = newnode;
答案 1 :(得分:0)
请研究以下附加功能,以便将节点添加到链接列表
append( int num )
{
struct node *temp,*r;
/* CREATING A NODE AND ASSIGNING A VALUE TO IT */
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
r=(struct node *)p;
if (p == NULL) /* IF LIST IS EMPTY CREATE FIRST NODE */
{
p=temp;
p->next =NULL;
}
else
{ /* GO TO LAST AND ADD*/
while( r->next != NULL)
r=r->next;
r->next =temp;
r=temp;
r->next=NULL;
}
}/* ADD A NEW NODE AT BEGINNING */