| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 616 人关注过本帖, 1 人收藏
标题:初学连表!编译没错,运行错误。
只看楼主 加入收藏
lockhawk
Rank: 1
等 级:新手上路
帖 子:94
专家分:0
注 册:2008-9-17
收藏(1)
 问题点数:0 回复次数:2 
初学连表!编译没错,运行错误。
非常谢谢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]]
搜索更多相关主题的帖子: 编译 初学 运行 
2008-10-12 16:21
lockhawk
Rank: 1
等 级:新手上路
帖 子:94
专家分:0
注 册:2008-9-17
收藏
得分:0 
错误信息:
--------------------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)

人生最大痛苦:虽然不明白你们说什么,但还是要听!
2008-10-12 16:22
wudaojun
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-9-5
收藏
得分:0 
回复 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;
}
2008-10-13 09:09
快速回复:初学连表!编译没错,运行错误。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012234 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved