注册 登录
编程论坛 数据结构与算法

头痛的东西!链表问题。谁能帮我改一下这段代码吗?

huting0501 发布于 2011-04-21 11:31, 386 次点击
程序代码:
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
struct jiadian
{
    int num;        //编号
    int dj;        //单价
    int sl;       //数量
};
typedef struct node
{   
    jiadian jd;
    struct node *next;
}lnode,linklist;
void menuprint()     //菜单1

{
    cout<<"\t*************************商场家电库存***********************"<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t 1冰箱"<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t 2电视机"<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t 3洗衣机"<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t 4电脑"<<endl;

}
linklist* creatlinklistl()      //创建表
{
    menuprint();
    linklist *h;

    lnode *s=(lnode *)malloc(sizeof(lnode));
    h=(lnode *)malloc(sizeof(lnode));
    h->next=NULL;

    printf("请输入家电编号并且以#结束:");
    scanf(" %d",&s->jd.num);
    while(s->jd.num!='#')        //当x等于#时结束
    {
        s=(lnode *)malloc(sizeof(lnode));
        cout<<"请输入家电的单价:"<<endl;
        scanf("% d",s->jd.dj);
        cout<<"请输入家电的数量:"<<endl;
        scanf("% d",s->jd.sl);
        s->next=h->next;
        h->next=s;
        cout<<"请输入家电的编号并以#结束"<<endl;
        scanf(" %d",&s->jd.num);

    }
    return h;
}
void main()
{
    creatlinklistl();      //创建表
}
这只是我写的一部分,执行时出错,我不知道怎么改???、
7 回复
#2
寒风中的细雨2011-04-21 12:41
什么错误?
#3
huting05012011-04-22 08:43
执行出现不是我想要的结果。
#4
寒风中的细雨2011-04-22 11:07
while(s->jd.num!=0)
改成这样子  不然 #  这个符号是接受不了的
#5
寒风中的细雨2011-04-22 11:30
程序代码:
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>

struct jiadian
{
    int num;        //编号
    int dj;        //单价
    int sl;       //数量
};

typedef struct node
{  
    jiadian jd;
    struct node *next;
}lnode,linklist;

void menuprint()     //菜单1
{
    cout<<"\t*************************商场家电库存***********************"<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t 1冰箱"<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t 2电视机"<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t 3洗衣机"<<endl;
    cout<<"\t\t "<<endl;
    cout<<"\t\t 4电脑"<<endl;

}

linklist* creatlinklistl()      //创建表
{
    menuprint();
    linklist *h;

    lnode *s=(lnode *)malloc(sizeof(lnode));
    h=(lnode *)malloc(sizeof(lnode));
    h->next=NULL;

    printf("请输入家电编号并且以#结束:");
    scanf("%d",&s->jd.num);
    while(s->jd.num!=0)        //当x等于#时结束
    {
        cout<<"请输入家电的单价:"<<endl;
        scanf("%d", &s->jd.dj);
        cout<<"请输入家电的数量:"<<endl;
        scanf("%d", &s->jd.sl);
        s->next=h->next;
        h->next=s;

        s=(lnode *)malloc(sizeof(lnode));
        cout<<"请输入家电的编号并以#结束"<<endl;
        scanf("%d",&s->jd.num);

    }

    return h;
}
void main()
{
    creatlinklistl();      //创建表
}

#6
寒风中的细雨2011-04-22 11:32
只有本站会员才能查看附件,请 登录


这个能出来吧
#7
huting05012011-04-23 20:43
这是我全部的代码,函数的功能不能实现,能不能帮我改改啊????
程序代码:


#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
int choose,i;
struct jiadian
{
    int num;        //编号
    int dj;        //单价
    int sl;       //数量
};
typedef struct node
{   
    jiadian jd;
    struct node *next;
}lnode, linklist;
linklist *h;
void menuprint()     //菜单1

{
     printf("*************************商场家电库存***********************\n");
     printf("                                                             \n");
     printf("          1冰箱                             2电视机          \n");
     printf("                                                             \n");
     printf("          3洗衣机                           4电脑            \n");
     printf("                                                             \n");
}
void zhu()           //菜单2
{
    cout<<"\t*************************你要进行的操作************************"<<endl;
    printf("                                                            \n");
    printf("          1删除                                  2插入      \n");
    printf("                                                            \n");
    printf("          3创建表                                4退出      \n");
    printf("                                                            \n");
    printf(" 请输入你的选择:                                           \n");
    cin>>choose;
}

