| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1385 人关注过本帖
标题:怎么样才可以把文件中的数据写到一个链表中
只看楼主 加入收藏
yuxiuyang
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-10-3
收藏
 问题点数:0 回复次数:2 
怎么样才可以把文件中的数据写到一个链表中
我已做好了一个关于类的单向链表并把它写到了文件中去了 现在问题是怎么样才可以把它重新调出来,实现各种操作
请指点迷津.
搜索更多相关主题的帖子: 链表 数据 文件 
2008-10-30 21:34
yangweiwei
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-10-11
收藏
得分:0 
#include <iostream>
#include <string>
#include "wordnode.h"
#include "word.h"
using namespace std;

//初始化
word::word()
{
    link.head = NULL;
}

//查找是否有相同单词
struct words* word::findword(string strword)
{
    struct words *pw = link.head;
        
    while( pw != NULL )
    {
        if( pw->sword==strword )
        return pw;
        
        pw = pw->next;
    }
    
    return pw;
}



 //插入不同的单词
 struct linkword word::insert(string strword)
 {
     struct words *wordsnode;
    
     wordsnode = new( words );

        if( !wordsnode )
        {
           cout<<"memory is full!stop!"<<endl;
           exit(1);
        }

        wordsnode->sword = strword;
        wordsnode->count = 1;
        
        if(link.head == NULL)
        {
           wordsnode->next = link.head;
           link.head = wordsnode;
        }
        else if(wordsnode->sword < link.head->sword)
        {
            wordsnode->next = link.head;
            link.head = wordsnode;
        }
        else
        {
          struct words *p = link.head;
          struct words *q;
          q = p->next;
          while(q!=NULL)
          
              if(wordsnode->sword < q->sword)
                  break;
              else
              {
                  p = q;
                  q = q->next;
              }
              wordsnode->next = q;
              p->next = wordsnode;
          }
 

        
        
        return link;
 }
 
 //打印单词链表
 void word::outputlist()
 {
     struct words *pw = link.head;
     
     while(pw!=NULL)
    {
        cout<<pw->sword<<"  "<<cout<<pw->count<<"  ";
        pw=pw->next;
    }
    cout<<endl;
 }
2008-10-30 21:55
yuxiuyang
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-10-3
收藏
得分:0 
把逻表中的数据已写到了文件中去了,第二次使用时怎么样才可以把它从文件中重新调出来
#include<iostream.h>
#include<cstring>
#include<stdlib.h>
//#include<yuxiuyang.h>
#include<iomanip.h>
//classmate.h
//同学类

#include<iostream.h>

class classmate
{
protected:
    int n;
       struct date_of_brith{         //出生年月日
      int year;
      int month;
      int day;
    }pp;
    char *telephone;                 //电话号码
    
    char *name;                //姓名
    char *sex;                     //姓别
    char *class_num;           //班级学号

    char *addr;                //地址

    char *information;        //信息

    public:
        void setname();  //设置姓名

        char *getname();   //提取姓名
        
        void setsex();     //设置姓别

        char *getsex();                     

        void setclass_num();                  //设置班级学号  
        
         char *getclass_num();                       //提取班级学号     
        
        void setdate_of_brith();//设置出生年月日

        void showbrithday();                         //显示出生年月日

        void settelephone();                    //设置电话号码

        char *gettelephone();                         //提取电话号码
    
};


#define Null 0
void classmate::setdate_of_brith()//设置出生年月日的实现
{
  int NewY,NewM,NewD;
   cout<<"请输入出生年月日:如1988 06 10"<<endl;
    cin>>NewY>>NewM>>NewD;
    cout<<endl;
  pp.year=NewY;
  pp.month=NewM;
  pp.day=NewD;
  cin.get();
}
void classmate::showbrithday(){                                //提取出生年月日的实现

    cout<<pp.year<<"年"<<pp.month<<"月"<<pp.day<<"日";
   
}
void classmate::setname(   )               //设置姓名的实现
{
  p2: char names[20];
     cout<<"请输入姓名:"<<endl;
     cin.getline(names,20);
     if(strlen(names)>20){
        cout<<"字符超出范围"<<endl<<"请重新输入:"<<endl;
        goto p2;}
    name=new char[strlen(names)+1];
    if(name==Null)
       {    cout<<"无法分配内存空间"<<endl;     exit(0);}      
    strcpy(name,names);
    cin.get();
 }

