建立链表和删除指定节点
题目描述:https://www.nowcoder.com/practice/0ab593ca56b1476eb05b1ff848fd7fcc?tpId=107&tags=&title=&diffculty=0&judgeStatus=0&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking
简单描述:先输入n个整数,按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。输出删除后的单链表信息。
#include <stdio.h> #include <stdlib.h> struct Node{ int data; Node *next; }; int main(){ int n,ele,m,len=0; scanf("%d",&n); Node *head = (Node *)malloc(sizeof(Node)); Node *p,*r; p=head;p->next=NULL; for (int i = 0; i<n; i++){ scanf("%d",&ele); r = (Node *)malloc(sizeof(Node)); r->data = ele; p->next = r; p = r; len++; } r->next = NULL; scanf("%d",&m); p=head;r=p->next; while(r!=NULL){ if (r->data==m) { p->next = r->next; free(r); len--; }else{ p=p->next; } r=p->next; } printf("%d\n",len); p=head->next; while(p!=NULL){ printf("%d ",p->data); p=p->next; } return 0; }