| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 334 人关注过本帖
标题:问个链表白痴问题
只看楼主 加入收藏
fleksin
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-7-10
结帖率:100%
收藏
 问题点数:0 回复次数:1 
问个链表白痴问题
有一个问题。。。删除和查找没用,为什么?
就这个问题了

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxlen 20
#define ERROR(str) printf("%s\n", str)
#define DATAFILE "all_stu.txt"
#define TITLE printf("  书名            科目             原价             折扣 \n")
struct student
{
      char name[20];
    char cata[20];
    int price;
    int dis;
};

typedef struct student elementtype;


struct nodelist
{
    struct nodelist *next;
    elementtype data;
};

typedef struct nodelist node;

node *initial_node()
{
    node *L;
    L=(node *)malloc(sizeof(node));
    L->next=NULL;
    return L;
}
//添加
node *nodeadd(node *L, elementtype x)
{
    node *S, *P = L;
    int k = 0;
    S=(node*)malloc(sizeof(node));
    S->data=x;
    S->next=P->next;
    P->next=S;
    return P;
}

//删除
node *nodedel(node *L, char n[20])        //elementtype x
{
    node *S, *P = L;
    //int k = 0;
    while( (P->next != NULL) && (P->next->data.name != n ))   //x.name
        P = P->next;
    if (P->next!= NULL)   //
    {

        S = P->next;
        P->next = S->next;
        free(S);
    }
    /*else
    {
        S=P->next;
        P->next=NULL;
        free(S);
   
    }*/
    //return P->next;
}
//修改
node *nodechange(node *L, elementtype x)
{
    node *P = L;
    char k[20];
    printf("请输入要替换的书名:\n");
    scanf("%s", k );
    while( (P->next != NULL) && (P->next->data.name != k))
        P = P->next;
   
    P->next->data = x;
   
    return P->next;
}
//查询
node *nodequa(node *L)   
{
    int i, j;
    node *P;
    char n[20];
    P = L;
    printf("按书名查找按1,科目按2,价格按3");    //分类查找
    scanf("%d",&i);   
    switch(i)
    {
    case 1:                                       //按书名
        printf("请输入书名:");
        scanf("%s", &n);        
        TITLE;
        while(P->next != NULL)
        {
            if(P->next->data.name == n)
            printf("  %s              %s               %d               %d \n;",
                      P->next->data.name, P->next->data.cata , P->next->data.price , P->next->data.dis);
            P=P->next;
        };
        break;
    case 2:             //按科目
        printf("请输入科目名:");
        scanf("%s", &n);
        TITLE;
        while(P->next != NULL)
        {
            if(P->next->data.cata == n)
            printf("  %s              %s               %d               %d \n;",
                   P->next->data.name, P->next->data.cata , P->next->data.price , P->next->data.dis);
            P=P->next;
        };
        break;
    case 3:                      //按价格
        printf("请输入价格:");
        scanf("%d", &j);
        TITLE;
        while(P->next != NULL)
        {
            if(P->next->data.price == j)
            printf("  %s              %s               %d               %d \n;",
                   P->next->data.name, P->next->data.cata , P->next->data.price , P->next->data.dis);
            P=P->next;
        };
        break;
    }





   
    /*if(P != NULL)
    {
        TITLE;
        printf("  %s              %s               %d               %d \n;",
                   P->next->data.name, P->next->data.cata , P->next->data.price , P->next->data.dis);
        
    }*/   
    return P->next;
}

//浏览
void list_print(node *L)
{
    node *P= L->next;
    TITLE;
    P= L->next;
    while(P != NULL)
    {
        printf("  %s%17s%17d%17d \n",
                   P/*->next*/->data.name, P/*->next*/->data.cata , P/*->next*/->data.price , P/*->next*/->data.dis);
        P=P->next;
    }

  printf("\n");
  printf("\n");
}

int getlistlen(node *L)
{
    node *P = L->next;
    int num = 0;
    while(P != NULL)
    {
        num++;

        P=P->next;
    }
    return num;
}

