博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
带头结点链表逆序实现
阅读量:6941 次
发布时间:2019-06-27

本文共 898 字,大约阅读时间需要 2 分钟。

#include<stdlib.h>

#include<stdio.h>
typedef struct list
 int data;
 struct list*next;
} List;
List* insert_list_last();
List*reverse(List*head);
int main()
{ List*head;
 List*p;
 head=insert_list_last();
 p=head->next;
 while(p) 
 {
  printf("%d\n",p->data);
  p=p->next;
 }
 printf("affter reverse\n");
 head=reverse(head);
 p=head;
 while(p->next)
 {
  printf("%d\n",p->data);
  p=p->next; 
 }
 
}
List* insert_list_last()
{ int i;
 List*head=(List*)malloc(sizeof(List));
 List*newnode,*tail;
 head->next=NULL;
 tail=head;
 for(i=0;i<10;i++)
 {
  newnode=(List*)malloc(sizeof(List));
  newnode->data=i;
  newnode->next=NULL;
  tail->next=newnode;
  tail=newnode;  
 }
 return head;
}
List*reverse(List*head)
{ List*p,*q,*r;
 if(head==NULL) return NULL;
 p=head;
 q=head->next;
 if(q==NULL) return head;
 while(q)
 {
  r=q->next;
  q->next=p;
  p=q;
  q=r;
 }
 head->next=NULL;
 return p;
}

转载于:https://www.cnblogs.com/ITfeng/archive/2012/04/26/2472118.html

你可能感兴趣的文章
Python中HTTPS连接
查看>>
基于epoll的简单的httpserver
查看>>
[Java] 简化正则表达式的使用
查看>>
关于echarts的那些事(地图标点,折线图,饼图)
查看>>
ExecutorService 的理解与使用
查看>>
[Javascript Crocks] Flatten Nested Maybes with `chain`
查看>>
【转载并记录】SpringBoot 入门(一)
查看>>
我的第一个python web开发框架(30)——定制ORM(六)
查看>>
performselectoronmainthread
查看>>
产品经理必须要了解的26个文档
查看>>
grep的两个替代品(补充?)
查看>>
HDOJ---1068 Girls and Boys[匈牙利算法]
查看>>
POJ-2528 Mayor's posters (点树+离散) 线段树 ----------------------转
查看>>
Create a Git Mirror (for your hg repository) / hg tip
查看>>
[转]sql server 存储过程中变量表与临时表的分析比较
查看>>
无法启动程序“http://localhost:3303/Default.aspx”
查看>>
SQL 把字符创分割成两个字符串
查看>>
java使用JNDI 获取weblogic配置的数据源 连接数据库
查看>>
大年初七回杭州
查看>>
Java--选择排序,冒泡排序
查看>>