char * classmate::getname()                         //提取姓别的实现
{   return name;      }  
            
void classmate::setsex()                  //设置姓别的实现
{   
 p3: char sexs[8];
 cout<<"请输入性别:"<<endl;
    cin.getline(sexs,8);
   if(strlen(sexs)>8){
        cout<<"字符超出范围"<<endl<<"请重新输入:"<<endl;
        goto p3;}
    sex=new char[strlen(sexs)+1];
    if(sex==Null)
    {cout<<"无法分配内存空间"<<endl;   exit(0);}
    strcpy(sex,sexs);  
    cin.get();
  }

char *classmate::getsex()                             //提取姓别的实现  
{  return sex;        }

void classmate::setclass_num()                     //设置班级学号的实现   
{
  p4: char class_nums[10];  
      cout<<"请输入班级学号:"<<endl;//班级学号的输入
    cin.getline(class_nums,10);
     if(strlen(class_nums)>10){
        cout<<"字符超出范围"<<endl<<"请重新输入:"<<endl;
        goto p4;}
    class_num=new char[strlen(class_nums)+1];
    if(class_num==Null)
       { cout<<"无法分配内存空间"<<endl;  exit(0);  }
    strcpy(class_num,class_nums);
     cin.get();

  }

char *classmate::getclass_num()                        //提取班级学号的实现      
{  return class_num;   }



void classmate::settelephone()                 //设置电话号码的实现
{  
  p1:  char telephones[15];                             //电话号码输入
     cout<<"请输入电话号码:"<<endl;
     cin.getline(telephones,15);
     if(strlen(telephones)>11){
        cout<<"字符超出范围"<<endl<<"请重新输入:"<<endl;
        goto p1;}
     telephone=new char[strlen(telephones)+1];
    if(telephone==NULL)
        cout<<"无法分配内存空间"<<endl;
    strcpy(telephone,telephones);
    cin.get();
   
}                           

char *classmate::gettelephone()                                //提取电话号码的实现
{  return telephone;}
class mate:public classmate
{  
public:
    mate *next;
    mate *head,*rear;      //表头表尾指针
        void putoutlink();  //输出整个链表
    void link();        //链表连接
    void inputdata(mate *);       //输入数据
    void midlink(mate *);//在表中插入一个结点
        void modify();//修改信息
    mate *search();//查找
    void outputdata(mate *);//输出一个结点数据
         void writein();//写入文件
         void read();//读出文件
};
void mate::link()//链表连接的实现
{  
    head=new mate;
    mate *prevptr,*ppp;
    ppp=head;
    int end=1;
    while(end!=0){
    prevptr=new mate;
    if(prevptr==NULL){cout<<"无法分配到空间";exit(0);}
    inputdata(prevptr);
     ppp->next=prevptr;
    prevptr->next =NULL;
    ppp=prevptr;
    cout<<"按0键结束";
    cout<<endl<<"end=";
    cin>>end;}
    rear=ppp;//表尾指针
    cin.get();    

}
void mate::inputdata(mate *prevptr)//向每个结点链表中输入数据
{
    //prevptr->setindividualEmpNo();
   
    prevptr->setdate_of_brith();
    prevptr->setname();
    prevptr->setclass_num();
    prevptr->setsex();
    prevptr->settelephone();
}
void mate::outputdata(mate *prevptr)
{
    int i;
      for(i=1;i<=50;i++)
    cout<<"*";
    cout<<endl;
        cout<<"*"<<"姓名: "<<setw(44)<<prevptr->getname()<<"*"<<endl;
    cout<<"*"<<"姓别: "<<setw(44)<<prevptr->getsex()<<"*"<<endl;
    cout<<"*"<<"班级学号: "<<setw(40)<<prevptr->getclass_num()<<"*"<<endl;
    cout<<"*"<<"出生年月日 :                 ";
    prevptr->showbrithday();cout<<"*"<<endl;
    for(i=1;i<=50;i++)
    cout<<"*";
    cout<<endl;
}
void mate::putoutlink()//输出整个链表的实现
{
  mate *currptr;
  currptr=head->next;
  if(currptr==NULL){cout<<"无法分配到空间";exit(0);}
  while(currptr!=NULL)
  {
      int i;
      for(i=1;i<=50;i++)
    cout<<"*";
    cout<<endl;
        cout<<"*"<<"姓名: "<<setw(44)<<currptr->getname()<<"*"<<endl;
    cout<<"*"<<"姓别: "<<setw(44)<<currptr->getsex()<<"*"<<endl;
    cout<<"*"<<"班级学号: "<<setw(40)<<currptr->getclass_num()<<"*"<<endl;
    cout<<"*"<<"出生年月日 : ";
    currptr->showbrithday();cout<<"*"<<endl;
    for(i=1;i<=50;i++)
    cout<<"*";
    currptr=currptr->next;
  }
}
void mate::midlink(mate *currptr)          //在表中插入一个结点  currptr是当前指针
{
   mate *pvreptr,*newmate;
   pvreptr=currptr->next;
     newmate=new mate;
if(newmate==NULL){cout<<"无法分配到空间";exit(0);}
   inputdata(newmate);
   currptr->next=newmate;
   newmate->next=pvreptr;

}
class mate *mate::search()
{
  mate *prevptr;
  prevptr=head->next;
  cout<<"*********************"<<endl;

