输入n个元素的数组,把该数组建成链表,删除链表中的指定元素
输入:
n
n个元素
x指定元素
#include<iostream>
#define MAXSIZE 100
using namespace std;
typedef struct Lnode {
int data;
Lnode* next;
}linknode;
void Delete(linknode* &L, int x) {
linknode* q;
q = L->next;
/*while (q->data == x) {
q = q->next;
}*/
//L = q;
// q = q->next;
linknode* m = L;
while (q != NULL) {
if (x == q->data) {
m->next = q->next;
q = q->next;
}
else{
m=q;
q = q->next;
}
}
}
int main()
{
int n;
while (cin >> n)
{
if (n == 0) break;
linknode* L = (linknode*)malloc(sizeof(linknode));
linknode* q = L;
while (n--) {
linknode *p = (linknode*)malloc(sizeof(linknode));
cin >> p->data;
q->next=p;
q=p;
p = p->next;
}
q->next = NULL;
int x;
cin >> x;
Delete(L, x);
linknode *p = (linknode*)malloc(sizeof(linknode));
p=L->next;
while(p!=NULL){
cout << p->data<<" ";
p = p->next;
}
}
return 0;
}