C语言数据结构

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值