创建链接列表插入功能但代码未运行

时间:2016-09-27 01:14:31

标签: c++ insert linked-list

无法识别链接列表类中创建链表插入函数时的错误,编译器在''''之前的声明中给出了这个“错误:qualified-id”但似乎所有的括号都正确放置。

    #include <iostream>

using namespace std;
    void additem();
    void deleteitem();
    void searchitem(int x);

struct student{
int data;
int * next;
};

   student * head;
   student * curr;

int main()
{
   int x;
   cout << "To add item type 1" << endl;
   cin >> x;

   switch(x)
   {
     case 1:
     additem();
   }

    return 0;
}


void additem()
{
    student * temp;

    if(head == NULL)
    {
        temp = new student;
        head = temp;
        curr = temp;
        temp->next = NULL;
        cout << "Enter data" << endl;
        cin >> temp->data << endl;
    }

    else if(head != NULL)
    {
         temp = new student;
         curr->next = temp;
         curr = temp;
         temp->next = NULL;
         cout << "Enter data" << endl;
         cin >> temp->data ;
    }
    else{
        break;
    }

}

1 个答案:

答案 0 :(得分:1)

您在main内宣布了一个班级和方法。这是不允许的(嵌套函数)。需要在main之前定义linkedlist::additem