lzhenf

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#include <stdio.h>

typedef struct node
{
	int val;
	node* next;
};

node* reverse(node* list , node* &head)
{
	if ( !list || !list->next )
	{
		head->next = NULL;
		head = list;
		return list;
	}
	else
	{
		node* temp = reverse( list->next , head);
		temp->next = list;
		return list;
	}
}

node* nonreverse(node* head)
{
	node* p1,*p2,*temp;
	p1 = head;
	if (p1)
		p2 = p1->next ;
	while ( p2)
	{
		temp = p2->next ;
		p2->next = p1;
		p1 = p2;
		p2 = temp;
	}
	head->next = NULL;
	head = p1;
	return head;
	
}
node* create(int n )
{
	int i ; 
	node* head ;
	node* temp ,*pre;
	head = NULL;
	
	head = new node;
	pre = head;
	while ( n --)
	{
		temp = new node;
		scanf("%d" , &temp->val);
		pre ->next= temp ;
		pre = temp;
	}
	pre->next = NULL;
	return head->next;
}
int main()
{
	node* list1,*list2;
	list1 = create(5);
	//list2= reverse(list1, list1);
	list2 = nonreverse(list1);
	return 0 ;
}

  

posted on 2012-03-19 23:22  lzhenf  阅读(3687)  评论(0编辑  收藏  举报