| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 483 人关注过本帖
标题:c++链表
取消只看楼主 加入收藏
许锋
Rank: 2
等 级:论坛游民
帖 子:5
专家分:12
注 册:2012-3-13
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:0 
c++链表
这是我亲手写的 和具有代表性的 希望大家看一下对你很有帮助的 希望大家给予评论
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
const int LEN=20;
struct student
{
    char ID[LEN];
    char *name;
    int nHeight;
    struct student*next;
};
typedef student stulst;
//显示函数
void Show(stulst*lst)
{
    student *p=lst->next;
    cout<<"********************************************"<<endl;
    cout.setf(ios::left);
    cout<<"      ";
    cout.width(16);
    cout<<"学号";
    cout.width(10);
    cout<<"姓名";
    cout.width(5);
    cout<<"身高"<<endl;
    while(p)
    {
        cout<<"      ";
        cout.width (16);
        cout<<p->ID;
        cout.width (10);
        cout<<p->name;
        cout.width (5);
        cout<<p->nHeight<<endl;
        p=p->next;
    }
    cout<<"*******************************************"<<endl;
}
//从文件中读取
void ReadFromFile(stulst*lst)
{
   
    stulst*p=lst;
    FILE*fp;
    fp=fopen("xufeng.txt","r");
    if(fp==NULL)
        fp=fopen("xufeng.txt","w");
    while(!feof(fp))
    { student *stu=new student;
        stu->name=new char[20];
    stu->next=NULL;
    int n=fscanf(fp,"%16s %10s %5d",stu->ID,stu->name,&stu->nHeight);
    if(n!=3)
    {
        delete [] stu->name;
        delete stu;
        break;
    }
   
    p->next=stu;
    p=p->next;
    }
    fclose(fp);
}

//写入文件
void WriteToFile(stulst*lst)
{
    stulst *p=lst->next;
    FILE *fp;
    fp=fopen("xufeng.txt","w");
    while(p)
    {
        fprintf(fp,"%16s %10s %5d\n",p->ID,p->name,p->nHeight);
        p=p->next;
    }
    fclose(fp);
}

//出入到头结点函数
void InsertToFirst(stulst*lst,char *id,char*name,int nHeight)
{
    if(!lst||!id)
        return;
    student *p=new student;
    strcpy(p->ID,id);
    p->nHeight=nHeight;
    if(name)
    {
        p->name=new char[strlen(name)+1];
        strcpy(p->name,name);
    }
    else p->name=NULL;
    p->next=lst->next;
    lst->next=p;
}
//插入到尾部
void InsertToLast(stulst*lst,char*id,char*name,int nHeight)
{if(!lst||!id)
return;
student *q=lst;
student *p=new student;
strcpy(p->ID,id);
p->nHeight=nHeight;
if(name)
{
    p->name=new char[strlen(name)+1];
    strcpy(p->name,name);
}
else p->name=NULL;
while(q->next)
q=q->next;
p->next=NULL;
q->next=p;
}
//修改函数
void ModifyByid(stulst*lst,char*id,char*Mid,char*name,int nHeight)
{
    lst=lst->next;
    while(lst)
    {
        if(strcpy(lst->ID,Mid)==0)
        {
            strcpy(lst->ID,id);
            lst->nHeight=nHeight;
            if(name)
            {
                lst->name=new char[strlen(name)+1];
                strcpy(lst->name,name);
                break;
        }}
        if(!lst)
            cout<<"输入错误没有要查找的信息!";
    }
}
//按升高降序排列
void SortByIDMax_Min(stulst *lst)
{
    student *p=new student;
    p->name=new char[20];
    stulst*p1,*p2;
    int n=0;
    p1=lst->next;
    p2=p1->next;
    while(p1)
    {
        while(p2)
        {
            if(p1->nHeight<p2->nHeight)
            {    strcpy(p->ID,p1->ID);strcpy(p->name,p1->name);p->nHeight=p1->nHeight;
            strcpy(p1->ID,p2->ID);strcpy(p1->name,p2->name);p1->nHeight=p2->nHeight;
            strcpy(p2->ID,p->ID);strcpy(p2->name,p->name);p2->nHeight=p->nHeight;
            n=1;
            }
            p2=p2->next;
        }
        if(n==0)
            break;
        p1=p1->next;
        p2=p2->next;
    }
}

//删除节点
void DeleteByID(stulst*lst,char*id)
{
    student *p1,*p2;
    p1=lst;
    while(p1->next)
    {
        if(strcpy(p1->next->ID,id)==0)
        {
            p2=p1->next;
            p1->next=p2->next;
            if(p2->name!=NULL)
                delete[] p2->name;
            delete p2;
            break;
        }
        p1=p1->next;
    }
}
//菜单
void menu()
{
    cout<<"*********************************************************"<<endl;
    cout<<"                  1 新建数据                             "<<endl;
    cout<<"                  2 删除数据                             "<<endl;
    cout<<"                  3 修改数据                             "<<endl;
    cout<<"                  4 排序                                 "<<endl;
    cout<<"                  5 信息查阅                             "<<endl;
    cout<<"                  0  退出                                "<<endl;
    cout<<"*********************************************************"<<endl;
}
void main()
{
    student stu;
    stulst lst;
    char sid[10];
    stu.next=lst.next=NULL;
    stu.name=new char[20];
    int choose,n;
    ReadFromFile(&lst);
        system("cls");
        menu();
    cout<<"输入你要实现的数据0—5"<<endl;
    cin>>choose;
    switch(choose)
   
    {
    case 0:
        system("cls");
        cout<<"\n\n\n\t\t\t\t退出程序再见!!!!!"<<endl;
        break;
   
    case 1:
            system("cls");
            cout<<"输入学号:";
            cin>>stu.ID;
            cout<<endl;
            cout<<"输入姓名:";
            cin>>stu.name;
            cout<<endl;
            cout<<"输入身高:";
            cin>>stu.nHeight;
            cout<<endl;
            system("cls");
            cout<<"                1.插入到头结点                  "<<endl;
            cout<<"                2.插入到尾结点                  "<<endl;
            cin>>n;
            switch(n)
            {
            case 1:
            InsertToFirst(&lst,stu.ID,stu.name,stu.nHeight);break;
            case 2:
                    InsertToLast(&lst,stu.ID,stu.name,stu.nHeight);break;
            }
            system("cls");
            WriteToFile(&lst);
            cout<<"文件保存成功!"<<endl;
            cout<<"按仍任意键继续!"<<endl;
            getch();
        break;
    case 2:
            system("cls");
            cout<<"输入要删除的学号:";
            cin>>stu.ID;
            ::DeleteByID(&lst,stu.ID);
            WriteToFile(&lst);
            system("cls");
            cout<<"删除成功!"<<endl;
            cout<<"按任意键继续!"<<endl;
            getch();
        break;
    case 3:
            system("cls");
            Show(&lst);
            cout<<"输入要修改的学号:";
            cin>>sid;
            cout<<"输入修改后的学号:";
            cin>>stu.ID;
            cout<<endl;
            cout<<"输入修改后的姓名:";
            cin>>stu.name;
            cout<<endl;
            cout<<"输入修改后的身高;";
            cout<<endl;
            ModifyByid(&lst,sid,stu.ID,stu.name,stu.nHeight);
            system("cls");
            cout<<"修改成功!"<<endl;
            WriteToFile(&lst);
            cout<<"按任意键继续!"<<endl;
            getch();
            break;
        
    case 4:
            system("cls");
            SortByIDMax_Min(&lst);
            Show(&lst);
            WriteToFile(&lst);
            cout<<"排序成功!"<<endl;
            cout<<"按任意键继续!"<<endl;
            getch();
            break;
        
    case 5:
        
            system("cls");
            Show(&lst);
            cout<<"按任意键继续!"<<endl;
            getch();
            break;
    }
}
搜索更多相关主题的帖子: void 代表性 next include 
2012-03-14 13:39
快速回复:c++链表
数据加载中...
 
   



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

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