#include <stdio.h>
#include <stdlib.h>
struct abc
{
int a;
char b;
struct abc *next;
};
void fun1(struct abc *myabc)
{
printf("%d\n",myabc->a );
}
void fun2(struct abc myabc)
{
printf("%d\n",myabc.a );
}
struct abc *new_node(int data)
{
struct abc *node=(struct abc *)malloc(sizeof(struct abc));
if (node != NULL)
{
node->a=data;
node->next=NULL;
// printf("%d\n",node->a );
}
return node;
}
void insert(struct abc *head,int data)
{
struct abc *node=new_node(data);
if (node != NULL)
{
node->next=head->next;
head->next=node;
}
}
void display(struct abc *p){
int i;
// printf("%d\n",p->next->next->a);
while(p->next){
printf("%d;", p->next->a);
p=p->next;
}
printf("\n");
}
void reverse(struct abc *p)
{
struct abc *current,*pnext,*prev;
current=p->next;
pnext=current->next;
current->next=NULL;
int i=0;
while(pnext)
{
/*current->next=pnext->next;
p->next=pnext;
pnext->next=current;*/
// printf("%d;", current->a);
// printf("%d;", pnext->a);
prev = pnext->next;
// printf("%d;", prev->a);
pnext->next = current;
current = pnext;
// printf("%d;", current->a);
pnext = prev;
// printf("%d;", pnext->a);
i++;
// printf("\n");
// display(current);
// 交换之后,notice:pnext!=current->next,注意每个指针所对应的结构体和对应next的是哪个指针地址,不要被名字搞混了
printf("%d->%d\n",current->a,current->next->a );
/*
4->5
3->4
2->3
1->2
*/
}
printf("遍历了%d次\n", i);
p->next=current;
}
int main(int argc, char const *argv[])
{
struct abc myabc;
printf("%d\n",myabc );
printf("%d\n",myabc.a );//输出值和上面的一样,如果是char b放第一个就不是了
printf("%c\n",myabc.b );//没定义就空
myabc.a=1;
fun1(&myabc);
fun2(myabc);
struct abc *p=(struct abc *)malloc(sizeof(struct abc));
p->b='c';
printf("%d ; %c\n",(*p).a,p->b);//新的一个指针,没定义a,所以0
free(p);
struct abc *q=(struct abc *)malloc(sizeof(struct abc));
int i;
for (i = 0; i < 5; ++i)
{
insert(q,i+1);
}
display(q);
reverse(q);
display(q);
return 0;
}
扩展阅读:
http://www.nowamagic.net/librarys/veda/detail/2241
http://www.oschina.net/code/snippet_105637_43706
http://www.jb51.net/article/51960.htm