有头单链表的创建 ,及有头单链表逆转
来源:互联网 发布:资源商城源码 编辑:程序博客网 时间:2024/06/08 05:15
有头头插单链表:
#include <stdio.h>#include <stdlib.h>typedef struct person{ int age; struct person *next;}per;per *head_list(per *one, int num){ per *temp = (per *)malloc(sizeof(per)); temp->age = num; per *head = one->next; one->next = temp; temp->next = head; return head;}void show(per *head){ if(NULL == head->next) { return; } while(head->next) { printf("age is %d\n",head->next->age); head = head->next; }}int main(){ per head; head.next = NULL; head_list(&head,10); head_list(&head,20); head_list(&head,30); head_list(&head,40); head_list(&head,50); show(&head); return 0;}有头尾插单链表:
#include <stdio.h>#include <stdlib.h>typedef struct person{ int age; struct person *next;}per;per *tail_list(per *one, int num){ per *temp = (per *)malloc(sizeof(per)); temp->age = num; per *head = one->next; while(one->next) { one = one->next; } one->next = temp; return head;}void show(per *head){ if(NULL == head) { return; } while(head->next) { printf("age is %d\n",head->next->age); head = head->next; }}int main(){ per head; head.next = NULL; tail_list(&head,10); tail_list(&head,20); tail_list(&head,30); tail_list(&head,40); tail_list(&head,50); show(&head); return 0;}
有头头插单链表逆转:
#include <stdio.h>#include <stdlib.h>typedef struct person{ int age; struct person *next;}per;per *head_list(per *one, int num){ per *temp = (per *)malloc(sizeof(per)); temp->age = num; per *head = one->next; one->next = temp; temp->next = head; return temp;}per *reverse(per *head){ if(NULL == head->next) { return NULL; } per *p, *q, *r; p = head->next; q = p->next; r = q->next; head->next->next = NULL; while(r) { q->next = p; p = q; q = r; r = r->next; } q->next = p; head->next = q; return head;}void show(per *head){ if(NULL == head->next) { return; } while(head->next) { printf("aeg is %d\n",head->next->age); head = head->next; }}int main(){ per head; head.next = NULL; head_list(&head,10); head_list(&head,20); head_list(&head,30); head_list(&head,40); head_list(&head,50); show(&head); printf("===============\n"); reverse(&head); show(&head); return 0;}
有头尾插单链表逆转:
#include <stdio.h>#include <stdlib.h>typedef struct person{ int age; struct person *next;}per;per *tail_list(per *one, int num){ per *temp = (per *)malloc(sizeof(per)); temp->age = num; per *head = one->next; while(one->next) { one = one->next; } one->next = temp; return head;}per *reverse(per *head){ if(NULL == head->next) { return; } per *p, *q, *r; p = head->next; q = p->next; r = q->next; head->next->next = NULL; while(r) { q->next = p; p = q; q = r; r = r->next; } q->next = p; head->next = q; return head;}void show(per *head){ if(NULL == head->next) { return; } while(head->next) { printf("age is %d\n",head->next->age); head = head->next; }}int main(){ per head; head.next = NULL; tail_list(&head,10); tail_list(&head,20); tail_list(&head,30); tail_list(&head,40); tail_list(&head,50); show(&head); printf("=====================\n"); reverse(&head); show(&head); return 0;}
阅读全文
0 0
- 有头单链表的创建 ,及有头单链表逆转
- 无头单链表的创建,及无头单链表逆转
- 单链表的创建和逆转
- 链表的输入输出及逆转
- 单向链表的创建和逆转(完整程序)
- 单链表的创建,插入,删除以及就地逆转
- 单链表的创建、遍历、插入、删除、查找、逆转
- 单链表创建、输出、逆转、查询
- 逆转的街角
- 链表的逆转
- 单链表的逆转
- 苛刻的字符串逆转
- 单链表的逆转
- 单链表的逆转
- 链表的逆转
- 单链表的就地逆转
- 单链表的逆转
- 单链表的逆转实现
- 插入排序(直接插入排序和希尔排序)
- 三大框架Spring+Mybatis+springMVC整合
- iOS开发之同一应用设置不同图标和名称
- BASE64加密解密
- 复习总结06:Kafka
- 有头单链表的创建 ,及有头单链表逆转
- tomcat启动错误:Error running tomcat: Address localhost:1099 is already in use
- JavaScript集合对象遍历方法总结
- yii文件上传
- 恶意代码分析实战 安装INetSim在CentOS 7上
- LuoguP1063 能量项链
- SQL笔记:基本操作
- POJ 1836 Alignment
- springboot 学习