#include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList; //尾插法 LinkList List_TailInsert(LinkList &L) { int x; L=(LinkList)malloc(sizeof(LNode)); LNode *s,*r=L; printf("请输入单链表各个节点,以9999结束!\n"); scanf("%d",&x); while(x!=9999) { s=(LNode*)malloc(sizeof(LNode)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=NULL; return L; } //带头结点单链表 删除所有值为 x的并释放其空间 void Del_x(LinkList &L,ElemType x){ LNode *p=L->next,*pre=L,*q; while(p!=NULL) { if(p->data==x){ q=p; p=p->next; pre->next=p; free(q); } else{ pre=p; p=p->next; } } } int main(){ LinkList L; LinkList R;//接收处理结果 R=List_TailInsert(L); int a; printf("请输入要删除的数字\n"); scanf("%d",&a); Del_x(R,a); LNode *p; p=R; while(p->next!=NULL){ p=p->next; printf("->%d",p->data); } }
浙公网安备 33010602011771号