华清远见c语言学习笔记三

/*
 * double_list.c
 *
 *  Created on: Jun 28, 2012
 *      Author: 孙旭
 *      华请实验室
 */

/*double list双链表的创建,查找,删除,翩历*/ 
#include<stdio.h>    //头文件
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

typedef struct teacher   //结构体
{
 char name[15];       //姓名
 char teacher_id[15]; //id号
 int age;             //年龄
 struct teacher *fwd; //前指针
 struct teacher *bwd; //后指针
}ttd;

/*创建一个双链表*/
ttd *teacher_create(int nu)
{
 ttd *head,*s,*q;
 //head=q=NULL;
 if((q=(ttd *)malloc(sizeof(ttd)))==NULL)
 {
  printf("AN create the double list error\n");
  exit(0);
 }
 //scanf("%s %s %d",s->name,s->teacher_id,&s->age);
 printf("-----enter teacher informetion: neme id age------\n");
 q->fwd=NULL;
 q->bwd=NULL;
 head=q;
 while(nu--)
 {
  if((s=(ttd *)malloc(sizeof(ttd)))==NULL)
  {
   printf("---an create the new node error---");
   exit(0);
  }
  scanf("%s %s %d",s->name,s->teacher_id,&s->age);
  q->fwd=s;
  s->fwd=NULL;
  s->bwd=q;
  q=s;
 }
 head->bwd=s;
 s->fwd=head;
 return head;
}

/*前向翩历*/
void teacher_show1(ttd *head)
{
 ttd *p;
 p=head->fwd;
 while(p!=head)
 {
  printf("%s %s %d\n",p->name,p->teacher_id,p->age);
  p=p->fwd;
 }
}

/*后向翩历*/
void teacher_show2(ttd *head)
{
 ttd *q;
 q=head->fwd;
 while(q->fwd!=head)
  q=q->fwd;
 while(q!=head)
 {
  printf("%s %s %d\n",q->name,q->teacher_id,q->age);
  q=q->bwd;
 }
}

/*查找教师信息*/
ttd *teacher_search(ttd *head,char *name)
{
 ttd *p;
 p=head->fwd;
 while(p!=head)
 {
  if(strcmp(p->name,name)==0)
   return p;
  else
   p=p->fwd;
 }
 return NULL;

}

/*插入教师信息*/
ttd *insert(ttd *head,int nm,char *name)
{
 ttd *p,*s;
 p=head;
 if((p=teacher_search(head,name))==NULL)
 {
  printf("can`t find the name to insert\n");
  exit(0);
 }
 else
 {
  printf("-----enter teacher informetion: neme id age------\n");
  while(nm--)
  {
   if((s=(ttd *)malloc(sizeof(ttd)))==NULL)
     {
      printf("create  new node error\n");
      exit(0);
     }
   scanf("%s %s %d",s->name,s->teacher_id,&s->age);
   s->bwd=p->bwd;
   s->fwd=p;
   p->bwd->fwd=s;
   p->bwd=s;
  }
 }
 return head;
}

/**/
ttd *teacher_delete(ttd *head,char *name)
{
 ttd *p;
 p=head->fwd;
 while(strcmp(p->name,name))
 {
  p=p->fwd;
 }
 p->fwd->bwd=p->bwd;
 p->bwd->fwd=p->fwd;
 return head;
}
int main()
{
 ttd *head,*head2;
 int nu;
 int nm;
 char name1[20],name3[20],name4[20];

 printf("----请输入教师个数------\n");
 scanf("%d",&nu);
 head=teacher_create(nu);
 //head2=head;
 printf("---前序翩历-------\n");
 teacher_show1(head);
 printf("\n");
 printf("---后序翩历-------\n");
 teacher_show2(head);
 printf("\n");


 printf("-----请输入查找教师姓名-----\n");
 scanf("%s",name1);
 head2=teacher_search(head,name1);
 if(head2!=NULL)
 {
  printf("查找信息成功\n");
  printf("%s\t%s\t%d\n",head2->name,head2->teacher_id,head2->age);
 }
 else printf("查找教师信息不存在\n");
 printf("---请输入插入教师数目 ----\n");
 scanf("%d",&nm);
 printf("---请输入插入位置教师名字-----\n");
 scanf("%s",name3);
 head=insert(head,nm,name3);
 printf("------前序翩历-------\n");
 teacher_show1(head);

 printf("----请输入删除教师姓名---\n");
 scanf("%s",name4);
 head=teacher_delete(head,name4);
 printf("------前序翩历-------\n");
 teacher_show1(head);

 return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值