C语言——动态数据结构,将一个链表中元素值为x的结点删除。(链表数据域为整数,初始长为6个元素)

将一个链表中元素值为x的结点删除。(链表数据域为整数,初始长为6个元素)
程序运行示例如下:
输入数组6个元素的值。
11 22 33 44 55 66
此链表各个结点的数据域为:11 22 33 44 55 66
输入要删除的数据x: 33
删除后链表各个结点的数据域为:11 22 44 55 66 

#include <stdio.h>
#include <stdlib.h>

#define N 6

struct LNode
{  	  		 		 
    int data;
    struct LNode *next;
}  	  		 		 ;

struct LNode* create_rear(int a[], int n);
void output(struct LNode *h) ;
struct LNode* delete_node(struct LNode* h, int x);

int main(int argc, char *argv[])
{  	  		 		 
    int a[N], i, x;
    struct LNode* head;

    printf("输入数组%d个元素的值。\n", N);
    for (i = 0; i < N; i++)
        scanf("%d", &a[i]);

    /*创建链表head,其结点的值依次为数组a元素的值*/
    head = create_rear(a, N);
    /*删除前输出链表head*/
    printf("此链表各个结点的数据域为:");
    output(head);

    printf("输入要删除的数据x: ");
    scanf("%d", &x);
    head = delete_node(head, x);   /*调用删除函数*/
    printf("删除后链表各个结点的数据域为:");
    output(head);  /*删除后输出链表head*/

    return 0;
}  	  		 		 

struct LNode* create_rear(int a[], int n)
{  	  		 		 
    /*新建一个链表h,每个结点依次插入到链尾,将链表的头指针返回 */
    struct LNode *h = NULL;
    struct LNode *s, *r; /*用s指向要插入结点,r指向链表的尾结点*/
    int i;

    for (i = 0; i < n; i++)
    {  	  		 		 
        s = (struct LNode *) malloc(sizeof(struct LNode));
        s->data = a[i];
        s->next = NULL;
        if (h == NULL)
            h = s;        /*如果链表为空,则头指针h指向s */
        else
            r->next = s;  /*否则将s链接到尾结点r之后     */
        r = s;                /*将r指向尾结点                */
    }
    return h;  /*返回链表的头指针*/
}  	  		 		 

void output(struct LNode *h)
{  	  		 		 
    /*将链表h的各个结点的数据域依次输出,即遍历该链表*/
    struct LNode *p = h;/*从第一个结点开始,用p依次指向各个结点*/
    while (p)
    {  	  		 		 
        /*只要p是一个非空结点,则输出其数据域,然后将p后移*/
        printf("%d ", p->data);
        p = p->next;  //将p后移
    }
    printf("\n");
}  	  		 		 

struct LNode* delete_node(struct LNode* h, int x)
{  	  		 		 
    /*将链表h中值为x的结点第一个结点删除,并返回头指针。*/
    struct LNode *pre, *p;/*pre所指结点为p所指结点的前驱*/

    p = h;
    while (p && x != p->data)
    {  	  		 		 
        /*如果p不空,且x不等于p所指结点的数据域,p后移,pre为p的前驱*/
        pre = p;
        p = p->next;
    }
    if (p)
    {  	  		 		 
        /*在链表中找到了要删除的结点p,即p->data为x*/
        if (p == h)
        {  	  		 		 
            /*p为链首结点,由于p没有前驱,删除后p的后继结点成为链首,需修改头指针*/
            h = p->next;
        }
        else
        {  	  		 		 
            /*删除的p非链首结点,则p有前驱pre,删除时需将pre后面链接到p的后继结点*/
            pre->next = p->next;
        }
    }

    return h;
}  	  		 		 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杪商柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值