博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链表的创建与反转
阅读量:4216 次
发布时间:2019-05-26

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

#include
using namespace std;/* created by zhangwei 2018/4/24*/struct Node{ int d; Node* nex;};Node *create() { struct Node *head = NULL,*q,*p; int x; while(scanf("%d",&x) == 1 && x!=-1){ Node *p = (struct Node*)malloc(sizeof(Node)); if(!head){ p->d = x; head = q = p; p->nex = NULL; } else{ p->d = x; q->nex = p; q = p; } q->nex = NULL; } return head; }void print(Node * head){ Node *p = head; while(p){ printf("%d ",p->d); p = p->nex; } printf("\n");} Node * reverse(Node *head){ Node *L = head; Node *cur = head,*cur_nex = cur->nex,*front = head; front->nex = NULL; //反转的第一个元素 的nex需要指向为NULL while(cur_nex){ front = cur; cur = cur_nex; cur_nex = cur->nex; cur->nex = front; } return head = cur;}int main() { Node *head = create(); printf("原链表: \n"); print(head); head = reverse(head); printf("反转后的链表:\n"); print(head); return 0; }

转载地址:http://aeimi.baihongyu.com/

你可能感兴趣的文章
Conclusion for Constructors,Destructors,and Assignment Operators
查看>>
Conclusion for Accustoming Yourself to C++
查看>>
面试题1:赋值运算函数(offer)
查看>>
Mark : MessagePack简介及使用
查看>>
Mark : hive文件存储格式
查看>>
mark : hadoop 四种压缩格式
查看>>
All Things OpenTSDB
查看>>
单例模式(singleton),工厂方法模式(factory),门面模式(facade)
查看>>
抽象模式,适配器模式(Adapter),模板方法模式(Template method)
查看>>
建造者模式(builder),桥梁模式(bridge mode),命令模式(Command mode)
查看>>
装饰模式(Decorator),迭代器模式(Iterator),组合模式(composite)
查看>>
观察者模式(Observer),责任链模式,访问者模式(Visitor)
查看>>
状态模式(State)
查看>>
快速排序
查看>>
插入算法
查看>>
希尔排序
查看>>
选择排序
查看>>
归并排序
查看>>
归并排序
查看>>
插入排序进行链表排序
查看>>