链表技术。

 

#include <stdio.h>

#include <stdlib.h>

 

#define ERROR1  "*** Wrong : the position is less than 1 ***/n"

#define ERROR2  "*** Wrong : the position is more than the length of linklist ***/n"

 

 

typedef struct node 

{

   int data;

   struct node *next;

} listnode, *linklist;

 

 

 

/***************************************/

void print()

{

   printf("******************linklist a********************/n");

   printf("*                                              */n");

   printf("* 1>  insert a node into the linklist          */n");

   printf("* 2>  insert a node into the linklist ascendly */n");

   printf("* 3>  delete a node depending on position      */n");

   printf("* 4>  delete nodes depending on value          */n");

   printf("* 5>  search the linklist according to a value */n");

   printf("* 6>  return the value depending on position   */n");

   printf("* 7>  clear the linklist                       */n");

   printf("* 8>  reverse the linklist                     */n");

   printf("* 9>  merge the linklist with La and Lb        */n");

   printf("* 10> exit                                     */n");

   printf("*                                              */n");

   printf("************************************************/n");

}

 

 

 

/***************************************/

linklist CreateLinklist_1()

{

   linklist head;

 

   head = (linklist)malloc(sizeof(listnode));

   head->next = NULL;

 

   return head;

}

 

 

 

/***************************************/

void CreateLinklist_2(listnode **head)

{

   *head = (linklist)malloc(sizeof(listnode));

   (*head)->next = NULL;

 

   return ;

}

 

 

 

/***************************************/

int LengthLinklist(linklist head)

{

   int count = 0;

   linklist p = head->next;

 

   while( p )

   {

      count++;

      p = p->next;

   }

 

   return count;

}

 

 

 

/***************************************/

void VisitLinklist(linklist head)

