lockhawk 发表于 2008-10-12 16:21

初学连表!编译没错,运行错误。

非常谢谢3楼~还有问题:可以运行。选择2,3,4,5都可以正常运行。但是选择1然后输入字符按回车就出现一个对话框:"0x00412cde指令引用的内存0x0000000。
该内存不能是writer"。  请问是什么问题啊~(选择3插入节点很正常)


#include<iostream.h>
struct node
{
        char data;
        node * next;
};
node *great();
node *search(node * head,char keyword);
void Insert(node * &head,char keyword,char newword);
void Delete(node * &head,char keyword);
void showlist(node *head);
void destroy(node *node);
int main()
{
        char a,keyword,newword;
        node *head=NULL;
        int chance;
        do
        {
                cout<<"你要执行的操作:1,创建;2,读取;3,插入;4,删除;5,退出";
                cin>>chance;
                switch(chance)
                {
                case 1:{great();break;}
                case 2:{showlist(head);break;}
                case 3:{
                        cout<<"你要插入的位置:";
                        cin>>newword;
                        Insert(head,keyword,newword);
                        break;
                           }
                case 4:{
                cout<<"你要删除的内容:";
                cin>>keyword;
                Delete(head,keyword);
                break;
                           }
                case 5:{
                        destroy(head);
                        break;
                           }
default:cout<<"没有这个选项"<<endl;
                }
                cout<<"是否要继续?(Y/N)";
                cin>>a;
        }while(a=='Y'||a=='y');
        return 0;
}
node *great()
{ node *head=NULL;
  node *pEnd=head;
  node *ps;
  char temp;
  cout<<"请输入数据。以#号结束。";
  do
  {
          cin>>temp;
          if(head==NULL)
          {
                  head->data=temp;
                  head->next=pEnd;
          }
          else
          {
                  ps=new node;
                  ps->data=temp;
                  pEnd->next=ps;
                  pEnd=ps;
          }
  }while(temp!='#');
  return head;
}
node *search(node *head,char keyword)
{
        node *pRead=head;
        while(pRead!=NULL)
        {
                if(pRead->data==keyword)
                        return pRead;
                pRead=pRead->next;
        }
        return NULL;
}
void showlist(node *head)
{
        node *pRead=head;
        cout<<"连表内的数据是:";
        while(pRead!=NULL)
        {
                cout<<pRead->data;
                pRead=pRead->next;
        }
        cout<<endl;
}
void Insert(node * &head,char keyword,char newword)
{
node *newnode=new node;
newnode->data=newword;
node *pGuard=search(head,keyword);
if(head==NULL||pGuard==NULL)
{
        newnode->next=head;
        head=newnode;
        return;
}
newnode->next=pGuard->next;
pGuard->next=newnode;
}
void Delete(node * &head,char keyword)
{
        node *pGuard=head;
        node *p;
        if(head!=NULL)
        {
                if(head->data=keyword)
                {
                        p=head;
                        head=head->next;
                        delete p;
                        cout<<"已经删除:"<<keyword<<endl;
                        return;
                }
                else
                {
                        while(pGuard->next!=NULL)
                        {
                        if(pGuard->next->data=keyword)
                        {
                                p=pGuard->next;
                                pGuard->next=p->next;
                                delete p;
                                cout<<"已经删除:"<<keyword<<endl;
                                return;
                        }
                        pGuard=pGuard->next;
                        }
                }
               
        }
cout<<"你要删除的数据不存在或者连表是空"<<endl;
}
void destroy(node *head)
{
        node *p;
        while(head!=NULL)
        {
                p=head;
                head=head->next;
                delete p;
        }
        cout<<"连表已删除"<<endl;
}

[[it] 本帖最后由 lockhawk 于 2008-10-13 21:09 编辑 [/it]]

lockhawk 发表于 2008-10-12 16:22

错误信息:
--------------------Configuration: 连表 - Win32 Debug--------------------
Linking...
连表.obj : error LNK2001: unresolved external symbol "struct node * __cdecl search(struct node *,char)" (?search@@YAPAUnode@@PAU1@D@Z)
Debug/连表.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.

连表.exe - 1 error(s), 0 warning(s)

wudaojun 发表于 2008-10-13 09:09

回复 1# lockhawk 的帖子

#include<iostream.h>
struct node
{
    char data;
    node * next;
};
node *great();
node *search(node * head,char keyword);
void Insert(node * &head,char keyword,char newword);
void Delete(node * &head,char keyword);
void showlist(node *head);
void destroy(node *node);
int main()
{
    char a,keyword,newword;
    node *head=NULL;
    int chance;
    do
    {
        cout<<"你要执行的操作:1,创建;2,读取;3,插入;4,删除;5,退出";
        cin>>chance;
        switch(chance)
        {
        case 1:{great();break;}
        case 2:{showlist(head);break;}
        case 3:{
            cout<<"你要插入的位置:";
            cin>>newword;
            Insert(head,keyword,newword);
            break;
               }
        case 4:{
        cout<<"你要删除的内容:";
        cin>>keyword;
        Delete(head,keyword);
        break;
               }
        case 5:{
            destroy(head);
            break;
               }
default:cout<<"没有这个选项"<<endl;
        }
        cout<<"是否要继续?(Y/N)";
        cin>>a;
    }while(a=='Y'||a=='y');
    return 0;
}
node *great()
{ node *head=NULL;
  node *pEnd=head;
  node *ps;
  char temp;
  cout<<"请输入数据。以#号结束。";
  do
  {
      cin>>temp;
      if(head==NULL)
      {
          head->data=temp;
          head->next=pEnd;
      }
      else
      {
          ps=new node;
          ps->data=temp;
          pEnd->next=ps;
          pEnd=ps;
      }
  }while(temp!='#');
  return head;
}
node *search(node *head,char keyword)//拼写错误了
{
    node *pRead=head;
    while(pRead!=NULL)
    {
        if(pRead->data==keyword)
            return pRead;
        pRead=pRead->next;
    }
    return NULL;
}
void showlist(node *head)
{
    node *pRead=head;
    cout<<"连表内的数据是:";
    while(pRead!=NULL)
    {
        cout<<pRead->data;
        pRead=pRead->next;
    }
    cout<<endl;
}
void Insert(node * &head,char keyword,char newword)
{
node *newnode=new node;
newnode->data=newword;
node *pGuard=search(head,keyword);
if(head==NULL||pGuard==NULL)
{
    newnode->next=head;
    head=newnode;
    return;
}
newnode->next=pGuard->next;
pGuard->next=newnode;
}
void Delete(node * &head,char keyword)
{
    node *pGuard=head;
    node *p;
    if(head!=NULL)
    {
        if(head->data=keyword)
        {
            p=head;
            head=head->next;
            delete p;
            cout<<"已经删除:"<<keyword<<endl;
            return;
        }
        else
        {
            while(pGuard->next!=NULL)
            {
            if(pGuard->next->data=keyword)
            {
                p=pGuard->next;
                pGuard->next=p->next;
                delete p;
                cout<<"已经删除:"<<keyword<<endl;
                return;
            }
            pGuard=pGuard->next;
            }
        }
        
    }
cout<<"你要删除的数据不存在或者连表是空"<<endl;
}
void destroy(node *head)
{
    node *p;
    while(head!=NULL)
    {
        p=head;
        head=head->next;
        delete p;
    }
    cout<<"连表已删除"<<endl;
}

页: [1]

编程论坛