指针和链表

#include "stdafx.h"
#include "stdio.h"
#include "string.h"

#define count 13
struct baby
{
 int position; //定义小伙伴的位置
 struct baby *next;
};

int _tmain(int argc, _TCHAR* argv[])
{
    struct baby gamer[count];

 //初始化小伙伴  组成一个循环链表
 for (int i=0;i<count-1;i++)
 {
  gamer[i].position=i+1;
  gamer[i].next=&gamer[i+1];
 }
 gamer[count-1].position=count;
 gamer[count-1].next=&gamer[0];

 //定义一个循环结构体指针
 struct baby *first;
 struct baby *temp;

 first=&gamer[0];

 while(first->next!=first)
 {
  temp=first;
  for (int i=1;i<6;i++)
  {
   temp=temp->next;
  }
  first=temp->next->next;
  temp->next=first;
 }
 printf("%d  ",first->position);
    printf("\n");
 //验证一个指针赋值区别
 int a[]={31,22,13,42,59};
 int *p;//定义一个指针变量  p只能赋地址
 p=a;
 for (int i=0;i<5;i++)
 {
  printf("%d  ",*(p+i));//*(p+i)=*(a+i)=a[i]=*p++
 }
 printf("\n");
 /*for (int i=0;i<5;i++)
 {
  printf("%d  ",*p+i);  //此方法表示错误 *p+i=*p的值   +    i的值
 }*/
 getchar();
 return 0;
}

posted @ 2013-08-21 10:46  露水上的青蛙  阅读(191)  评论(0编辑  收藏  举报