填空题
以下程序首先建立一条链表,然后按照如下顺序删除链表中的结点:以链表的第一个结点为1号结点开始依次搜索,删除所有序号为3的倍数的结点,即删除第3、6、9、……个结点, 当搜索一遍结束后再从链表头部继续此操作,直到链表的结点个数少于3个为止a
程序输出为:
当前链表中的结点依次为:23 12 32 54 74 25 65 94 17 72
第1轮删除的结点为:32 25 17
当前链表中的结点依次为:23 12 54 74 65 94 72
第2轮删除的结点为:54 94
当前链表中的结点依次为:23 12 74 65 72
......
第5轮删除的结点为:72
链表中剩余的结点为:23 12
#include
struct node{
int data;
node *next;
};
node *Insert(int x, node *head)
{node *p;
p= new node;
p->data=x;
1(27) 2 ;
return p;
}
void fun(node *head, int n)
{ node *p,*p1,*q;
int i,num=1;
if(!head) return;
while (n>2){
cout<<"当前链表中的结点依次为:";
p=head;
while (p){
coutnext ;
}
cout<第"<轮删除的结点为:";
p=head;
q=head->next ;
i=2 ;
while (q) {
if(i%3==0){
cout3(28) 4;
delete q;
q=p->next ;
n-- ;
i++;
}
else {
p=p->next ;
i++;
5(29) 6;
}
}
num++;
cout<
}
cout<<"链表中剩余的结点为 :";
p=head;
while (p) {
coutnext ;
}
cout<
void main(void)
{
int a[10]={23, 12, 32, 54, 74, 25, 65, 94, 17, 72};
node *head=0;
for(int i=9; i>=0; i--)
7(30) 8;
fun (head, 10) ;
}