linklist* creatlinklistl()      //创建表
{

    char flag;

    lnode *s;
    h=(lnode *)malloc(sizeof(lnode));
    h->next=NULL;

    //printf("请输入家电编号并且以#结束:");
    cout<<"你是否要建立表,输入#确认建立"<<endl;
    scanf(" %c",&flag);
    while(flag=='#')        //当x等于#时继续
    {
        s=(lnode *)malloc(sizeof(lnode));
        cout<<"请输入家电编号"<<endl;
        scanf("%d",&(s->jd.num));
        cout<<"请输入家电的单价:"<<endl;
        scanf("%d",&(s->jd.dj));
        cout<<"请输入家电的数量:"<<endl;
        scanf("%d",&(s->jd.sl));
        s->next=h->next;
        h->next=s;
        cout<<"请输入家电编号并且以#结束:"<<endl;
        cin>>flag;
      
    }
    return h;
}
void insertlinklist()    //插入
{
    menuprint();
    linklist* p=h;
    linklist* s;
    int j=0,i;


    cout<<"请输入你要插入的位置i:";
    cin>>i;
    while(p&&j<i-1)
    {p=p->next;j++;}
    if(!p)
        cout<<"插入失败"<<endl;
    s=(linklist*)malloc(sizeof(lnode));
       cout<<"请输入插入家电的编号"<<endl;
    scanf("%s",&(s->jd.num));
    cout<<"请输入插入家电的单价:"<<endl;
    scanf("% d",&(s->jd.dj));
    cout<<"请输入插入家电的数量:"<<endl;
    scanf("% d",&(s->jd.sl));
    s->next=p->next;
    p->next=s;
    cout<<"插入成功"<<endl;
}
void deletelinklist()           //删除
{
    linklist *p;
    linklist *q;
    int i;
    cout<<"请输入你要删除的第i个元素:"<<endl;
    cin>>i;
    int j=0;
    p=h;
    while(p->next&&j<i-1)
    {p->next;j++;}
    if(!(p->next))
        cout<<"删除失败"<<endl;
    q=p->next; p->next=q->next;
    free(q);
    cout<<"删除成功"<<endl;
}

void main()
{


   
    menuprint();
    cout<<"请输入你的选择:";
    cin>>choose;            //对菜单1进行选择
    switch(choose)
    {
    case 1 : printf("         编号             单价            数量     \n");     //输出冰箱的记录
             printf("         001             1000          1002000     \n");
             printf("         002             500           1222223     \n");
             printf("                                       \n");
             printf("                                       \n");
             printf("                                       \n");
          zhu();         //(菜单2)对冰箱进行的操作
            switch(choose)
            {   
            case 1:
                deletelinklist();                              //删除
                break;
            case 2:
                insertlinklist();                               //插入
                break;
            case 3:  creatlinklistl();                          //创建
                break;
            case 4:                                             //退出
                exit(0);
            default:cout<<"输入错误"<<endl;
            }
      break;
      case 2:;                      //输出电视机的记录
          zhu();        //对电视机进行的操作
          switch(choose)
            {
            case 1:
                deletelinklist() ;                            //删除
                break;
            case 2:
                insertlinklist() ;                            //插入
                break;
            case 3: creatlinklistl();                         //创建
                break;
            case 4:                                           //退出
                exit(0);
            default:cout<<"输入错误"<<endl;
            }
      break;
      case 3: ;                        //输出洗衣机的记录
          zhu();        //对洗衣机进行的操作
        
          switch(choose)
            {
            case 1:
                deletelinklist();                              //删除
                break;
            case 2:   cout<<"请输入你要在第几个位子前插入记录:";
                      cin>>i;
                insertlinklist();                               //插入
                break;
            case 3: creatlinklistl() ;                          //创建
                break;
            case 4:                                             //退出
                exit(0);
            default:cout<<"输入错误"<<endl;
            }
       break;
       case 4: ;                           //输出电脑的记录
           zhu();       //对电脑进行的操作
           switch(choose)
            {
            case 1:
                deletelinklist();                                //删除
                break;
            case 2:
                insertlinklist();                                //插入
                break;
            case 3: creatlinklistl();                           //创建
                break;
            case 4:                                             //退出
                exit(0);
            default:cout<<"输入错误"<<endl;
      
            }
        default:cout<<"输入错误"<<endl;
    }
}




