1025反转链表
2016-05-30 17:09
363 查看
两个错误不知道怎么改了= =希望有人解答
提交情况
代码
#include<iostream> #include<malloc.h> #include<iomanip> #pragma warning(disable:4996) using namespace std; struct Node { int address; int data; int Next; struct Node * next; }; Node * GetNode(int item, int address, int Next) { Node* newNode = (Node *)malloc(sizeof(Node)); if (newNode == NULL) { printf("overflow!"); exit(1); } newNode->Next = Next; newNode->address = address; newNode->data = item; newNode->next = NULL; return (newNode); } void InsertAfter(Node *p, Node* nextptr) { nextptr->next = p->next; p->next = nextptr; } Node* DeleteAfter(Node *p) { Node *t = p->next; if (t->next != NULL) { p->next = t->next; } else { p->next = NULL; } return (t); } int a[100000005][2]; int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int hAdd, N, k, i, add, begin = 0; Node *head = (Node *)malloc(sizeof(Node)); Node *p = (Node *)malloc(sizeof(Node)); Node *del = (Node *)malloc(sizeof(Node)); Node *rear = (Node *)malloc(sizeof(Node)); cin >> hAdd >> N >> k; for (i = 0; i < N; i++) { cin >> add; cin >> a[add][1] >> a[add][0]; if (add == hAdd) { head->next = GetNode(a[add][1], add, a[add][0]); begin = a[add][0]; } } p = head; i = begin; while (a[i][0] != -1) { p = p->next; p->next = GetNode(a[i][1], i, a[i][0]); i = a[i][0]; } p = p->next; p->next = GetNode(a[i][1], i, a[i][0]); p = head; rear = p->next; while (N >= k) { N = N - k; for (i = 0; i < k - 1; i++) { del = DeleteAfter(rear); InsertAfter(p, del); } p = rear; rear = p->next; } for (p = head->next; p != NULL; p = p->next) { if (p->next != NULL) { p->Next = p->next->address; } else { p->Next = -1; } if (p->Next == -1) { cout << setw(5) << setfill('0') << p->address << " " << p->data << " " << p->Next << endl; } else { cout << setw(5) << setfill('0') << p->address << " " << p->data << " " << setw(5) << setfill('0') << p->Next << endl; } } free(head); free(p); free(del); free(rear); return 0; }
相关文章推荐
- 使用Python写一个小小的项目监控
- [置顶] 图片压缩到指定大小范围内KB、MB
- ORM, ormlite和sqlite
- JQuery中的基本筛选选择器
- n的阶乘末尾含0的个数
- hdu1548 A strange lift(bfs)
- db2常用函数(1)
- 再谈json
- 第二冲刺阶段第一天
- powerdesigner导出sql
- 春哥来了
- POJ 3181 Dollar Dayz
- Leetcode 204. Count Primes
- Java 性能优化
- 强悍的 Linux —— 网络
- 简单Git服务器搭建
- OPatch Version: 11.2.0.3.13版本与OPatch Version: 11.2.0.3.10版本升级补丁区别
- 【BZOJ4145】[AMPPZ2014]The Prices【状压DP】【背包】
- Android的IPC机制(二)——AIDL实现原理简析
- 【Android Studio快捷键】之导入相应包声明(import packages)