{

   linklist p = head->next;

 

   while ( p )

   {

      printf("%d ", p->data);

      p = p->next;

   }

   printf("/n"); 

 

 

 

/***************************************/

int EmptyList(linklist head)

{

   return ( NULL == head->next ? 1 : 0 );

}

 

 

 

/***************************************/

int SearchLinklist(linklist head, int value)

{

   linklist p = head->next;

   int pos = 1;   

 

   while ( p )

   {

      if ( p->data == value )

      {

         return pos;

      }

      p = p->next;

      pos++;

   }

 

   return 0;

}

 

 

 

/***************************************/

int GetLinklist(linklist head, int pos, int *val)

{

   linklist p = head;

 

   if ( pos < 1 )

   {

      printf(ERROR1);

      return -1;

   }

 

   while ( pos-- )

   {

      p = p->next;

      if ( NULL == p )

      {

         printf(ERROR2);

         return -1;

      }

   }

 

   *val = p->data;

 

   return 0;

}

 

 

 

/***************************************/

int InsertLinklist_1(linklist head, int pos, int value)

{

   linklist p,q;

 

   if ( pos < 1 ) 

   { 

      printf(ERROR1);

      return -1;

   }

   p = head;

   while ( --pos )

   {

      p = p->next;

      if ( NULL == p ) 

      {

         printf(ERROR2);

         return -1;

      }

   }

   q = (linklist)malloc(sizeof(listnode));

   q->data = value;

   q->next = p->next;

   p->next = q;

 

   return 0;

}   

 

 

 

/***************************************/

void InsertLinklist_2(linklist head, int value)

{

   linklist p = head, q;

 

   while ( (p->next != NULL) && (p->next->data < value) )

   {

      p = p->next;

   }

   q = (linklist)malloc(sizeof(listnode));

   q->data = value;

   q->next = p->next;

   p->next = q;

 

   return;

}

 

 

 

/***************************************/

int DeleteLinklist_1(linklist head, int pos)

{

   linklist p = head,q;

 

   if ( pos < 1 ) 

   {

      printf(ERROR1);

      return -1;

   }

 

   while( --pos )

   {

      p = p->next;

      if ( NULL == p )

      {

         printf(ERROR2);

         return -1;

      }

   }

   if ( NULL == (q = p->next) )

   {

      printf(ERROR2);

      return -1;

   }

 

   p->next = q->next;

   free(q);

 

   return 0;

}

 

 

 

/***************************************/

void DeleteLinklist_2(linklist head, int value)

{

   linklist p,q;

 

   p = head;

   q = p->next;

 

   while( q )

   {

      if ( q->data == value )

      {

         p->next = q->next;

         free(q);

      }

      else

      {

         p = q;

      }

      q = p->next;

   }

 

   return;

}

 

 

 

/***************************************/

void ClearLinklist(linklist head)

{

   linklist p,q;

 

   p = head->next;

   while ( p )

   {

      q = p;

      p = p->next;

      free(q);

   }

   head->next = NULL;

 

   return;

}

 

 

 

/***************************************/

void MergeLinklist(linklist La, linklist Lb)//按链表数值的大小连接两个链表

{

   linklist pa,pb,r;

 

   r = La;

   pa = La->next;

   pb = Lb->next;

 

   while ( pa && pb )

   {

      if ( pa->data < pb->data )

      {

         r->next = pa;

         r = pa;

         pa = pa->next;

      }

      else

      {

         r->next = pb;

         r = pb;

         pb = pb->next;

      }

   }

 

   if ( pa == NULL ) 

   {

       r->next = pb;

   }

   else

   {

       r->next = pa;

   }

   Lb->next = NULL;   

 

   return;

}  

 

 

 

/***************************************/

void ReverseLinklist(linklist head)//对单链表实现就地逆转。

{

 

   linklist p,q;

 

   p = head->next;

   head->next = NULL;

 

   while(p)

   {

      q = p;  

      p = p->next;

      q->next = head->next;

      head->next = q;

   }

}

 

 

/***************************************/

void ReverseLinklist_1(linklist h, linklist p)

{

   linklist q;

 

   if ( NULL == p ) return;

   else

   {

       q = p->next;

       p->next = h->next;

       h->next = p;

       ReverseLinklist(h, q);

   }

}

 

 

 

/***************************************/

 

int main()

{

   int i,value,pos,result;

   linklist ha, hb;

 

   ha = CreateLinklist_1();

   CreateLinklist_2(&hb);

 

   for(i=0;i<10;i++)

   {

      InsertLinklist_1(hb,i+1,i+1);

   }

   printf("length of the linklist(b) : %d /n", LengthLinklist(hb));

   VisitLinklist(hb);

 

   while ( 1 )

   {

      print();

      printf("Please choose your operation : ");

      scanf("%d", &i);

      if ( i == 10 ) break;

      switch ( i )

      {

         case 1 : 

                  printf("Please input as format <value> <pos> : ");

                  scanf("%d %d", &value, &pos);

                  InsertLinklist_1(ha, pos, value);

                  break;

         case 2 :

                  printf("Please input as format <value> : ");

                  scanf("%d", &value);

                  InsertLinklist_2(ha, value);

                  break;

         case 3 :

                  printf("Please input the position you want to delete : ");

                  scanf("%d", &pos);

                  DeleteLinklist_1(ha, pos);

                  break;

         case 4 :

                  printf("Please input the value you want to delete : ");

                  scanf("%d", &value);

                  DeleteLinklist_2(ha, value);

                  break;

         case 5 :

                  printf("Pleae input the value you want to search : ");

                  scanf("%d", &value);

                  if ( (result=SearchLinklist(ha, value)) > 0 )

                  {

                     printf("# Yes, we got it, the position is %d/n", result);

                  }

                  else

                  {

                     printf("# No, we can't find it/n");

                  }

                  break;

         case 6 :

                  printf("Please input the position you want to get : ");

                  scanf("%d", &pos);

                  if ( GetLinklist(ha, pos, &value) == 0 )

                  {

                     printf("The value of node #%d is %d/n", pos, value);

                  }

                  break;

         case 7 :

                  ClearLinklist(ha);

                  break;

         case 8 :

                  ReverseLinklist(ha);

                  break;

         case 9 :

                  MergeLinklist(ha, hb);

                  break;

      }

      printf("length of the linklist(a) : %d /n", LengthLinklist(ha));

      VisitLinklist(ha);

      printf("/n");

   }

 

   ClearLinklist(ha);

   ClearLinklist(hb);

   free(ha);

   free(hb);

 

   return 0;

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值