//存盘
node *savelist(node *L, char *filename)
{
    node *P = L->next;
    FILE *fd;
    int num;
    fd = fopen(filename, "w");
    if(fd == NULL)
        ERROR("file do not open!");

    num = getlistlen(L);
    fprintf(fd, "%d ", num);

    while(P != NULL)
    {
        fprintf(fd, "%s ", P->data.name);
        fprintf(fd, "%s ", P->data.cata);
        fprintf(fd, "%d ", P->data.price);
        fprintf(fd, "%d ", P->data.dis);
        P=P->next;
    }
  fclose(fd);
  return P;
}  

//读盘
node *readlist(node *L, char *filename)
{
    node *P = L->next;
    FILE *fd;
    int i, num;
    elementtype stu;
    fd = fopen(filename, "r");
    if(fd == NULL)
        ERROR("file do not open!");

    fscanf(fd, "%d", &num);
    for(i=0; i<num; i++)
    {
        fscanf(fd, "%s%s%d%d", &stu.name, &stu.cata, &stu.price, &stu.dis);
        nodeadd(L, stu);
    }

  fclose(fd);
  return P;
}  

void printmenu( void )
{
    printf("〓//////////////////// 二手书信息管理系统 ////////////////////// 〓\n");
    printf("〓                          1 添加                               〓\n");
    printf("〓                          2 删除                               〓\n");
    printf("〓                          3 查询                               〓\n");
    printf("〓                          4 修改                               〓\n");
    printf("〓                          5 浏览                               〓\n");
    printf("〓                          6 存盘                               〓\n");
    printf("〓                          7 读盘                               〓\n");
    printf("〓                          8 退出                               〓\n");
    printf("〓////////////////////////  请输入://////////////////////////// 〓 \n");
}


int main()
{
    int i ;
    char c[20];
    node * all_stu;
    node * all_stu2;
    int choise;
    elementtype stu;
    i=1;
    all_stu = initial_node();//初始化
    //printmenu();
    while(1)
    {
        printf("\n");
        printmenu();
        scanf("%d", &choise);
        switch(choise)
        {
        case 1:
            printf("添加\n");
            printf(" 请输入书名,科目,原价,折扣\n");
            do //添加
            {
                scanf("%s%s%d%d", &stu.name, &stu.cata, &stu.price, &stu.dis);
                nodeadd(all_stu, stu);
            }while(stu.dis != 0);
            /*do //添加
            {
                scanf("%s%s%d%d", &stu.name, &stu.cata, &stu.price, &stu.dis);
                nodeadd(all_stu, stu);
                printf("继续录入请按1,返回请按0");
                scanf("%d", &i);
            }while(i != 0);*/
            break;

        case 2:
            printf("2 删除\n");
            printf(" 请输入书名\n");
            scanf("%s", c);//&stu.name, &stu.cata,&stu.price,&stu.dis);%s%d%d
            nodedel(all_stu, c);  // 删除stu);
            break;

        case 3:
            printf("3 查询\n");
            nodequa(all_stu);  // 查询
            break;

        case 4:
            printf("4 修改\n");
            printf(" 请输入修改后的信息\n");
            scanf("%s%s%d%d", &stu.name, &stu.cata, &stu.price, &stu.dis);
            nodechange(all_stu, stu);  // 修改
            break;

        case 5:
            printf("5 浏览\n");
            printf("所有书籍: \n");
            list_print(all_stu); //浏览
            break;

        case 6:
            printf("6 存盘\n");
            printf("保存\n");
            savelist(all_stu, DATAFILE); //存盘
            break;

        case 7:   
            printf("7 读盘\n");
            all_stu2 = initial_node();//初始化
            printf("读取 \n");
            readlist(all_stu2, DATAFILE); //读盘
            printf("读盘结果 \n");
            list_print(all_stu2);
            break;

        case 8:   
            printf("8 退出\n");
            exit(1);
            break;
        
        default:
            printf("输入错误!\n");
        }
    }
}
搜索更多相关主题的帖子: 链表 
2010-07-10 22:23
fleksin
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-7-10
收藏
得分:0 
自己沙发一个,第一次来就发贴,我是来找骂的
2010-07-10 22:35
快速回复:问个链表白痴问题
数据加载中...
 
   



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

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