单链表删除元素

单链表删除元素

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#define OVERFLOW 0
#define OK 1

typedef struct student
{
    int num;
    struct student *next;
}node,*slist;

node* creat(int n)
{
    int i,a,t;
    slist h,p,q;
    h = NULL;
    printf("please input the integers:\n");
    for(i=0; i<n; i++)
    {
        scanf("%d", &a);
        p = (slist)malloc(sizeof(node));
        p->next = NULL;
        p->num = a;
        if(h == NULL)
        {
            h = p;
            q = p;
        }
        else
        {
            q->next = p;
            q = p;
        }
    }
    return h;
}

slist delnode(slist p, int key)
{
    slist q , s;
    q = p;
    while(q->next->num != key && q != NULL
          )
    {
        q = q->next;
    }
    s = q->next;
    q->next = q->next->next;
    free(s);
    return  p;
}


int main()
{
    int key,n,i;
    node* h;
    printf("input the counts:\n");
    scanf("%d", &n);

    h = creat(n);
    printf("%d",h->num);
    printf("the original data are:\n");
    slist t = h;
    while(t != NULL)
    {
        printf("%d ", t->num);
        t = t->next;
    }

    h = delnode(h,5);
    printf("\nafter delete these data are:\n");
    t = h;
    while(t != NULL)
    {
        printf("%d ", t->num);
        t = t->next;
    }
    return 0;
}

在这里插入图片描述

发布了29 篇原创文章 · 获赞 5 · 访问量 670

猜你喜欢

转载自blog.csdn.net/ever_promise/article/details/104362610