- 论坛徽章:
- 0
|
//Link list
struct List
{
int data;
List *next;
};
List *CreateList()
{
List *head = NULL;
List *p = NULL, *q = head;
int d;
while(cin>>d)
{
p = new List();
p->data = d;
p->next = NULL;
if(NULL != head)
{
q->next = p;
q = q->next;
}
else
{
head = p;
q = p;
}
}
return head;
}
void ShowList(List *head)
{
List *tmp = head;
while(tmp != NULL)
{
cout<<tmp->data;
tmp = tmp->next;
}
}
int Length(List *head)
{
List *tmp = head;
int length = 0;
while(NULL != tmp)
{
length++;
tmp = tmp->next;
}
return length;
}
List * DelList(int data, List *head)
{
List *tmp = head;
List *p = tmp;
while(NULL != tmp)
{
if(data == tmp->data)
{
if(head == tmp)
{
head = head->next;
delete tmp;
tmp = NULL;
}
else
{
p->next = tmp->next;
delete tmp;
tmp = NULL;
}
break;
}
p = tmp;
tmp = tmp->next;
}
return head;
}
int main()
{
List *head = CreateList();
ShowList(head);
cout<<"\nthe length of List :"<<Length(head)<<endl;
int data;
cin>>data;
head = DelList(data, head);
ShowList(head);
return 0;
}
为什么ctrl+x之后就全部退出了呢? |
|