筛选号码(c语言版)

2025-05-12 09:06:51

1、#include<stdio.h>#include <衡痕贤伎stdlib.h>typedef struct node{艘早祓胂 struct node *next; int data;}LinkList;LinkList *CreatListH(int n){ LinkList *L,*S,*head; int num=n-1; L=(LinkList*)malloc(sizeof(LinkList)); head=L; head->data=n; L->next=head; while(num>=1) { S=(LinkList*)malloc(sizeof(LinkList)); S->data=num; S->next=L->next; L->next=S; num--; } return head;}LinkList *Count(LinkList *head){ LinkList *p,*u; int count=0; p=head; while(p->next!=p) { count++; p=p->next; if(count==2) { u=p->next; p->next=u->next; free(u); count=0; } } printf("%d\n",p->data);}int main(){ LinkList *head; int n; scanf("%d",&n); head=CreatListH(n); Count(head); return 0;}

筛选号码(c语言版)

2、本题采用链表遍历的方法,根据题意每遍历3个,则删除第三个结点的数据。删除到最后所剩的一个即为所求,链表采用单向循环链表。while(p->next!=p){count++;p=p->next;if(count==2)//此处删除的是p->next,所以是2{u=p->next;p->next=u->next;free(u);

3、此外采用头插法创建链表,为符合题目意思,在链表中存储数据时,数据逆序存储,如果采用尾插法,则顺序存储即可。LinkList *CreatListH(int n){LinkList *L,*S,*head;int num=n-1;L=(LinkList*)malloc(sizeof(LinkList));head=L;//所有节点都存储数据head->data=n;L->next=head;while(num>=1){S=(LinkList*)malloc(sizeof(LinkList));S->data=num;S->next=L->next;L->next=S;num--;//逆序存储}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