单向链表 循环链表
选首领。N个人为成一圈,从第一个人开始顺序报号1,2,3,。凡报道3着退出圈子,最后留在圈子里的人即为首领。
此问题可建一个循环链表,凡是到三的倍数时,将此节点删除然后,将计数改为0,然后再次往后执行,知道总数==1时停止,并输出该人标记(名字或编号)。
程序如下:
#include<stdio.h>
#include<stdlib.h>
#define N 13
typedef struct NODE{
short code;
struct NODE *next;
}CLIST;
void outPut(CLIST *head)
{
CLIST *p;
printf("\n");
p = head;
do{
printf("%4d \n",p->code);
p = p->next;
}while(p!=head);
}
CLIST *create()
{
CLIST *head,*p;
int i;
head = (CLIST*) malloc(sizeof(CLIST));
if(!head)
{
printf("memory error!")
exit(1);
}
head->code = 1;
head->next = head; //循环链表的区别
for(i = 1;i<=N;i++)
{
p=(CLIST*)malloc(sizeof(CLIST));
if(!p)
{
printf("meory error!\n");
exit(1);
}
p->next = head->next;
head->next = p;
}
return head;
}
void main()
{
CLIST *head, *p, *q;
int c = 0,k = N;
head = create();
outPut(head);
printf("\n The outPut sequence:");
p = head; c=1;
while(k>1)
{
if(c==2) //当C==2时,p指针所指节点即为要删节点
{
q=p->next;
printf("%d ",q->code);
p->next = q->next; free(q); //删除结点
c=0; k--;
}
else
{
c++;
p = p->next;
}
}
printf("\n%4d was the header.",p->code); //输出最后留在圈子里的人编号
}
某公司类型面试题:
50 people are standing in a circle, the are counting one by one, the person who gets 3 or a multiple(倍数) of 3 should get out. For example, A counted 4, next B counted 5, then C counted 6 and C should get out. For others, they need countinue . Please use any language to write a program to calculate(计算)who will be last and what's the original position for him?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
上面建表为插入过程,即有个头结点后一次向中间插入建表,除第一个结点外,其他为逆序,接下来是一种依次建表过程,即和你建表出入的次序是一样的。代码如:
#include<iostream>
using namespace std;
typedef struct LNode{
int data;
LNode *next;
}*listLink;
/*void initList()
{
Lode L= new LNode;
L.next = NULL;
}*/
void outputList(LNode *head);
LNode *createList(listLink &L,int n)
{
LNode *head=NULL;
L= new LNode;
L->next = NULL;
//initList();
LNode *p;
for(int i=0;i<n;i++)
{
cout<<"plese input a number :"<<endl;
p=new LNode;
cin>>p->data;
if(NULL==head)
{
head=p;
L=p;
} //明确链表结构,一个指向一个,
else
{
L->next= p;
L=p; //无此项则只输出第一和最后一个节点
}
}
p->next=NULL;
return head;
}
void outputList(LNode *head) //head 形参传递
{
LNode *L;
L=head;
while(L!=NULL)
{
cout<<L->data<<endl;
L=L->next;
}
cout<<endl;
}
int main()
{
LNode *L1;
L1=new LNode;
int n;
cout<<"plese putin the length:"<<endl;
cin>>n; //表常 问什么大于1
LNode *head=createList(L1,n);
outputList(head);
cout<<"链表成功"<<endl;
return 0;
}
#include<iostream>
using namespace std;
typedef struct LNode{
int data;
LNode *next;
}*listLink;
/*void initList()
{
Lode L= new LNode;
L.next = NULL;
}*/
void outputList(LNode *head);
LNode *createList(listLink &L,int n)
{
LNode *head=NULL;
L= new LNode;
L->next = NULL;
//initList();
LNode *p;
for(int i=0;i<n;i++)
{
cout<<"plese input a number :"<<endl;
p=new LNode;
cin>>p->data;
if(NULL==head)
{
head=p;
L=p;
} //明确链表结构,一个指向一个,
else
{
L->next= p;
L=p; //无此项则只输出第一和最后一个节点
}
}
p->next=NULL;
return head;
}
void outputList(LNode *head) //head 形参传递
{
LNode *L;
L=head;
while(L!=NULL)
{
cout<<L->data<<endl;
L=L->next;
}
cout<<endl;
}
int main()
{
LNode *L1;
L1=new LNode;
int n;
cout<<"plese putin the length:"<<endl;
cin>>n; //表常 问什么大于1
LNode *head=createList(L1,n);
outputList(head);
cout<<"链表成功"<<endl;
return 0;
}