• 单链表的逆序


    #include <stdio.h>
    #include <stdlib.h>
    //单链表的逆序使用三个辅助指针
    typedef struct Node
    {
    	int data;
    	struct Node *next;
    }Node;
    //从尾部添加节点
    void AppendtoEnd(Node *head, int data)
    {
    	Node *new_node=NULL;
    	Node *temp=NULL;
    
    	new_node=malloc(sizeof(Node));
    	new_node->data=data;
    	new_node->next=NULL;
    
    	//找到最后一个节点
    	temp=head;
    	for(;temp->next!=NULL;temp=temp->next);
    	temp->next=new_node;
    }
    
    //打印链表
    void ShowList(Node *head)
    {
    	Node* temp=NULL;
    	if(head==NULL)
    	{
    		return ;
    	}
    	temp=head->next;
    	for(;temp!=NULL;temp=temp->next) 
    	{
    		printf("%d",temp->data);
    		printf("-->");
    	}
    	printf("
    ");
    
    }
    
    //单链表的逆序
    Node* ReverseList(Node *head)
    {
    	Node* frist=NULL;
    	Node* second=NULL;
    	Node* third=NULL;
    	if(head==NULL||head->next==NULL)
    	{
    		return head;
    	}
    	frist=head->next;
    	second=frist->next;
    	third=second->next;
    	frist->next=NULL;
    	while(third!=NULL)
    	{
    		second->next=frist;
    
    		frist=second;
    		second=third;
    		third=third->next;
    	}
    	second->next=frist;
    	head->next=second;
    	return head;
    }
    
    int main()
    {
    	int i=0;
    	Node *head=malloc(sizeof(Node));
    	Node *newhead=NULL;
    	head->next=NULL;
    	for(i=1;i<11;i++) 
    	{
    		AppendtoEnd(head, i);
    	}
    	ShowList(head);
    	newhead=ReverseList(head);
    	ShowList(newhead);
    	free(head);
    	return 0;
    }
    

      

  • 相关阅读:
    用 Java 爬美女图片,厉害了。。
    Java-Stream流方法学习及总结
    Swagger3 更新配置详解
    一、MySQL下载和安装
    hack(兼容IE及浏览器常用的写法)
    css常用单位
    Ps使用 和精灵图的制作
    HTML5
    JS面向对象
    seajs与requirejs
  • 原文地址:https://www.cnblogs.com/jueshi0208/p/5546119.html
Copyright © 2020-2023  润新知