您的位置:首页 > 编程语言 > C语言/C++

C语言 02-线性结构3 Reversing Linked List

2018-03-24 14:44 423 查看
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and
NULL
is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next


where
Address
is the position of the node,
Data
is an integer, and
Next
is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 6 4

00000 4 99999

00100 1 12309

68237 6 -1

33218 3 00000

99999 5 68237

12309 2 33218


Sample Output:

00000 4 33218

33218 3 12309

12309 2 00100

00100 1 99999

99999 5 68237

68237 6 -1


show me the code:

#include<stdio.h>

#include<stdlib.h>

#ifndef NULL

#define NULL 0

#endif // NULL

#define MAXSIZE 100000

typedef struct Node *PtrToNode;

struct Node

{

int Address;

int Data;

int Next;

struct Node *Next_id;

};

typedef PtrToNode List;

List CreateList(int N, int FirstAddr);

void ListReverse(List L, int K);

PtrToNode Reverse(PtrToNode Head, int K);

void printList(List L);

List SortList(int *data, int *next, int FirstAddr);

PtrToNode CreateNode(int Address,int Data,int Next);

int main()

{

int N,K;

int FirstAddr;

List L;

scanf("%d %d %d", &FirstAddr, &N, &K);

L = CreateList(N, FirstAddr);

ListReverse(L,K);

printList(L);

}

List CreateList(int N, int FirstAddr)

{

int data[MAXSIZE];

int next[MAXSIZE];

int i, address;

List L;

for(i=0;i<N;i++)

{

scanf("%d", &address);

scanf("%d %d", &data[address], &next[address]);

}

//建立头结点

L = SortList(data, next, FirstAddr);

return L;

}

List SortList(int *data, int *next, int FirstAddr)

{

List L,tmp,ptr;

L = (List)malloc(sizeof(struct Node));

L->Address = L->Data = L->Next = -1;

L->Next_id = NULL;

if(FirstAddr == -1)

return L;

else if(FirstAddr > -1)

{

tmp = CreateNode(FirstAddr,data[FirstAddr],next[FirstAddr]);

L->Next = FirstAddr; L->Next_id = tmp;

ptr = L->Next_id;

while(ptr->Next > -1)

{

tmp = CreateNode(ptr->Next,data[ptr->Next],next[ptr->Next]);

ptr->Next_id = tmp;

ptr = ptr->Next_id;

}

return L;

}

else

return NULL;

}

PtrToNode CreateNode(int address,int data,int next)

{

PtrToNode node;

node = (PtrToNode)malloc(sizeof(struct Node));

node->Address = address;

node->Data = data;

node->Next = next;

node->Next_id = NULL;

return node;

}

void ListReverse(List L,int K)

{

if(K == 1)

return;

int n = 0, circle,i,j;//n 链表的元素数量

PtrToNode ptr, new_node;

ptr = L;

while(ptr->Next_id)

{

n++;

ptr = ptr->Next_id;

}

if(n == 0)

return;

else

circle = n/K;//需要翻转的次数

ptr = L;

for(i=0;i<circle;i++)

{

new_node = Reverse(ptr,K);

ptr = new_node;

for(j = 1;j<K;j++){

ptr = ptr->Next_id;

}

}

return;

}

PtrToNode Reverse(PtrToNode Head, int K)

{

int cnt = 1;

PtrToNode New_node, Old_node, tmp;

New_node = Head->Next_id;

Old_node = New_node->Next_id;

while(cnt < K)

{

tmp = Old_node->Next_id;

Old_node->Next_id = New_node;

New_node = Old_node;Old_node = tmp;

cnt++;

}

Head->Next_id->Next_id = Old_node;

Head->Next_id = New_node;

return New_node;

}

void printList(List L)

{

PtrToNode ptr;

ptr = L->Next_id;

while(ptr)

{

if(!ptr->Next_id)

printf("%.5d %d -1\n",ptr->Address,ptr->Data);

else

printf("%.5d %d %.5d\n",ptr->Address,ptr->Data,ptr->Next_id->Address);

ptr = ptr->Next_id;

}

}


结果

遇到的问题

int类型的地址如何格式化输出

printf()
函数作为格式化输出函数,提供了格式化的多种方式,详见链接c语言格式化输出

这里使用
%.5d
输出一个5位的整型,不足长度左补齐0

在陈越姥姥给出的单次反转模板的基础上,如何实现多次反转

首先需要知道拿到一个链表需要反转的次数,由于首行输入给出的N中可能有多余的结点,故这里需要首先获得链表的长度,一种是直接写一个链表查链表长度的函数,另一种是在创建链表时传入一个整型长度指针,在创建链表的过程中记录链表长度。这里采用的是方法一,但是复杂度上来说,肯定没有第二种好。

如何用好模拟内存的数组,而不是从将数据读入到数组后再单独创建新的数据结构(链表)对节点进行排序

(暂时还没有研究,欢迎交流)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: