带头结点的链表

/*带头结点的链表*/


#include<stdio.h>

#include<stdlib.h>

 

struct stuhead/*头结点的结构体类型,记录长度*/

{

   int length;

       structstu *next;

};

 

struct stu/*其他结点的结构类型*/

{

       intnum;

   struct stu *next;

};

 

struct stuhead *initlist(void)/*建立链表,先建立头结点,期中要求输入长度,根据长度来建立后面相应个数的结点*/

{

       intn,i;//n记录表中的元素个数,i用于循环

       structstuhead *head;

       structstu *p,*ph;/*PH是辅助指针,P向后移动,用PH串联起来*/

       printf("howmany element your list have?\n");

       scanf("%d",&n);

   head=(struct stuhead*)malloc(sizeof(struct stuhead));/*给头结点释放一个stuhead型的空间*/

      if(head==NULL)/*判断是否分配成功*/

       {

       printf("malloc error!!!!");

              return0;

         }

         head->next=NULL;//讲头结点指向NULL

   p=(struct stu*)malloc(sizeof(struct stu));

       if(p==NULL)

              {

                     printf("mallocerrror");

           return 0;

              }

       head->next=p;

     ph=p; /*将头结点与第一个结点连接起来,因为类型不同,需要单独列出来,不能放在循环中*/

   for(i=1;i<=n-1;i++)

       {

       p=(struct stu*)malloc(sizeof(struct stu));

       if(p==NULL)

              {

                     printf("mallocerrror\n");

           return 0;

              }

              ph->next=p;

              ph=p;

  }/*用PH将后面的结点连接起来*/

   p->next=NULL;//链尾巴指向NULL

       head->length=n;/*头结点中放入长度 */

       printf("*************************************\n");

 return head;

}

 

void typeelem(struct stuhead *head)//输入相应个数的元素

{  

   

       inti;

       structstu *p;

       p=head->next;

//     printf("%d",head->length);   

   printf("type in your element!!as long as it is init!!\n");

       for(i=0;i<=((head->length)-1);i++)

       {  

              scanf("%d",&(p->num));

            p=p->next;

       }

      

       printf("\n*************************************\n");

}

 

         

void printlist(struct stuhead *head)//打印元素的内容

{

       inti;

       structstu *p;

       p=head->next;

       for(i=0;i<=head->length-1;i++)

       {

              printf("%4d",p->num);

              p=p->next;

       }

printf("\n*************************************\n");

 

}

 

void clearlist(struct stuhead *head)//将表中的元素置为空的,即使置为零

{

    int i;

     struct stu *ph;

    ph=head->next;    

    for(i=0;i<=head->length-1;i++)

       if(ph->next!=NULL)

          {

          ph->num=0;

           ph=ph->next;

           }          

          ph->num=0;

          printf("*************************************\n");

}

 

int listempty(struct stuhead *head)

{

    if(head->next==NULL)

      return 1;

    else

      return 0;

    printf("*************************************\n");

}           

 

int getelem(struct stuhead *head,int n)

{

   struct stu *ph;

   int i;

   ph=head->next;

   for(i=1;i<=n-1;i++)

      ph=ph->next;

   return ph->num;

   printf("*************************************\n");

}

 

int listlength(struct stuhead *head)

   int k=0;

   struct stu *ph;

   ph=head->next;

    while(ph->next!=NULL)

    {

       k++;

       ph=ph->next;

   }  

   k++;

   return k;

  

   }        

       

int priorelem(struct stuhead *head,int num)

{

   struct stu *ph,*pb;

  ph=head->next;

  if(num==ph->num)

    {

    printf("no prior");

    exit(0);

    }

    pb=ph;

  ph=ph->next;

   do

   {

     if(ph->num==num)

        return pb->num;

     else

        {

        pb=ph;

        ph=ph->next;

        }

}while(ph->next!=NULL);

if(ph->num==num)

   return pb->num;

 

printf("cannot find");

}

 

int nextelem(struct stuhead *head,int num)

{

   struct stu *ph,*pf;

  ph=head->next;

    pf=ph->next;

  while(ph->next!=NULL)

   {

     if(ph->num==num)

        return pf->num;

     else

        {

        pf=ph->next;

        ph=ph->next;

        }

};

printf("cannot find");

 

}

 

void listinsert(struct stuhead *head,intloc,int num)

{  

   int i;

    struct stu *ph,*pnew,*p;

    ph=head->next;

    for(i=1;i<=loc-2;i++)

        ph=ph->next;

    pnew=(struct stu*)malloc(sizeof(struct stu));

    pnew->num=num;

    p=ph->next;

    ph->next=pnew;

    pnew->next=p;    

    head->length++;

    printf("***********************************************\n");

}

 

