您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: