首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

链表,弄了好几天,没办法了伸手一次…

2013-11-29 
求助链表,弄了好几天,没办法了伸手一次……我用的是vc++6.0,本来程序还在完善中,但是提前编译了宜下,所有问

求助链表,弄了好几天,没办法了伸手一次……
我用的是vc++6.0,本来程序还在完善中,但是提前编译了宜下,所有问题一一解决,最后总是在一个字符串比较的地方无限出错!~~用了各种版本,包括str1.compare(str2),或者strcmp(char *,char*),或者直接string类的‘==’重载,各种蛋疼到碎,我怀疑是不是编译器有问题,下面我把程序发一下,请高手大哥大姐教我!~~~~感激不尽!!~~~~~
//头文件--item.h
#ifndef _ITEM_H
#define _ITEM_H
#include<iostream>
#include<string.h>
using namespace std;
struct item
{
   int credit;
   string name;
   item *next;   //10
};

class student
{
private:
   item *head;
   item *last;
   int count;
public:
   student();   //20
   void creat_s(int n,const string &str);
   void delete_s(int n);
   void delete_s(const string *str);
   void print_s();
   int Count_s(){return count;}
   ~student();
};

student::student()
{    //30
   head=last=NULL;
   count=0;
}

void student::creat_s(int n,const string &str)
{
   if(head==NULL)
     {
        head=new item;
        last=head;   //40
        head->credit=n;
        head->name=str;
        head->next=NULL;
        count++;
        return ;
     }
   else
     {
        item *p=new item;
        p->credit=n;   //50
        p->name=str;
        p->next=NULL;
        last->next=p;
        last=p;
        count++;
        return ;
     }   
}        
          
void student::delete_s(int n)  //60
{      
   item *p,*q;
   if(head==NULL)
       {
        std::cout<<"This list is empty!You can't delete anything! "<<endl;
        return ;
       }
   while(head->credit==n)  //1:第一个节点需要删除的情况
       {
         if(head->next==NULL)  //a:只有一个节点的情况    //70
            {
              delete head;
              head=NULL;
              return ; 
            }
         item *it=head;    //b:两个以上节点的情况
         head=head->next;
         delete it;
       }
   for(p=head->next,q=head;p!=last;q=p,p=p->next)  //2:第一个和最后一个之间的节点需要删除的情况   //80
       {
         if(p->credit==n)
           {
             item *it=p;
             p=p->next;
             q->next=p;
             delete it;
           }
       }
    if(last->credit==n)        //3:最后一个节点需要删除的情况   //90  
       {
         item *it=last;
         last=q;
         last->next=NULL;
         delete it;
       } 
}


void student::delete_s(string &str)  //100
{      
   item *p,*q;
   if(head==NULL)
       {


        std::cout<<"This list is empty!You can't delete anything! "<<endl;
        return ;
       }
   while((head->name).compare(str)==0)  //1:第一个节点需要删除的情况
       {
         if(head->next==NULL)  //a:只有一个节点的情况   //110
            {
              delete head;
              head=NULL;
              return ; 
            }
         item *it=head;    //b:两个以上节点的情况
         head=head->next;
         delete it;
       }
   for(p=head->next,q=head;p!=last;q=p,p=p->next)  //2:第一个和最后一个之间的节点需要删除的情况  //120
       {
           while((p->name).compare(str)==0) 
           {
             item *it=p;
             p=p->next;
             q->next=p;
             delete it;
           }
       }
    if((last->name).compare(str)==0)        //3:最后一个节点需要删除的情况     //130
       {
         item *it=last;
         last=q;
         last->next=NULL;
         delete it;
         return ;
       } 
}     
        
 //140       
void student::print_s()
{   
    if(head==NULL)
       {
          std::cout<<"This is a empty list!You can't print it! "<<endl; 
          return ;
       }
    std::cout<<"\tPoint\tName"<<endl;   
    for(item *p=head;p!=NULL;p=p->next)
         std::cout<<'\t'<<p->credit<<'\t'<<p->name<<endl;
    return ;
}     
       
student::~student()
{    
    while(head!=NULL)
       {
         item *it=head;
         head=head->next;
         delete it;
       }
}          
         
#endif

//主文件
#include"item.h"
#include<iostream>
#include<cstring>

using namespace std;
static student stu;
             
void add_message()
{             
    item it;
    cout<<"You choose '1',let's add message to the system.\n"
        <<"Please enter a name: "<<endl;
    cin>>it.name;
    cout<<"Please enter his(or her) points: "<<endl;        
    cin>>it.credit;
    cout<<"You entered this group of message: \n"
        <<'\t'<<it.name<<'t'<<it.credit<<'\n'
        <<"Do you want to add it? Y/N"<<endl;
    char c;
    int count_c=0;
    while(!((c=='n'||c=='N')||(c=='y'||c=='Y')))
        {
           cout<<"Mistake!Please enter again!"<<endl;


           cin>>c;
           c++;
           if(c==10)
           {
              cout<<"You lost ten chance!The message is lost!"<<endl;
              cout<<"Adding is over!"<<endl;
              return ;
           }
        }
    if(c=='y'||c=='Y')
        stu.creat_s(it.credit,it.name);
    else if(c=='n'||c=='N')
        cout<<"OK,that massage is shrow!"<<endl;
    else
        cout<<"OH!Mistake is happened!"<<endl;
}

void delete_message()

    cout<<"You choose '2',let's delete message from the system."<<endl;
    cout<<"Now,what do you want to enter?name or point? N/P"<<endl;
    char c;
    cin>>c;
    while(!((c=='n'||c=='N')||(c=='p'||c=='P')))
        {
           cout<<"Mistake!Please enter again!"<<endl;
           cin>>c;
        }
    if(c=='n'||c=='N')
      {
         string str1;
         cout<<"Please enter the name you want to delete: "<<endl;
         cin>>str1;
         cout<<"You want to delete "<<str1<<"\'s message."<<endl;
         stu.delete_s(str1);
         cout<<str1<<"\'s message has been deleted!"<<endl;
         return ;
      }
    else if(c=='p'||c=='P')
      {
         int un1;
         cout<<"Please enter the point you want to delete: "<<endl;
         cin>>un1;
         cout<<"You want to delete the message of "<<un1<<" point."<<endl;
         stu.delete_s(un1);
         cout<<un1<<" point has been deleted!"<<endl;
         return ;
       }
     else
         cout<<"Mistake! deleting is over!"<<endl;
     return;
}

void print_student()
{
cout<<"You choose 3,let's print all the message!"<<endl;
}




void choose()
{      
    cout<<"Please enter a figure to contiune: \n"
        <<"1:to add message(names and points);\n"
        <<"2:to delete message(input name or point);\n"
        <<"3:to print all the message;\n"
        <<"4:to exit!"<<endl;
}

int main()
{
    cout<<"Welcome to use this system!\n"
       <<"You can set up a list for points and names.\n"
       <<"Now!Let's begin!"<<endl;
    choose();
    int i;
add_message();
return 0;
}
求助 字符串比较


[解决办法]
你有引入string头文件吗?你是直接复制我的文件还是自己改的?逻辑上可能有问题,但编译上应该没问题的。
[解决办法]
string是对象,不是一段内存,不适合放在struct中。

热点排行