坐位问题的 数组和指针 实现

 

指针实现方法 Code

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 
 5 #define LEN 1000
 6 #define SIZE sizeof(struct node)
 7 
 8 struct node
 9 {
10     int data;
11     node* next;
12 };
13 
14 int main()
15 {
16     int i;
17     struct node *head;
18     struct node *p;
19     head = (struct node *)malloc(SIZE);
20     head->data = 0;
21     head->next = NULL;
22     p = head;
23 
24     for (i=1; i<LEN;i++)
25     {
26         struct node *ptmp;
27         ptmp = (struct node *)malloc(SIZE);
28         p->next = ptmp;
29         ptmp->data = i;
30         ptmp->next = NULL;
31         p = ptmp;
32     }
33     
34     p->next = head;
35 
36     p = head;
37 
38     while(p != p->next) // 直到只有一个节点为止
39     {
40         p->next->next = p->next->next->next;
41         p = p->next->next;
42     }
43 
44     printf("%d\n",p->data);
45     
46 
47     return 0;
48 }

 

posted @ 2012-04-14 15:57  蘑菇主义  阅读(103)  评论(0)    收藏  举报