数据结构——链接表

//链接表
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define OVERFLOW 0
#define ERROR 0
using namespace std;
typedef struct user
{
    char name[15];
    char numb[20];
}user;

typedef struct Lnode
{
    user data;
    struct Lnode *next;
}Lnode,*Linklist;

//************************函数定义****************************//
int init(Linklist &L);
int creat(Linklist &L);
int insert(Linklist &L,int pos,user e);
int dele(Linklist &L,int pos,user &e);
int show(Linklist L);

int main()
{
    cout<<"**欢迎使用电话本系统**"<<endl;
    Linklist L;
    user e;
    int pos;
    while(1)
    {
        cout<<"*******************菜单栏************************"<<endl;
        cout<<"             按键1:初始电话本      \n";
        cout<<"             按键2:创建电话本      \n";
        cout<<"             按键3:添加联系人      \n";
        cout<<"             按键4:删除联系人      \n";
        cout<<"             按键5:显示电话本      \n";
        cout<<"             按键0:退出系统        \n";
        cout<<"*************************************************"<<endl;
        int n;
        char c;
        cout<<"选择菜单:";
        cin>>n;
        if(n==0)//***************************************************exit
        {
            cout<<"谢谢您的使用!";
            break;
        }
        else if(n==1)//**********************************************inti
        {
            init(L);
        }
        else if(n==2)//**********************************************creat
        {
            cout<<"******************************您将创建电话本!"<<endl;
            creat(L);
            cout<<"创建成功!"<<endl;
        }
        else if(n==3)//**********************************************insert
        {
            cout<<"请输入您要添加的位置:";
            cin>>pos;
            cout<<"\n";
            cout<<"输入联系人的名字:";
            cin>>e.name;
            cout<<"\n";
            cout<<"输入联系人的号码:";
            cin>>e.numb;
            insert(L,pos,e);
        }
        else if(n==4)//**********************************************dele
        {
            cout<<"请输入您要删除的位置:";
            cin>>pos;
            dele(L,pos,e);
        }
        else if(n==5)//**********************************************show
        {
            show(L);
            cout<<endl;
        }
        else if(n!=0||n!=1||n!=2||n!=3||n!=4||n!=5)//****************error
        {
            cout<<"还没有该菜单,请您重新选择!"<<endl;
        }
    }//**************************************************************end while
    return 0;
}
//**************************************函数声明*************************************
int init(Linklist &L)//******************************************************************************init
{
    L=(Linklist)malloc(sizeof(Lnode));
    if(!L)exit(OVERFLOW);
    L->next=NULL;
    cout<<"初始化成功!\n";
    return 1;
}

/*int creat(Linklist &L)//*****************************************************************************creat
{
    int n,coun=0;
    Linklist p;
    cout<<"请输入您要创建的联系人数目:";
    cin>>n;
    L=(Linklist)malloc(sizeof(Lnode));
    L->next=NULL;
    for(int i=n;i>0;--i)
    {
        ++coun;
        p=(Linklist)malloc(sizeof(Lnode));
        cout<<"第"<<coun<<"个联系人"<<endl;
        cout<<"姓名:";
        cin>>p->data.name;
        cout<<"\n";
        cout<<"号码:";
        cin>>p->data.numb;
        cout<<endl;
        p->next=L->next;
        L->next=p;
    }
    return 1;
}*/
int creat(Linklist &L)
{
    int n,coun=0;
    Linklist p,r;
    cout<<"请输入您要创建的联系人数目:";
    cin>>n;
    L=(Linklist)malloc(sizeof(Lnode));
    r=L;
    L->next=NULL;
    for(int i=0;i<n;i++)
    {
        ++coun;
        p=(Linklist)malloc(sizeof(Lnode));
        cout<<"第"<<coun<<"个联系人"<<endl;
        cout<<"姓名:";
        cin>>p->data.name;
        cout<<"\n";
        cout<<"号码:";
        cin>>p->data.numb;
        cout<<endl;
        r->next=p;
        r=p;
    }
    r->next=NULL;
    return 1;
}
int insert(Linklist &L,int pos,user e)//************************************************************insert
{
    Linklist p,s,q;
    p=L;
    q=L->next;
    while(q)
    {
        if(strcmp(q->data.numb,e.numb)==0)
        {
            cout<<"此号码已经存在!信息如下:\n";
            cout<<"姓名:\n";
            cout<<q->data.name<<"\n";
            cout<<"号码:\n";
            cout<<q->data.numb<<"\n";
            return 0;
        }
        q=q->next;
    }
    int j=0;
    while(p&&j<pos-1){p=p->next;++j;}
    if(!p||j>pos-1)
    {
        cout<<"位置不合法!请您重新输入";
        return ERROR;
    }
    s=(Linklist)malloc(sizeof(Lnode));
    s->data=e;
    s->next=p->next;
    p->next=s;
    cout<<"添加成功!\n";
    return 1;
}
int dele(Linklist &L,int pos,user &e)//**************************************************************delete
{
    Linklist p,q;
    p=L;
    int j=0;
    while(p->next&&j<pos-1){p=p->next;++j;}
    if(!(p->next||j>pos-1))
    {
        cout<<"位置不合法,请您重新输入!\n";
        return ERROR;
    }
    q=p->next;
    p->next=q->next;
    e=q->data;
    free(q);
    cout<<"删除成功!\n";
    return 1;
}
int show(Linklist L)//********************************************************************************show
{
    Linklist p;
    p=L;
    p=p->next;
    int j=0;
    while(p)
    {
        ++j;
        cout<<"第"<<j<<"个\n";
        cout<<"姓名:"<<p->data.name<<"\n";
        cout<<"号码:"<<p->data.numb<<"\n";
        p=p->next;
    }
    return 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值