Reversing Linked List(7个测试点只过了3个,好难,放弃了)

#include 
#include 

typedef struct Node *List;
struct Node {
	int Address;
	int Data;
	int NxtAdd;
	List Next;
};

List Create(int *N, int *K);
void Print(List list);
List Reverse(int n, int k, List list);

int main() {
	List list, rList;
	int n, k;
	list = Create(&n, &k);
//	Print(list);
	rList = Reverse(n, k, list);
	Print(rList);
}

List Create(int *N, int *K) {
	int firstAdd;
	int i, j;
	scanf("%d", &firstAdd);
	scanf("%d", N);
	scanf("%d", K);

	int *arrList;
	int len=(*N)*3;
	arrList = (int*)malloc(sizeof(int)*len);


	i=0;
	while (len--) {
		scanf("%d", &arrList[i++]);
	}

	i=0;
	j=0;
	int add = firstAdd;
	List list, head, temp;
	head = (List)malloc(sizeof(struct Node));
	head->Next = NULL;
	list = head;

	for ( i=0; i<(*N); i++ ) {
		for ( j=0; j<(*N); j++ ) {
			int findAdd = j*3;
			if (arrList[findAdd] == add) {
				temp = (List)malloc(sizeof(struct Node));
				temp->Address = arrList[findAdd];
				temp->Data = arrList[findAdd+1];
				temp->NxtAdd = arrList[findAdd+2];
				temp->Next = NULL;
				list->Next = temp;
				list = temp;
				add = arrList[findAdd+2];
				break;
			}
		}
	}

	return head;
}

void Print(List list) {
	for ( list=list->Next; list; list=list->Next ) {
		printf("%.5d ", list->Address);
		printf("%d ", list->Data);
		if ( list->NxtAdd==-1 )
			printf("%d\n", list->NxtAdd);
		else
			printf("%.5d\n", list->NxtAdd);
	}
}

List Reverse(int n, int k, List list) {
	List HOL, TOL, head, rList, temp;
	HOL = list;
	if (list = list->Next) {
		int remainder, quotient;
		quotient = n/k;
		remainder = n%k;
		head = (List)malloc(sizeof(struct Node));
		head->Next = NULL;
		rList = head;

		int i, j;
		for ( i=0; i<quotient; i++ ) {

			int start;
			start = k*(i+1);
			int p;
			int nxtAdd;
			while (start) {
				list = HOL;
				for (p=0; p<start; p++) {
					nxtAdd = list->Address;
					list = list->Next;
				}
				if ( start==k*quotient ) {
					TOL = list->Next;
				}
				temp = (List)malloc(sizeof(struct Node));
				temp->Address = list->Address;
				temp->Data = list->Data;
				temp->NxtAdd = nxtAdd;
				temp->Next = NULL;
				rList->Next = temp;
				rList = temp;
				start--;
			}
		}

		if (TOL) {
			rList->NxtAdd = TOL->Address;
			for ( ; TOL; TOL=TOL->Next) {
				temp = (List)malloc(sizeof(struct Node));
				temp->Address = TOL->Address;
				temp->Data = TOL->Data;
				temp->NxtAdd = TOL->NxtAdd;
				temp->Next = NULL;
				rList->Next = temp;
				rList = temp;
			}
		}
		
		rList->NxtAdd = -1;
	}

	return head;
}

Reversing Linked List(7个测试点只过了3个,好难,放弃了)_第1张图片

你可能感兴趣的:(Reversing Linked List(7个测试点只过了3个,好难,放弃了))