zoukankan      html  css  js  c++  java
  • 单链表的逆序

    #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;
    }
    

      

  • 相关阅读:
    Revit 二次开发参照属性
    存储过程分页 (多条件拼接)
    Dapper的使用
    C#实现简单的邮件发送
    ORM 简介
    Web Services简介
    事物、锁、存储过程
    游标和触发器简介
    ASP.NET 上传文件方法
    C# Web API 实现上传功能
  • 原文地址:https://www.cnblogs.com/jueshi0208/p/5546119.html
Copyright © 2011-2022 走看看