【发布时间】:2014-01-21 04:35:00
【问题描述】:
我正在创建一个链接列表。我想调用函数
addToTail(head*, data)
从函数
addToTail(head*){int n = length(head*); addtoTail(head*, n)}
其中包含一个计算列表长度的函数。
不幸的是,这给了我一个错误
\linkedList.cpp:97:16: error: too few arguments to function 'void addAtTail(node*, int)'
这对我来说似乎很有意义,我做错了什么吗?
这里是代码。任何改进它的 cmets 将不胜感激。
#include<iostream>
using namespace std;
struct node
{
int data;
struct node* next;
};
void changeToNull(struct node** head)
{
*head = NULL;
}
int listLength(struct node* head)
{
struct node* curr = head;
int i = 0;
while(curr!=NULL)
{
i++;
curr = curr->next;
}
return i;
}
void addAtTail(struct node* head)
{
int length = listLength(head);
addAtTail(head, length);
}
void addAtTail(struct node* head, int n)
{
struct node* newNode = new node;
struct node* curr = new node;
newNode->next = NULL;
newNode->data = n;
curr= head;
while(curr->next!=NULL)
{
curr = curr->next;
}
curr->next = newNode;
}
int main()
{
//changeToNull(&head);
addAtTail(head);
printList(head);
return 0;
}
【问题讨论】:
标签: c++ function linked-list overloading singly-linked-list