#8
寒风中的细雨2011-04-23 21:23
程序代码:
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
int choose,i;
struct jiadian
{
    int num;        //编号
    int dj;        //单价
    int sl;       //数量
};
typedef struct node
{  
    jiadian jd;
    struct node *next;
}lnode, linklist;
linklist *h = NULL;
void menuprint()     //菜单1

{
     printf("*************************商场家电库存***********************\n");
     printf("                                                             \n");
     printf("          1冰箱                             2电视机          \n");
     printf("                                                             \n");
     printf("          3洗衣机                           4电脑            \n");
     printf("                                                             \n");
}
void zhu()           //菜单2
{
    cout<<"\t*************************你要进行的操作************************"<<endl;
    printf("                                                            \n");
    printf("          1删除                                  2插入      \n");
    printf("                                                            \n");
    printf("          3创建表                                4退出      \n");
    printf("                                                            \n");
    printf(" 请输入你的选择:                                           \n");
    cin>>choose;
}

linklist* creatlinklistl()      //创建表
{

    char flag;

    lnode *s;
    h=(lnode *)malloc(sizeof(lnode));
    h->next=NULL;

    //printf("请输入家电编号并且以#结束:");
    cout<<"你是否要建立表,输入#确认建立"<<endl;
    scanf("%c",&flag);
    while(flag=='#')        //当x等于#时继续
    {
        s=(lnode *)malloc(sizeof(lnode));
        cout<<"请输入家电编号"<<endl;
        scanf("%d",&(s->jd.num));
        cout<<"请输入家电的单价:"<<endl;
        scanf("%d",&(s->jd.dj));
        cout<<"请输入家电的数量:"<<endl;
        scanf("%d",&(s->jd.sl));
        s->next=h->next;
        h->next=s;
        cout<<"请输入家电编号并且以#结束:"<<endl;
        cin>>flag;
      
    }
    return h;
}
void insertlinklist()    //插入
{
//    menuprint();
    linklist* p=h;
    linklist* s;
    int j=0,i;


    cout<<"请输入你要插入的位置i:";
    cin>>i;
    while(p&&j<i-1)
    {p=p->next;j++;}
    if(!p)
    {
        cout<<"插入失败"<<endl;
        return;
    }
    s=(linklist*)malloc(sizeof(lnode));
       cout<<"请输入插入家电的编号"<<endl;
    scanf("%d",&(s->jd.num));
    cout<<"请输入插入家电的单价:"<<endl;
    scanf("%d",&(s->jd.dj));
    cout<<"请输入插入家电的数量:"<<endl;
    scanf("%d",&(s->jd.sl));
    s->next=p->next;
    p->next=s;
    cout<<"插入成功"<<endl;
}
void deletelinklist()           //删除
{
    if (NULL == h)
    {
        cout << "没有数据" << endl;
        return;
    }
    linklist *p;
    linklist *q;
    unsigned int i;
    cout<<"请输入你要删除的第i个元素(从1开始计数):"<<endl;
    cin>>i;
    if (0 >= i)
    {
        cout << "操作错误" << endl;
        return;
    }
    unsigned int j=1;
    p=h;
    while(NULL!=p->next && j!=i)
    {
        p = p->next;
        j++;
    }
    if(NULL == p->next)
    {
        cout<<"删除失败"<<endl;
        return;
    }
    q=p->next;
    p->next=q->next;
    free(q);
    cout<<"删除成功"<<endl;
}

void show()
{
    linklist *p = h;

    if (NULL == h)
    {
        return;
    }
    p = p->next;
    while(NULL != p)
    {
        cout<<"家电编号:"<< p->jd.num << ' ';
        cout<<"家电的单价:"<< p->jd.dj << ' ';
        cout<<"请输入家电的数量:"<< p->jd.sl << endl;
      
        p = p->next;
    }
}
void main()
{
    while( true )
    {
        //menuprint();
        zhu();        //对电视机进行的操作
        
//cout<<"请输入你的选择:";
        
//cin>>choose;            //对菜单1进行选择
        switch(choose)
        {
        case 1:
            deletelinklist() ;                            //删除
            break;
        case 2:
            insertlinklist() ;                            //插入
            break;
        case 3:
            creatlinklistl();                         //创建
            break;
        case 4:                                           //退出
            exit(0);
        default:
            cout<<"输入错误"<<endl;
        }
        show();
    }
}
1