#include "LinkList.cpp"
#include <bits/stdc++.h>
void Delfirstx(LinkNode *&L,int x) {
if (L==NULL)
return;
if (L->data==x) {
LinkNode *p=L;
L=L->next;
free(p);
} else
Delfirstx(L->next,x);
}
int main() {
int a[]= {1,2,5,2,3,2};
int n=sizeof(a) / sizeof(a[0]);
LinkNode *L;
L = CreateList(a,n);
cout << "删除前:";
DispList(L);
cout << endl;
int x = 2;
Delfirstx(L,x);
cout << "删除后:";
DispList(L);
Release(L);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef struct Node {
int data;
struct Node *next;
} LinkNode;
LinkNode *CreateList(int a[],int n) {
if(n < 0)
return NULL;
LinkNode *head = (LinkNode *)malloc(sizeof(LinkNode));
LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
p = head;
head->data = a[0];
int i = 0;
for(i = 1; i < n; i++) {
LinkNode *node = (LinkNode *)malloc(sizeof(LinkNode));
node->data = a[i];
p->next = node;
p = node;
}
p->next = NULL;
return head;
}
void DispList(LinkNode *ln) {
cout << " ";
if(ln != NULL){
cout << ln->data;
DispList(ln->next);
}
}
void Release(LinkNode *ln) {
if(ln->next == NULL)
return ;
else {
Release(ln->next);
free(ln);
}
}