算法导论-链表

编译环境:dev-c++

以C89的标准来写的,自测通过,后续会陆续更新



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

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


void ll_insert(Node **root,int value){
	Node *a;
	Node *t = (Node *) malloc(sizeof(Node));
	t->value = value;
	t->next = NULL;
	printf("insert %d.\n",value);
	if(*root == NULL){		
		*root = t;
		return;
	}
	a = *root;
	while(1){
		if(a->next == NULL){
			a->next = t;
			break;
		}
		a = a->next;
	}
}

/* 反转单链表 */
void ll_reversal(Node **head_ref){
	Node* prev   = NULL;
    Node* current = *head_ref;
    Node* next;
    while (current != NULL)
    {
        next  = current->next;  
        current->next = prev;   
        prev = current;
        current = next;
    }
    *head_ref = prev;
}


void ll_printf(Node *root){
	Node *t;
	if(root == NULL){
		printf("root null/n");
		return;
	}
	t = root;
	while(1){
		printf("value : %d\n", t->value);
		if(t->next != NULL){
			t = t->next;
		} else {
			break;
		}
	}
}

int main(){

	int i = 0;
	Node *root = NULL;

	//先插入10个元素
	for (i = 0; i < 10; ++i)
	{
		ll_insert(&root,i);		
	}

	//打印
	ll_printf(root);

	//搜索
	ll_search(root,2);

	//删除3个元素
	ll_delete(&root,2);

	//打印
	ll_printf(root);

	printf("ll_reversal linkedlist\n");
	//反转
	ll_reversal(&root);
	//打印
	ll_printf(root);


	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值