| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 510 人关注过本帖
标题:链表的问题。
只看楼主 加入收藏
kim2014
Rank: 2
等 级:论坛游民
帖 子:21
专家分:11
注 册:2011-5-20
结帖率:100%
收藏
 问题点数:0 回复次数:4 
链表的问题。
有个小错误...

data_type.h

#ifndef    DATA_TYPE_H
#define DATA_TYPE_H

/*************************************
** 功能:                            **
**        为业务流程提供数据结构      **
**                                    **
*************************************/

#define     NAME_LENGTH        20
#define        NO_LENGTH        10

//学生的数据
typedef struct _STUDENT
{
    char    name[NAME_LENGTH];    /*学生姓名*/
    char    no[NO_LENGTH];        /*学号*/
    int        elec;                /*选修课成绩*/
    int     expe;                /*实验课成绩*/
    int        requ;                /*必修课成绩*/
    int        sum;                /*总分*/
}STUDENT, *PSTUDENT;

//链表结构类型
typedef struct _LIST
{
    //数据部
    STUDENT stu;
    //指针部
    struct _LIST* pst_next;
}LIST, *PLIST;

#endif

list.h
#ifndef    LIST_H
#define LIST_H

/***************************************
** 功能:                              **
**        为业务流程提供对链表的操作    **
**                                      **
***************************************/

//插入节点
int insert_node(STUDENT para_stu);
//int insert_node(STUDENT para _stu);

//查找节点
PLIST find_node(char *para_stu);

//删除节点
int delete_node(char *para_stu);

//显示链表
void show_list();

#endif

exe.c
#include <stdio.h>
#include <stdlib.h>

#include "list.h"
#include "data_type.h"

int main(void)
{
    STUDENT st_node = {'\0'};
    //插入函数返回值
    int     ret = 0;

    printf("请输入学号,姓名以及各个成绩:\n");
    scanf("%s%s", st_node.no, st_node.name);
    scanf("%d%d%d", &st_node.elec, &st_node.expe, &st_node.requ);

    ret = insert_node(st_node);

    if (ret == 8)
        printf("插入成功!\n");

    return 0;
}

list.c
/**************************************
** 功能:                             **
**        为业务逻辑提供对链表的操作   **
**                                     **
**************************************/

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#include "data_type.h"
#include "list.h"

//头节点
PLIST pgst_head = NULL;
//链表长度
int n_size = 0;

//插入节点
int    insert_node(STUDENT para_stu)
{
    PLIST pst_temp = NULL;

    //申请空间
    pst_temp = (PLIST)malloc(sizeof(LIST));

    //判断是否申请成功
    if (pst_temp == NULL)
    {
        return 8;
    }

    //赋值
    strcpy(pst_temp->stu.name, para_stu.name);
    strcpy(pst_temp->stu.no, para_stu.no);
    pst_temp->stu.elec = para_stu.elec;
    pst_temp->stu.expe = para_stu.expe;
    pst_temp->stu.requ = para_stu.requ;
    pst_temp->stu.sum = para_stu.elec + para_stu.expe + para_stu.requ;

    pst_temp->pst_next = NULL;

    //插入链表
    //空链表的情况
    if (pgst_head == NULL)
    {
        pgst_head = pst_temp;
        n_size++;
    }

    //链表不为空的情况
    else
    {
        pst_temp->pst_next = pgst_head;
        pgst_head = pst_temp;
        n_size++;
    }

    return 0;
}

// 查找节点
PLIST find_node(char *para_no)
{
    //定义一个临时变量代替head做遍历
    PLIST pst_text = pgst_head;

    //遍历整个链表进行查找
    while (pst_text != NULL)
    {
        if (strcmp(pst_text->stu.no, para_no) == 0)
        {
            return pst_text;
        }

        pst_text = pst_text->pst_next;
    }

    return pst_text;
}

//删除节点
int delete_node(char *para_no)
{
    PLIST pst_temp = NULL;
    PLIST pst_temp2 = NULL;

    pst_temp = pgst_head;

    //第一个节点就是要删除的节点
    if (strcmp(pst_temp->stu.no, para_no) == 0)
    {
        //将第二个链表节点赋给头节点
        pgst_head = pst_temp->pst_next;
        //删除第一个节点
        free(pst_temp);
        //清空
        pst_temp = NULL;

        n_size--;

        return 0;
    }

    //其他情况
    while (pst_temp->pst_next != NULL)
    {
        //找到了要删除的节点
        if (strcmp(pst_temp->pst_next->stu.no, para_no) == 0)
        {
            pst_temp2 = pst_temp->pst_next;
            pst_temp->pst_next = pst_temp2->pst_next;
            free(pst_temp2);
            pst_temp2 = NULL;

            n_size--;

            return 0;
        }

        pst_temp = pst_temp->pst_next;
    }

    return 0;
}

/*
//显示链表
void show_list()
{
    PLIST pst_temp = pgst_head;

    while (pst_temp != NULL)
    {
        show_infor(&(pst_temp->stu));
        pst_temp = pst_temp->next;
    }
}
*/









搜索更多相关主题的帖子: 数据 name 
2012-04-12 17:46
kim2014
Rank: 2
等 级:论坛游民
帖 子:21
专家分:11
注 册:2011-5-20
收藏
得分:0 
错误是expected '(' before para_stu.
      expected '=', ',', ''', 'asm' or '_attribute_' before find_node.
2012-04-12 17:55
kim2014
Rank: 2
等 级:论坛游民
帖 子:21
专家分:11
注 册:2011-5-20
收藏
得分:0 
显示是在list.h的错误。
2012-04-12 17:56
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
#ifndef    LIST_H
#define LIST_H

/***************************************
** 功能:                              **
**        为业务流程提供对链表的操作    **
**                                      **
***************************************/
#include "data_type.h"

//插入节点
int insert_node(STUDENT para_stu);
//int insert_node(STUDENT para _stu);

//查找节点
PLIST find_node(char *para_stu);

//删除节点
int delete_node(char *para_stu);

//显示链表
void show_list();

#endif

我们都在路上。。。。。
2012-04-13 10:45
kim2014
Rank: 2
等 级:论坛游民
帖 子:21
专家分:11
注 册:2011-5-20
收藏
得分:0 
回复 4楼 hellovfp
好几天没上这儿来了。我看没人回、就结贴了。不好意思。Thank you all the same!
2012-04-19 19:59
快速回复:链表的问题。
数据加载中...
 
   



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

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