ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 链表练习

链表练习

原创 Linux操作系统 作者:wuft2003 时间:2009-06-20 12:39:35 0 删除 编辑

才发现insert 等还有问题,哈哈,过一段在改吧

#include
#include
#include
/***
** 这是链表的练习
***/

typedef struct student
{
    int data;
    struct student *next;
} node;
/** 创建队列 ***/
node * create()
{
     node *head, *s, *p;
     int x = 0;
    
     head = (node *) malloc( sizeof( node));
     p = head;

     printf(" Put the num ,exit when puted the number 0\n");
     while( 1 )
     {

        printf(" Put the num :  ");
        scanf("%d", &x);
       
        if( x != 0 )
        {
             s = (node *) malloc( sizeof( node));
             s->data = x;
             p->next = s;
             p = s;
        }else
        {
            p->next = NULL;
            break;
        }
     }
      head= head->next;
     return ( head );
}
/***
*** 打印链表,同时统计链表长度
***/

void list_print( node *list )
{
    int length = 0;
    node *p;
    p = list;

    if( p != NULL )
    {
        while( (p != NULL))
        {
            length++;
            printf(" %d \n", p->data );
            p = p->next;
        }
    }
   printf(" list's length is %d \n", length );
}

/***
** 删除一个节点:
**  如果是第一个节点 :
**  如果是中间的节点
***/
node *delete_node( node *head ,int num)
{
    node *p1,*p2;
    p1 = head;
    while( num != p1->data && p1->next != NULL)
    {
        p2 = p1;
        p1 = p2->next;
    }
   
    if( num == p1->data )
    {
      if( p1 == head )
      {
        head = p1->next;
        free(p1);
      }else
      {
          p2->next = p1->next;
      }
     
    }else
    {
        printf(" It doesn't find the num! \n");
    }
    return ( head);
}
/**  插入一个节点
** 1. 插入头 2 插入中间 3, 插入尾
***/
node *insert_node( node *head, int num )
{
    node *p0, *p1, *p2;
    /** 对插入节点 p0 赋值 **/
    p0 = (node *)malloc( sizeof( node ));
    p0->data = num;
   
    p1 = head;
   
    while( p0->data > p1->data && p1->next != NULL )
    {
        p2=p1;
        p1=p2->next;
    }
   
    if( (p0->data <= p1->data )&& (p1 == head ) )
    {
       p0->next = p1;
       head = p0;
    }else if( (p0->data <= p1->data )&& (p1 != head ))
    {
           p2->next = p0;
           p0->next = p1;
    }else
    {
        p1->next = p0;
        p0->next = NULL;
    }
    return (head );
}
/***
** 实现链表的排序,从小到大(冒泡排序)
***/
node * list_order( node *head )
{
    node *p1,*p2, *p3, *p4,*p5,*pa;

    int temp,temp1;
    p1 = head;    /** 防止指针地址被修改 */
   
    printf(" start order the list,list as follow before odered: \n") ;
    list_print( p1 );
     if( p1 == NULL  )
     return head;

    while( p1 != NULL  )
    {
          temp = p1 ->data ;
          /** 查找后面有比其还小的数据
          ***/
          pa=p1;
          p2 = p1->next;
          p4 = NULL;
          while(  p2 != NULL)
          {
              temp1 = p2->data;
              if ( temp1 < temp )
              {
                 temp = temp1;
                 p4=p2;
              }
              p3 = p2;
              p2 = p3->next;
          }
          if( p4 != NULL)
          {
            p4->data = p1->data;
            p1->data = temp;
          }
          p1 = pa->next;
    }
    return (head);
}

/**
** 实现链表的逆转
***/
node* node_reverse(node *head )
{
   node *p1,*p2,*p3;
   p1 = head ;
   p2 = p1->next;
  
   if( p1 == NULL || p2 == NULL )
   {
         return head;
   }

   while( p2 != NULL )
   {
       p3 = p2 ->next;
       p2->next = p1;
       p1 = p2;
       p2 = p3;
   }
   head->next = NULL;
   head = p1;
}

int main()
{
   node *list ;
   int add,del;
  
   list = create();

   list_print( list );
  

   printf(" Put add num : ");
   scanf("%d",&add);
   printf("\12");
  

   list = insert_node( list,add);
   list_print( list );
  
   printf("Put  delete num : ");
   scanf("%d", &del);
   list = delete_node( list,del);
   list_print( list );


    list = list_order( list );
  
   printf(" list after ordered as follow \n");
  
   list_print( list );

   list = node_reverse( list );
  
   list_print( list );

   return 0;
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21634752/viewspace-606994/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 子网划分方法
请登录后发表评论 登录
全部评论

注册时间:2009-05-12

  • 博文量
    295
  • 访问量
    322788