链表插入 大家帮帮忙阿 谢谢了
我分文件作的 , 总共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;
}
}
谢谢 帮忙阿