链表这种东西一直让人头疼,今天专门看了看,写了个创建链表和删除链表中某个值得程序,需要的可以看看~~~
#include
#include<stdlib.h>
using namespace std;
struct A
{
int date;
A *next;
};
void main()
{
A A1,A2,A3,A4,*head,*p,*q;
cout << “创建一个链表” << endl;
cin >> A1.date;
cin >> A2.date;
cin >> A3.date;
cin >> A4.date;
head=&A1;
A1.next=&A2;
A2.next=&A3;
A3.next=&A4;
A4.next=NULL;
p=head;
int x;
cout << "请输入需要删除的值" << endl;
cin >> x;
int number=1;
int a=0;
int j;
while(p!=NULL)
{
if(p->date!=x)
{
number=number+1;
p=p->next;
}
else
{
p=p->next;
a++;
cout << "被删除的链表为第"<<number<<"个" << endl;
//删除链表中的有X值的节点
p=head;
int i=1;
j=number-1;
while(i>0&&p->next)
{
if(i==j)
break;
p=p->next;
i++;
}
q=p->next;
p->next=q->next;
number--;
}
}
if(a==0)
{
cout << "该链表中没有和X值相同的元素" << endl;
}
p=head;
cout << "删除后的链表中为:" << endl;
while(p!=NULL)
{
cout << p->date << endl;
p=p->next;
}
system("pause");
delete[]p;
}