首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

看上这个代码。

2012-08-24 
看下这个代码。。。。。[codeC/C++][/#include stdio.h#include stdlib.h#includestring.htypedef struc

看下这个代码。。。。。
[code=C/C++][/#include <stdio.h>
#include <stdlib.h>
#include<string.h>

typedef struct student {
char name[10];
int num;
struct student * next;
}STU;

STU *creat(int n); //创建链表
STU *look_for(STU *head,int m); //寻找结点
STU * del_point(STU *head,int m); //删除结点 
STU *insert_point(STU *head,STU *pi); //插入结点
void link_free(STU *head); //
void print_all(STU *head); //显示链表
STU *look_name(STU *head,char *name);


int main()
{
int num,num1;
char look_name1[10];
STU * head=NULL;
STU *pb,*p;
STU *pi;
pi=(STU *)malloc(sizeof(STU));
head=creat(5);
print_all(head);

//============look_for(STU *head,int m)测试模块=====
printf("please input the number you look for.\n");
scanf("%d",&num);
pb=look_for(head,num);
if(pb!=NULL)
printf("search the number:%d name: %s \n",pb->num,pb->name);
else
printf("can not find the number %d\n",num);

//==============look_name(STU *head,char name[])测试模块=====
printf("please input the name what you want look for\n");
  scanf("%s",look_name1);
p=look_name(head,look_name1);
if(p!=NULL)
printf("search the number:%d name: %s \n",pb->num,pb->name);
else
printf("can not find the name %d\n",num);


//===========del_point(STU *head,int m)测试模块=============
printf("the num you want del\n");
scanf("%d",&num1);
head=del_point(head,num1);
print_all(head);

//============insert_point(STU *head,STU *pi)测试模块======
  printf("please input insert pi->num pi->age\n");
scanf("%d %s",&pi->num,&pi->name);
pi->next=NULL;
  head=insert_point(head,pi);
print_all(head);
link_free(head);
return 0;
}

/*===============链表创建函数=============================*/
STU *creat(int n) //创建链表
{
int i;
STU * head,*pb,*pf;
for(i=0;i<n;i++)
{
pb=(STU *)malloc(sizeof(STU));
printf("please input num name:\n");
scanf("%d %s",&(pb->num),&(pb->name));
getchar();
if(i==0)
{
head=pf=pb;
}
else
{
pf->next=pb;
pf=pb;
}
}
pb->next=NULL;
return head;
}
/*=======================================================*/

/*===============寻找结点函数=============================*/
STU *look_for(STU *head,int m) //寻找结点
{
STU *pf;
pf=head;
  while(pf!=NULL)
{
if(pf->num==m)
return pf;
else
pf=pf->next;
}
return NULL;
}
/*==========================================================*/

/*===============结点删除函数===============================*/
STU * del_point(STU *head,int m) //删除结点
{
STU *pb,*pf;
pb=pf=head;
if(head==NULL)
{
printf("link is empty\n");
return NULL;
}
  while((pb->next!=NULL)&&(pb->num!=m)) //寻找 所要删除的结点
{
pf=pb;
pb=pb->next;
}
if(pb->num==m) //找到了 所要删除的结点
{
if(pb==head) //所删除的是 头结点
{
head=head->next;
}
else // 所删除的是普通结点
{
pf->next=pb->next;
}
free(pb); //释放所删除结点的 空间
}
else
{
printf("no this point.\n");
}
return head;
}

/*================遍历链表函数===============================*/
void print_all(STU *head) //显示 链表 元素
{
STU *pb;
pb=head;
while(pb!=NULL)
{
printf("num=:%d, name=:%s\n",pb->num,pb->name);


pb=pb->next;
}
}
/*=============================================================*/

/*===============结点插入函数==================================*/
STU *insert_point(STU *head,STU *pi) //插入结点
{
STU *pf,*pb;
pb=pf=head;
if(head==NULL) //空链表 插入
{
head=pi;
head->next=NULL;
return head;
}
while((pb->next!=NULL)&&(pb->num<pi->num)) //比较大小 按顺序插入
{
pf=pb;
pb=pb->next;
}
if(pb->num>=pi->num) //找到了所要插入的位置 (从小到大)
{
if(pb==head) //插入到 链表 的 头
{
head=pi;
pi->next=head;
}
else
{ //插入到普通位置
pf->next=pi;
pi->next=pb;
}
}
else //插入到链表的 尾
{
pb->next=pi;
pi->next=NULL;
}
return head;
}
/*=============================================================*/

void link_free(STU *head)
{
STU *pb;
pb=head;
while(head!=NULL)
{
pb=head;
head=head->next;
free(pb);
}
}

//==============look_name()函数========
STU *look_name(STU *head,char *name) //寻找结点
{
STU *pf;
pf=head;
  while(pf!=NULL)
{
if(strcmp(pf->name,name)==0)
return pf;
else
pf=pf->next;
}
return NULL;
}]

[解决办法]
[size=16px][color=#FF0000]1.请用[code=C/C++][/code]插入。
2.简单介绍一下功能。
3.对这段代码有什么疑问。
4.。。。。。。。。。。。。[/color][/size]
[解决办法]
在[code=C/C++][/code]之间,即"c++]"与"[/"之间插入源代码就可以了。

热点排行