迷之1025 反转链表
2016-05-21 16:19
246 查看
错误的代码:
#include <cstdio> #include <iostream> #include <stdlib.h> #include <string.h> #include <string> using namespace std; struct Node { int Nodenow; int Nodenext; int data; Node *next; }; struct store { int now; int next; int data; }a[100005],b[100005]; int turn[100005]; void Node_Print(Node *head) { Node *p = head; while(p != NULL) { if(p -> Nodenext != -1) printf("%05d %d %05d",p->Nodenow,p -> data,p -> Nodenext); else { printf("%05d %d %d",p->Nodenow,p -> data,p -> Nodenext); } p = p -> next; if(p != NULL)printf("\n"); } } Node * ReversePart_Node(Node *head,int start,int end) { if(start == end)return head; Node *pPrv = NULL; Node *pNow = head; Node *pNext = NULL; Node *BeforeStart = NULL; //开始的结点前的结点 Node *StartNode = NULL; //开始的时候的结点 int i,j; //for循环指向需要反转的位置 for(i = 1; i <= start; i++) { if(i == start) { BeforeStart = pPrv; StartNode = pNow; } pPrv = pNow; pNow = pNow -> next; pNext = pNow -> next; } for(i = 1; i <= end - start; i++) { pNext = pNow -> next; //保存下一个 pNow -> next = pPrv; //改变指向 pPrv = pNow; //移动 pPrv和 pNow pNow = pNext; } //反转部分 未反转前的第一个结点 反转之后变成该反转部分的最后一个结点 StartNode -> next = pNow; //连接 if(BeforeStart == NULL) //从head开始反转 { head = pPrv; //pNow 反转过后是 该反转部分的第一个结点 } else { BeforeStart -> next = pPrv; //连接反转部分前面的结点 和 该反转部分 } return head; } int inturn(int ibegin,int stot) { int i,j,found; int tot = 0; int cnt = 1; if(!turn[ibegin]) return 0; b[cnt++] = a[turn[ibegin]]; found = a[turn[ibegin]].next; while(found != -1) { b[cnt++] = a[turn[found]]; found = a[turn[found]].next; } cnt--; return cnt; } Node *Node_Creat(int tot) { Node *head; head = (Node *)malloc(sizeof(Node)); if(head == NULL) { printf("Overflow\n"); exit(1); } head -> data = b[1].data; head -> Nodenow = b[1].now; head -> Nodenext = b[1].next; Node *p1,*p2; p1 = p2 = head; for(int i = 2; i <= tot; i++) { p1 = (Node *)malloc(sizeof(Node)); if(p1 == NULL) { printf("Overflow\n"); exit(1); } p1 -> data = b[i].data; p1 -> Nodenow = b[i].now; p1 -> Nodenext = b[i].next; p2 -> next = p1; p2 = p1; } p2 -> next = NULL; return head; } int main() { int stot,reverse; int ibegin; int i,j; cin >> ibegin >> stot >> reverse; int tot = 0; //结点数 memset(turn,0,sizeof(turn)); for(i = 1; i <= stot; i++) { cin >> a[i].now >> a[i].data >> a[i].next; turn[a[i].now] = i; } tot = inturn(ibegin,stot); Node *head; head = Node_Creat(tot); for(i = 1; i <= tot/reverse; i++) { head = ReversePart_Node(head, (i - 1)*reverse + 1, i*reverse); } head = changedata(head); Node_Print(head); return 0; } /* 00100 12 13 72222 12 -1 62222 11 72222 52222 10 62222 42222 9 52222 32222 8 42222 22222 7 32222 00000 4 99999 00100 1 12309 68237 6 22222 33218 3 00000 99999 5 68237 12309 2 33218 */
后来根据一些数据的对比,发现反转后的结点下一个地址要发生改变,于是添加了一个函数改变结点中地址的值。
Node *changedata(Node *head) { Node *p = head; while(p -> next != NULL) { p -> Nodenext = p -> next -> Nodenow; p = p -> next; } p -> Nodenext = -1; return head; }
改了一下输入方式,但是还是超时(挠头
相关文章推荐
- 音频相关的基本知识
- Python——脚本(calculator)
- Think In Java
- 回到顶部按钮
- Huffman算法
- 动态存储实现通讯录
- UEditor路径配置
- 使用spring4.1 hibernate4.1 struts2.1 Tomcat8 部署项目异常解决方法
- 跟我一起学习MySQL技术内幕(第五版):(第三章学习日记9)
- 常用的Mysql数据库操作语句大全
- 树的直径
- Android ViewPager使用详解
- Axure 图片轮播(广告通栏图片自动播放效果)
- SM2算法第二十六篇:openSSL 中BIGNUM的使用记录
- 电容的等效串联电阻 ESR
- bzoj 1004: [HNOI2008]Cards(置换)
- 2016年华为网络技术精英大赛复赛试题
- 自动完成文本输入框提示,下拉列表,发送状态栏消息,消息提醒
- RESTfulClient
- JVM性能监控与故障处理工具