如何将一下代码实现链表反转和链表销毁

#include <stdio.h>
#include <stdlib.h>

typedef struct LinkedNode{
	int score;
	struct LinkedNode * next;
} LinkedNode_t, * LinkedNodeP_t;

LinkedNodeP_t insertNode(LinkedNodeP_t pHeader, int newScore) {
	LinkedNodeP_t p1, p2;
	p1 = pHeader;
	p2 = pHeader;
	
	if (p1 == NULL) {
		p1 = (LinkedNodeP_t)malloc(sizeof(LinkedNode_t));
		if (!p1) {
			exit(-1);			
		}
		p1->score = newScore;
		p1->next = NULL; 
		return p1;
	}
	else if (p1->score	>=  newScore) {
		p2 = (LinkedNodeP_t)malloc(sizeof(LinkedNode_t));
		p2->score = newScore;
		p2->next = p1;
		return p2;
	}
	do {
		p2 = p1->next;
		if (p1->score < newScore && (!p2 || p2->score >= newScore)) {
			LinkedNodeP_t p3 = malloc(sizeof(LinkedNode_t));
			p3->score = newScore;
			p3->next = p2;
			p1->next = p3;
			return pHeader;
		}
		else {
			p1 = p2;
			p2 = p2->next;			
		}		
	} while(1);
}

LinkedNodeP_t removeNode(LinkedNodeP_t pHeader, int scoreToRemove) {
	LinkedNodeP_t p1, p2, p3;
	p1 = pHeader;
	p2 = NULL;
	p3 = pHeader;
	
	while(p1 && p1->score < scoreToRemove) {
		p2 = p1;
		p1 = p1->next;				
	}
	
	while (p1 && p1->score == scoreToRemove) {
		if (p2)	{
			p2->next = p1->next;			
		}
		else {
			p3 = p1->next;
		}
		LinkedNodeP_t p4 = p1;
		p1 = p1->next;
		free(p4);
	}
	
	return p3;
}

/**
 ?????????????????????С???? 
*/
LinkedNodeP_t revertList(LinkedNodeP_t pHeader) {
	printf("not implemented yet\n");
	return NULL;
}

/**
 ???????????????? 
*/
void freeList(LinkedNodeP_t pHeader) {
	printf("not implemented yet\n");
	return;	
}

void outputList(LinkedNodeP_t pHeader) {
	LinkedNodeP_t pLn = pHeader;
	int count = 0;
	printf("\n***************\noutput the linkedList\n");
	while(pLn) {
		count ++;
		printf("%d\t", pLn->score);
		pLn = pLn -> next; 
	}
	printf("\n%d nodes totally\n**************\n\n\n", count);		
}

int main( )
{
	LinkedNodeP_t pSortedList = NULL;

	printf("enter the scores to be inserted:\n");
	do {
		int temp;
		scanf("%d", &temp);
		if (temp <= 0) {
			break;			
		}
		pSortedList = insertNode(pSortedList, temp);
	} while(1);
	
	outputList(pSortedList);
	
	printf("enter the one to be removed\n");
	int temp2;
	scanf("%d", &temp2);
	
	pSortedList = removeNode(pSortedList, temp2);
	
	outputList(pSortedList);
	
	printf("now revert the list\n");
	pSortedList = revertList(pSortedList);
	
	outputList(pSortedList);
	
	freeList(pSortedList);
	
	return 0;	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值