北京杜宾最好的犬舍:怎么将Node Type 的状态由 Broadcast 变为 None ?

来源:百度文库 编辑:神马品牌网 时间:2024/05/09 06:46:21
谢谢

题目有点烂。 head没有任何值的时候就使用它是不合理的,所以我改了一下,
把 Node * head; 改成了Node *head=new Node; 在VC下,如果 head不指向任何值,把它做为参数是没有意义的。

#include <iostream.h>
typedef struct node
{
int no;
struct node *next;
}Node; /*把node改成了Node*/

void create(Node *h){
int i;
static Node *current;
h->no=1;
h->next=0;
current=h;
for(i=2;i<10;i++){
Node * temp= new Node;
temp->no=i;
temp->next=0;
current=current->next=temp;
}
}

int len(Node *h){
int count=0;
Node *current=h;
while(current!=0){
count++;
current=current->next;
}
return count;
}

void disp(Node *h){
Node *current=h;
while(current!=0){
cout<<current->no<<" ";
current=current->next;
}
cout<<endl;
}

void del(Node *h,int i){
Node *temp,*current=h;
if(i<1||i>len(h))
return;
if(--i){
while(--i)
current=current->next;
temp=current->next;
current->next=temp->next;
delete temp;
}
else{
temp=h;
h=h->next;
delete temp;
}
}

void main()
{
Node *head=new Node;
int i;
create(head);
disp(head);
cout<<"链表长度:" <<len(head)<<endl;
cout<<"删除第几个节点:";
cin>>i;
del(head,i);
disp(head);
}