#include <iostream>
#include <conio.h>
using namespace std;
struct Node
{
int value;
Node* next;
};
void creatlist(Node **head)
{
int i;
(*head)=new Node;
(*head)->next=NULL;
cout<<"请输入数字:";
while(cin>>i) //按Ctrl+D结束输入
{
Node* p=new Node;
p->value=i;
p->next=(*head)->next;
(*head)->next=p;
}
cin.clear(); //清除错误标记
cin.seekg(0);
}
int print(Node *head)
{
Node *p=head;
p=p->next;
while(p)
{
cout<<p->value<<endl;
p=p->next;
}
return 0;
}
void del(Node **head)
{
int i;
Node* q;
Node* p=*head;
if(*head == NULL)
return;
cout<<"删除一个元素:";
cin>>i;
if(p->value == i)
{
*head=p->next;
delete p;
return;
}
while(p->next && p->next->value!=i)
{
p=p->next;
}
if(p->next)
{
q=p->next;
p->next=q->next;
delete q;
}
}
int main()
{
Node *head;
creatlist(&head);
printf("Old Array:/n");
print(head);
del(&head);
printf("/nNew Array:/n");
print(head);
cout<<"Press any key to exit";
getch();
return 0;
}