  cout<<"*        查找       *"<<endl;
  cout<<"*  姓名:         1  *"<<endl;
  cout<<"*  班级学号:     2  *"<<endl;
  cout<<"*  编号:         3  *"<<endl;

  cout<<"*********************"<<endl;
   int i;
 n1:cout<<"请选择:";
  cin>>i;
  switch(i)
  {
  case 1:cout<<"请输入要查找的姓名:"<<endl;
          char names[20];
          cin>>names;
          while(prevptr!=NULL){
              if(strcmp(prevptr->name,names)==0){
                  outputdata(prevptr);
                 } break;
              prevptr=prevptr->next;} break;
          
  case 2:cout<<"请输入要查找的班级学号:"<<endl;
      char class_nums[8];
      cin>>class_nums;
      while(prevptr!=NULL){
          if(strcmp(prevptr->class_num,class_nums)==0){
                 outputdata(prevptr);
                 }  break;
          prevptr=prevptr->next;} break;
      
 ;
 default :cout<<"未找到请重新查找:";goto n1;
      
}
return prevptr;
}
void mate::modify()    //修改信息
{
  mate *prevptr;
  int i;
  cout<<"*******************"<<endl;
  cout<<"修改编号请按1:"<<endl;
  cout<<"修改姓名请按2:"<<endl;
  cout<<"修改姓别请按3:"<<endl;
  cout<<"修改班级学号请按4:"<<endl;
  cout<<"修改电话请按5:"<<endl;
  cout<<"修改地址请按6:"<<endl;
  cout<<"修改信息请按7:"<<endl;
  cout<<"*******************"<<endl;
  cout<<"请选择:";
  cin>>i;
  
  switch(i)
  {
  case 1:cout<<"查找所要修改的信息"<<endl;
         prevptr= search();
         cout<<"重新设置编号:"<<endl;
         int i;
         cin>>i;
         prevptr->individualEmpNo=i;break;
  case 2:cout<<"查找所要修改的信息"<<endl;
         prevptr= search();
         cout<<"重新设置姓名:";
         char names[20];
         cin>>names;
         strcpy(prevptr->name,names);break;
  case 3:cout<<"查找所要修改的信息"<<endl;
         prevptr= search();
         cout<<"重新设置姓别:";
         char sexs[5];;
         cin>>sexs;
         strcpy(prevptr->sex,sexs);break;
  case 4:cout<<"查找所要修改的信息"<<endl;
         prevptr= search();
         cout<<"重新设置班级学号:";
         char class_nums[8];
         cin>>class_nums;
         strcpy(prevptr->class_num,class_nums);break;
  case 5:cout<<"查找所要修改的信息"<<endl;
         prevptr= search();
         cout<<"重新设置电话:";
         char telephones[11];
         cin>>telephones;
         strcpy(prevptr->telephone,telephones);break;
 
  default : cout<<"error";
  }
}
 void mate::write()
{
mate *p;
    p=head->next;
    ofstream myfile("datam",ios::binary);
  if(!myfile)  cout<<"error";
  while(p!=NULL){
    myfile.write((char *)((mate*)p),sizeof(p));
    p=p->next;
}
void mate::read{//请教怎样写}
void main()
{
   cin.get();
          
}
谢谢
2008-10-31 12:19
快速回复:怎么样才可以把文件中的数据写到一个链表中
数据加载中...
 
   



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

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