Chinaunix首页 | 论坛 | 博客
  • 博客访问: 624387
  • 博文数量: 144
  • 博客积分: 5037
  • 博客等级: 大校
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-30 21:49
文章存档

2010年(16)

2009年(128)

分类: WINDOWS

2009-04-10 21:19:45

/*=============================
 文件名: delete.c
 功能:     链表的删除(有序无序都适合)
 参数:     无
 返回值: 建链表的首地址
===============================*/

#include "main.h"

TYPE *delete()
{
    TYPE *pf,*pb;
    int num;
    printf("please put in the number of the element which will be deleted:\n");
    scanf("%d", &num);
    if(head==NULL)        //list is empty
    {
        printf("list is empty!\n");
        return NULL;                
    }    
    pb = head;                                 //不为空则载入列表
    while(pb->next != NULL && pb->num != num) //当不是要删除的结点,且也不是最后一个结点时,继续循环                                      

    {
        pf = pb;
        pb = pb->next;                         //pf指向当前结点,pb指向下一结点
    }                                            
    if (pb->num == num)
    {
        if(pb == head)
            head = pb->next;                 //如找到被删结点,且为第一结点,则使head指向第二个结点
        else
            pf->next = pb->next;             //否则使pf所指结点的指针指向下一结点
       
        free(pb);                             //释放删除节点所占空间
        pb = NULL;
        printf("The node is deleted\n");
    }
    else
        printf("The node not been found!\n");
        return head;
}

 

阅读(554) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~