链表

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


struct Node{
	int value;
	struct Node* next;
};



typedef struct linkList{
	struct Node* head;
	int solt;
}linkList;



int empty(linkList* list)
{
	return list->head == NULL && list->solt == 0;
}



linkList* createList()
{
	linkList* list = (linkList*) malloc(sizeof(linkList));
	if(list == NULL){
		printf("createList malloc faild\n");
		exit(0);
	}
	list->solt = 0;
	list->head = NULL;
	return list;
}


//插入链表
void insert1(linkList* list,int value)
{
	struct Node* node = (struct Node*)malloc(sizeof(struct Node));
	if(node == NULL){
		printf("malloc faild\n");
		exit(0);
	}
	node->next = NULL;
	node->value = value;
	if(empty(list)){
		list->head = node;
		list->solt += 1;	
	}else{
		struct Node* head = list->head;
		while(head->next){
			head = head->next;
		}
		head->next = node;
		list->solt++;
	}
}



void insert2(linkList* list,int value)
{
	struct Node* node = (struct Node*) malloc(sizeof(struct Node));
	if(node == NULL){
		printf("malloc faild");
		exit(0);
	}
	node->value = value;
	node->next = NULL;
	
	struct Node* head = list->head;
	if(head == NULL){
		list->head = node;
		list->solt++;
		return;
	}
	while(head->next){
		head = head->next;
	}
	head->next = node;
	list->solt++;
}


void remove1(linkList* list,int value)
{
	if(empty(list)){
		printf("not own this node\n");
		return;
	}


	struct Node* head = list->head;
	if(head->value == value){
		if(head->next){
			list->head = head->next;
			head->next = NULL;
			free(head);
			list->solt--;
			return;
		}
	}

	while(head->next){
		if(head->next->value == value){
			//head->next是尾节点
			if(head->next->next == NULL){
				free(head->next);
				head->next = NULL;
				list->solt--;
				break;
			}else{
				struct Node* node = head->next;
				head->next = head->next->next;
				node->next = NULL;
				free(node);
				node = NULL;
				list->solt--;
				break;
			}
		}	
		head = head->next;
	}
}


//迭代实现链表的逆序
struct Node* reverse(struct Node* head)
{
	if(head == NULL) return;
	//因为head每次都要和前面后面的进行交换
	struct Node *pre,*next;

	pre = NULL;

	while(head){
		next = head->next;
		head->next = pre;
		pre = head;
		head = next;
	}
	return pre;
}


struct Node* recursiveReverse(struct Node* head)
{
	if(head == NULL || head->next == NULL) return head;
	
	struct Node* newHead = recursiveReverse(head->next);
	//回溯部分如何编码
	head->next->next = head;
	head->next = NULL;
	return newHead;
}


//迭代实现逆序
void reverseList(linkList* list)
{
	//struct Node* pre,next;
	//pre = NULL;
	//if(list->head && list->head->next == NULL) return;
	//reverse(list->head->next);
	//if(list->head->next->next == NULL)
	list->head = reverse(list->head);		

}


//递归实现逆序
void recursiveReverseList(linkList* list)
{
	list->head = recursiveReverse(list->head);

}

//迭代正序打印
void print(linkList* list)
{
	struct Node* head = list->head;
	while(head){
		printf("%d ",head->value);
		head = head->next;
	}
	printf("\n");
}

//递归正序打印
void print2(struct Node* head)
{
	if(head == NULL) return;
	printf("%d ",head->value);
	if(head->next)
		print2(head->next);
	else
		printf("\n");
}

//递归逆序打印
void print3(struct Node* head)
{
	
	if(head == NULL) {
		//printf("\n");
		return;
	}
	if(head->next)
		print3(head->next);
	printf("%d ",head->value);

}


//逆序打印
void reversePrint(linkList* list)
{
	print3(list->head);
	printf("\n");
}



int main()
{
	linkList* list = createList();
	int i;
	for(i=0;i<10;i++){
		insert2(list,i);
		//print(list);
	}
	//删除首节点
	remove1(list,0);	
	print2(list->head);
	//删除尾节点
	remove1(list,9);
	print2(list->head);

	//删除普通节点
	remove1(list,5);
	print2(list->head);

	//逆序打印
	reversePrint(list);
	//print(list);
	
	//逆序
	reverseList(list);
	print(list);
	recursiveReverseList(list);
	print(list);
	return 0;
}



































 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值