- 论坛徽章:
- 0
|
遇到了一个问题..求大侠指点一下.
目的: 建立一个链表,存储 1-20 数字..然后把它们打印出来.- #include <stdlib.h>
- #include <stdio.h>
- #define MAXLIST 20
- struct node
- {
- int data;
- struct node *next;
- };
- //线性表的初始化
- //顺序表
- i
- int PInitList(void)
- {
- struct node *head = NULL;
- struct node *p = NULL, *q = NULL;
- int i = 0;
- head = (struct node *)malloc(sizeof(struct node));
- head->next = NULL;
- head->data = 100;
-
- q = head;
- printf("%d line,\n head addr : %p , %p ,%d\n",__LINE__, &head, &(head->next), head->data);
- printf("q addr %p\n",&q);
- printf("p addr %p\n",&p);
-
- if (head == NULL)
- {
- printf("malloc error!\n");
- }
- else
- {
- printf("malloc ok!\n");
-
- for (i = 0; i < 20; i++)
- {
- p = (struct node *)malloc(sizeof(struct node));
- if (p != NULL)
- {
- p->data = i;
- p->next = head->next;
- head->next = p;
- }
- printf("i %d p addr %p %d\n",i, &(p->next), p->data);
- }
- }
- int j = 0;
- for (j = 0; j < 21; j++)
- {
-
- printf("head addr : %p , num %d %d\n",&(head->next), j, head->data);
- head = head->next;
-
- }
-
-
- return 0;
- }
- int main(void)
- {
- PInitList();
- return 0;
- }
复制代码 打印的结果是:
39 line,
head addr : 0xbfb0e72c , 0x804a00c ,100
q addr 0xbfb0e724
p addr 0xbfb0e728
malloc ok!
i 0 p addr 0x804a01c 0
i 1 p addr 0x804a02c 1
i 2 p addr 0x804a03c 2
i 3 p addr 0x804a04c 3
i 4 p addr 0x804a05c 4
i 5 p addr 0x804a06c 5
i 6 p addr 0x804a07c 6
i 7 p addr 0x804a08c 7
i 8 p addr 0x804a09c 8
i 9 p addr 0x804a0ac 9
i 10 p addr 0x804a0bc 10
i 11 p addr 0x804a0cc 11
i 12 p addr 0x804a0dc 12
i 13 p addr 0x804a0ec 13
i 14 p addr 0x804a0fc 14
i 15 p addr 0x804a10c 15
i 16 p addr 0x804a11c 16
i 17 p addr 0x804a12c 17
i 18 p addr 0x804a13c 18
i 19 p addr 0x804a14c 19
head addr : 0x804a00c , num 0 100
head addr : 0x804a14c , num 1 19
head addr : 0x804a13c , num 2 18
head addr : 0x804a12c , num 3 17
head addr : 0x804a11c , num 4 16
head addr : 0x804a10c , num 5 15
head addr : 0x804a0fc , num 6 14
head addr : 0x804a0ec , num 7 13
head addr : 0x804a0dc , num 8 12
head addr : 0x804a0cc , num 9 11
head addr : 0x804a0bc , num 10 10
head addr : 0x804a0ac , num 11 9
head addr : 0x804a09c , num 12 8
head addr : 0x804a08c , num 13 7
head addr : 0x804a07c , num 14 6
head addr : 0x804a06c , num 15 5
head addr : 0x804a05c , num 16 4
head addr : 0x804a04c , num 17 3
head addr : 0x804a03c , num 18 2
head addr : 0x804a02c , num 19 1
head addr : 0x804a01c , num 20 0
现在问题是:
打印出来的是结果是倒序的:
而我希望是打印出来的.是0-20...
还请大侠指点一下..谢谢~~ |
|