循环链表
时间:2010-08-31 来源:chengbin_liu
/* ************************************************************************
* Filename: loopLink.c
* Description:
* Version: 1.0
* Created: 08/31/2010 08:18:12 PM
* Revision: none
* Compiler: gcc
* Author: chengbin_liu,
* Company:
* ************************************************************************/ #include <unistd.h>
#include <stdlib.h>
#include <stdio.h> typedef struct node
{
int data;
struct node *next;
}node;
node *node_create(int n)
{
node *pRet=NULL;
if(0!=n)
{
int n_idx=1;
node *p_node=NULL;
p_node=new node[n];
if(NULL==p_node)
{
return NULL;
}
else
{
memset(p_node,0,n*sizeof(node));
}
pRet=p_node;
while(n_idx<n)
{
p_node->data=n_idx;
p_node->next=p_node+1;
p_node=p_node->next;
n_idx++;
}
p_node->data=n;
p_node->next=pRet;
}
return pRet;
}
int main()
{
int n=20;
int m=6;
node *pList=NULL;
node *pIter=NULL;
pList=node_create(n);
pIter=pList;
m%=n;
while(pIter !=pIter->next)
{
int i=1;
for(;i<m-1;i++)
{
pIter=pIter->next; }
printf("%d",pIter->next->data);
pIter->next=pIter->next->next;
pIter=pIter->next;
}
printf("%d\n",pIter->data);
delete[] pList;
return 0;
}
* Filename: loopLink.c
* Description:
* Version: 1.0
* Created: 08/31/2010 08:18:12 PM
* Revision: none
* Compiler: gcc
* Author: chengbin_liu,
* Company:
* ************************************************************************/ #include <unistd.h>
#include <stdlib.h>
#include <stdio.h> typedef struct node
{
int data;
struct node *next;
}node;
node *node_create(int n)
{
node *pRet=NULL;
if(0!=n)
{
int n_idx=1;
node *p_node=NULL;
p_node=new node[n];
if(NULL==p_node)
{
return NULL;
}
else
{
memset(p_node,0,n*sizeof(node));
}
pRet=p_node;
while(n_idx<n)
{
p_node->data=n_idx;
p_node->next=p_node+1;
p_node=p_node->next;
n_idx++;
}
p_node->data=n;
p_node->next=pRet;
}
return pRet;
}
int main()
{
int n=20;
int m=6;
node *pList=NULL;
node *pIter=NULL;
pList=node_create(n);
pIter=pList;
m%=n;
while(pIter !=pIter->next)
{
int i=1;
for(;i<m-1;i++)
{
pIter=pIter->next; }
printf("%d",pIter->next->data);
pIter->next=pIter->next->next;
pIter=pIter->next;
}
printf("%d\n",pIter->data);
delete[] pList;
return 0;
}
相关阅读 更多 +