使用以下功能实现链接列表程序

时间:2015-10-11 22:04:04

标签: c++ pointers

我正在尝试使用添加,搜索,删除和打印功能来实现链接列表。但我不断收到错误,而且#34;搜索,打印和文件未在此范围内声明"和ISO C ++禁止指针和整数[-fpermissive]错误之间的比较。任何帮助将不胜感激。

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

struct node
{
    public:


    char name;
    node *next;

};

bool isEmpty(node *head);
char welcomeScreen();
void addInitialElement(node *head, node*last, char name);
void add (node *&head, node *&last, char name);
void serach (node *current);
void remove (node *&head, node *&last);
void printFile(node *current);


bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;

}
char welcomeScreen()
{
    char options; 

    cout <<"Address Book \n";
    cout <<"Available Commands \n";
    cout <<" add <name>. \n";
    cout <<" search <name>. \n";
    cout <<" remove <name>. \n";
    cout <<" print \n";
    cout <<" file <filename>. \n";
    cout <<" quit \n";

    cin >> options;
    return options;

}
void addInitialElement(node *head, node*last, char name)
{
    node *temp = new node;
    temp->name = name;
    temp->next = NULL;
    head = temp;
    last = temp;
}
void add (node *&head, node *&last, char name)
{
    if(isEmpty(head))
        addInitialElement(head, last,name);
    else
    {
        node *temp = new node;
        temp->name = name;
        temp->next = NULL;
        last->next = temp;
        last = temp;

    }
}
void serach (node *current)
{
    node *temp = new node;


    if (current->name == temp)  
    {
        cout << "The List Contains the Following: \n";

        while(current != NULL)
        {
            cout<< current->name <<endl;
            current = current->next;
        }
    }
} 
void remove (node *&head, node *&last)
{
    if(isEmpty(head))
        cout << "ERROR: Not found for Removal. \n";  

    else if(head == last)
    {
        delete head;
        head == NULL;
        last == NULL;
    }
    else
    {
        node *temp = head;
        head = head->next;
        delete temp;
    }
}
void printFile(node *current)
{
    if(isEmpty(current))
    cout << "NO NAME IN LIST. \n";

    else
    {
        cout << "The List Contains the Following: \n";

        while(current != NULL)
        {
            cout<< current->name <<endl;
            current = current->next;
        }
    }


}

int main()
{
    node *head = NULL;
    node *last = NULL;
    char options;
    char name;

    do{
        options = welcomeScreen();

        switch(options)
        {
        case '1': cout <<"please add a name: ";
                  cin >> name;
                  add(head, last, name);
                  break; 

        case '2' : search(head, last);
                   cin >> name;
                   break;


        case '3' : remove(head, last);
                   break;

        case '4' : print(head);
                   break;

        case '5' : file(head);
                   break;

        default: cout << "system exit \n"; 
        }

    }while(options != '6');

}

1 个答案:

答案 0 :(得分:0)

  1. 你拼错了搜索。在“serach”上找到/替换。
  2. 在案例4中,您可能需要调用printFile而不是print

    案例'4':printFile(head);

  3. 在案例5中,我不知道你的意图是什么,但没有名为“文件”的功能。尝试搜索该单词,文件,你会看到。