int listdelete(struct stuhead*head,int loc)

{

    int i,num;

    struct stu *ph,*p;

    ph=head->next;

     for(i=1;i<=loc-2;i++)

        ph=ph->next;

    p=ph->next;

    num=p->num;

    ph->next=ph->next->next;

    free(p);

    head->length--;

    printf("***********************************************\n");

     return num;

}    

    

int compare(int a,int b)/*判断a是否比b大一*/

{ if(a==(b+1))

  return 1;

  else

  return  0;

   }

 

int locateelem(struct stuhead *head,intn,int compare(int a,int b))/*找到表中比输入元素大一元素的位序*/

{

   struct stu *p;

   int k=0;

   p=head->next;

   while(p->next!=NULL)

   {  

       k++;

       if(compare(p->num,n))

       {//printf("%d",k);

       return k++;}

       p=p->next;

       }

  if(compare(p->num,n))

       return k+2;

  return 0;

}

 

void visit(int n)

   

   if((n/2.0-n/2)==0)

   printf("%d是偶数\n",n);

}

 

void listtraverse(struct stuhead *head,voidvisit(int n))

{

    struct stu *p;

    p=head->next;

    while(p->next!=NULL)

    {

       visit(p->num);

       p=p->next;

       }

       visit(p->num);

}       

 

void destroylist(struct stuhead *head)

{

     structstu *p,*ph;

    p=head->next;

    ph=head->next;

    while(p->next!=NULL)

    {

 

    ph=p->next;

    free(p);

   p=ph;

    }

    free(p);

    head->next=NULL;

   printf("***********************************************\n");

    

}

         

 

 

 

int main()

{

       intnum,n,loc;

   struct stuhead  *head=NULL;

      

      

       printf("*****************plesechose*******************\n");

   printf("   1.initilst\n   2.typeelem\n   3.destroy\n  4.clearlist\n   5.listempty\n   6.listlength\n   7.getlem\n  8.locateelem\n  9.priorelem\n   10.nextelem\n   11.listinsert\n   12.listdelete\n   13.listtraverse\n   14.printlist\n");

   printf("***********************************************\n");

loop:       scanf("%d",&n);

      

       switch(n)

                     {case1:

                                   {

                          head=initlist();

                            printf("请按你建立时输入的表的长度输入相应个数的元素!\n");

                    typeelem(head);

                                   break;

                                   }

                      case 2:

                                   {typeelem(head);

                                    break;

                                   }

                                    

         case 3:

                             { destroylist(head);

                             break;

                             }

             

           case 4:

                             { clearlist(head);

                             break;

                             }

                      case 5:

                             {

                                   if(listempty(head))

                    printf("listempty\n");

                    else

                    printf("notempty\n");

                                 break;

                             }

                      

                              

                      case 6:

                     {

                printf("表的长度是%d",listlength(head));

               printf("*************************************\n");

                            break;

                     }

                     case7:

                 {    printf("type in the element's numberyou want to get\n");

                     scanf("%d",&num);

                     printf("the elementis %d",getelem(head,num));

                     printf("*************************************\n");

                      break;

                      }

         case 8:{printf("输入元素\n");

                     scanf("%d",&num);

                if(locateelem(head,num,compare)==0)

                    printf("cannotfind\n");

                 else

                  //printf("%d",k);

                  printf("比%d大1的元素的位序是 %d\n",num,locateelem(head,num,compare)) ;    

                  printf("*************************************\n");           

                 break;

                }

              case 9:

                {

                      printf("type in yourelement\n");

                     scanf("%d",&num);

                     printf("the prioris %d\n",priorelem(head,num));

                      printf("*************************************\n");

                      break;

      

               }

            case 10:

                 {

                      printf("type in yourelement\n");

                     scanf("%d",&num);

                      printf("the prior is%d\n",nextelem(head,num));

                     printf("*************************************\n");

                       break;

               }

        

              case 11:

                {

                     printf("type in thelocation\n ");

                     scanf("%d",&loc);

                      printf("type in theelement\n");

                     scanf("%d",&num);

                      listinsert(head,loc,num);

                  

                      break;

                      }

      

         case 12:

               {

                      printf("type in thelocation\n");

                     scanf("%d",&num);

                      printf("%d is deleted\n",listdelete(head,num));

                      printf("*************************************\n");

                      break;

                      }

          

          case 13:{

                      listtraverse(head,visit);

                        printf("*************************************\n");

                       break;

                       }                                                            

              case 14:

                             { 

                                    printlist(head);

                                  

                                    break;

                             }   

}


       gotoloop;

       return0;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值