| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 392 人关注过本帖
标题:链表插入 大家帮帮忙阿 谢谢了
只看楼主 加入收藏
liyandong106
Rank: 2
等 级:论坛游民
帖 子:21
专家分:35
注 册:2009-9-4
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
链表插入 大家帮帮忙阿 谢谢了
我分文件作的 , 总共6个文件,大家帮忙看看,谢谢大虾们,
(chain.h)
#ifndef CHAIN
#define CHAIN
#define T_true 1
#define ERR 0
#define END 9
#define SAME 6
#define DIF 2
#define HAIL 3
#define HOST 4
#define RET 5
typedef struct
{
    int num;    //学号
    int age;    //学生的年龄
}sStuMess, *pStuMess;
typedef struct student
{
    sStuMess data;
    struct student *next;   // 后继域
    /* 函数:打印学生信息
       形参:学生信息*/
    void (* stu_print)();
        /* 函数:输入学生信息
       形参:学生信息*/
    int (* stu_input)();    //
}sStu, *pStu;
typedef struct list
{    //链表头结点
    pStu head;
       /* 函数:向链表插入结点
       形参:链表头结点、要插入结点、
    判断插入结点是否存在(链表头结点,插入结点),
    判断插入结点与当前结点的大小(链表头结点,插入结点)*/
    int (* insert)(struct list *, pStu, int (*live)(), int (*find_insert)());
         /* 函数:删除链表中结点
       形参:链表头结点、删除结点条件*/
    //int (*del)(struct list * , int (*)());
     /* 函数:打印链表中每个结点信息
       形参:链表头结点*/
    void (*print)();
     /* 函数:查找学生信息
       形参:链表头结点,查找条件函数*/
    //int (*search)(struct list * , int (*)());
   
}sList, *pList;
int input(sStuMess *pdata);
int count_insert(sList *plist,sStu *pstu, int (*live)(), int (*find_insert)());
int insert_sort(sStu *phead, sStu *pstu);
int live_stu(sList *phead, sStu *pstu);
void printl(sStu *phead);
#endif

(main.c)
#include <stdio.h>
#include "chain.h"
#include "../pub/mymalloc.h"
int main()
{
    int stu_opt;
    sList *pls;
    sStu *pstu;   
   
    pls=(sList *)mymalloc(sizeof(sList));
    pls->head=(sStu *)mymalloc(sizeof(sStu));
    pls->head->next=NULL;
   
   

    while(T_true)
    {
        printf("please choose a num(1:new,2:del,3:print,4:search):");
        scanf("%d",&stu_opt);
        switch(stu_opt)
        {
            case 1:
                pstu=(sStu *)mymalloc(sizeof(sStu));
                pstu->stu_input=input;
                pstu->stu_input(&pstu->data);
                pls->insert=count_insert;
                pls->insert(pls,pstu,live_stu,insert_sort);
      
                break;
               
            case 3:
               
                pls->print=printl;
                pls->print(pls->head);
                printf("*******\n");
                break;
                       
      
        default:
            printf("opreate end!\n");
            return END;
        }
    }
}
3.
#include <stdio.h>
#include "chain.h"
int count_insert(sList *plist,sStu *pstu, int (*live)(), int (*find_insert)())
{
    int ret;
    int res;
   
    sStu *ptemp;
    ptemp=plist->head;
    if(pstu == NULL)
    {
        printf("count_insert err!\n");
        return ERR;
    }
   
    if(ptemp->next == NULL)
    {
        ptemp->next=pstu;
    //    pstu->next=NULL;
        //ptemp=ptemp->next;
    }
    else
    {
        ret=live(plist,pstu);
           
        if(ret == SAME)
        {
           
            return RET;
        }
        else if(ret == DIF)
        {   
            ptemp=ptemp->next;
           
            while(ptemp!=NULL)
            {
                printf("***********\n");
                res=find_insert(ptemp,pstu);
               
                if(res==HOST)
                {
                    printf("aaa\n");
                    ptemp->next=pstu;
                    pstu->next=ptemp->next;
                  
                }
           
                ptemp=ptemp->next;
               
            }
            
                ptemp=pstu;
                pstu->next=NULL;
                       
               
        }
   
    }
}
4.
#include <stdio.h>
#include "chain.h"
#include "../pub/mymalloc.h"
int live_stu(sList *phead, sStu *pstu)
{
    sStu *pthis;
           
    pthis=phead->head;
    if(pthis == NULL)
    {
        return 0;
    }
   
    while(pthis!= NULL)
    {
   
        if(pthis->data.num == pstu->data.num )
        {
            printf("have a same number!\n");   
           
            return SAME;
           
        }
        pthis=pthis->next;
    }
    return DIF;
}
5.
#include <stdio.h>
#include "chain.h"
#include "../pub/mymalloc.h"
int insert_sort(sStu *phead, sStu *pstu)
{
    sStu *pthis;
    pthis=phead;
        printf("%d,%d\n",pthis->data.num,pstu->data.num);
   
        if(pthis->data.num < pstu->data.num )
        {
            if(pthis->next!=NULL)
            {
                printf("find a safe space!\n");
                return HOST;
            }
            else
            {
                return 0;
            }
           
        }
      
   
      

    return 0;
   
}
6.
#include <stdio.h>
#include "chain.h"

int input(sStuMess *pdata)
{
    printf("num:");
    scanf("%d",&pdata->num);
    if(pdata->num <= 0)
    {
        printf("input err(num)!\n");
        return ERR;
    }
    printf("age:");
    scanf("%d",&pdata->age);
    if(pdata->age <= 0)
    {
        printf("input err(age)!\n");
        return ERR;
    }
   
}
   
void printl(sStu *phead)
{
    sStu *pl;
    pl=phead->next;
   
    while(pl!=NULL)
    {
        printf("******\n");
        printf("name:%d\n",pl->data.num);
        printf("age:%d\n",pl->data.age);
   
        pl=pl->next;
    }
}
谢谢 帮忙阿


搜索更多相关主题的帖子: 链表 
2009-09-05 17:28
choco1024
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:183
专家分:140
注 册:2008-8-31
收藏
得分:14 
有点崩溃。。。
2009-09-05 23:38
快速回复:链表插入 大家帮帮忙阿 谢谢了
数据加载中...
 
   



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

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