单链表的实现

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

typedef struct Node{
	int data;
	struct Node *next;
}node;

typedef struct{
	node head;
	node tail;
}link;

void link_init(link *p)
{
	p->head.next = &(p->tail);
	p->tail.next = NULL;
}

void link_delete(link *p)
{
	node *first = NULL, *mid = NULL, *last = NULL;
	while(p->head.next != &(p->tail)){
		first = &(p->head);
		mid = first->next;
		last = mid->next;
		if(mid != &(p->tail)){
			first->next = last;
			free(mid);
			mid = NULL;
		}
	}
}

int link_size(const link *p)
{
	int cnt = 0;
	node *tmp = p->head.next;
	while(tmp != &(p->tail)){
		cnt++;
		tmp = tmp->next;
	}
	return cnt;
}

int link_empty(const link *p)
{
	return p->head.next == &(p->tail);
}

int link_add_head(link *p, int val)
{
	node *new = NULL;
	new = (node *)malloc(sizeof(node));
	if(!new)
		return 0;
	new->data = val;
	new->next = p->head.next;
	p->head.next = new;
	return 1;
}

int link_append(link *p, int val)
{
	node *new = NULL, *tmp = NULL;
	new = (node *)malloc(sizeof(node));
	if(!new)
		return 0;
	new->data = val;
	new->next = NULL;

	tmp = &(p->head);
	while(tmp->next != &(p->tail)){
		tmp = tmp->next;
	}

	tmp->next = new;
	new->next = &(p->tail);
	return 1;
}

int link_insert(link *p, int val)
{
	node *new = NULL, *tmp = NULL;
	node *first = NULL, *mid = NULL, *last = NULL;

	new = (node *)malloc(sizeof(node));
	if(!new)
		return 0;
	new->data = val;
	new->next = NULL;

	for(tmp = &(p->head); tmp != &(p->tail); tmp = tmp->next){
		first = tmp;
		mid = first->next;
		last = mid->next;
		if(mid == &(p->tail) || mid->data > val){
			first->next = new;
			new->next = mid;
			return 1;
		}
	}
}

int link_remove_head(link *p)
{
	node *tmp = NULL;
	if(p->head.next == &(p->tail))
		return 0;

	tmp = p->head.next;
	p->head.next = tmp->next;
	free(tmp);
	tmp = NULL;
	return 1;
}

int link_remove_tail(link *p)
{
	node *tmp = NULL;
	node *first = NULL, *mid = NULL, *last = NULL;
	if(p->head.next == &(p->tail))
		return 0;

	for(tmp = &(p->head); tmp != &(p->tail); tmp = tmp->next){
		first = tmp;
		mid = first->next;
		last = mid->next;
		if(last == &(p->tail)){
			first->next = last;
			free(mid);
			mid = NULL;
			return 1;
		}
	}
}

int link_remove(link *p, int val)
{
	node *tmp = NULL;
	node *first = NULL, *mid = NULL, *last = NULL;
	if(p->head.next == &(p->tail))
		return 0;

	for(tmp = &(p->head); tmp != &(p->tail); tmp = tmp->next){
		first = tmp;
		mid = first->next;
		last = mid->next;
		if(mid != &(p->tail) && mid->data == val){
			first->next = last;
			free(mid);
			mid = NULL;
			return 1;
		}
	}
	return 0;
}

int link_get_head(const link *p, int *val)
{
	if(p->head.next == &(p->tail))
		return 0;
	*val = p->head.next->data;
	return 1;
}

int link_get_tail(const link *p, int *val)
{
	const node *tmp = NULL;
	if(p->head.next == &(p->tail))
		return 0;
	tmp = p->head.next;
	while(tmp->next != &(p->tail)){
		tmp = tmp->next;
	}
	*val = tmp->data;
	return 1;
}

int link_get(const link *p, int pos, int *val)
{
	int cnt = 0;
	const node *tmp = NULL;
	if(p->head.next == &(p->tail))
		return 0;
	tmp = p->head.next;
	while(tmp != &(p->tail)){
		if(cnt == pos){
			*val = tmp->data;
			return 1;
		}
		cnt++;
		tmp = tmp->next;
	}
	return 0;
}

int main()
{
	int i = 0, size = 0;
	int ret = 0, val = 0;
	link lk = {0};
	link_init(&lk);
	link_insert(&lk, 20);
	link_insert(&lk, 30);
	link_insert(&lk, 50);
	link_add_head(&lk, 10);
	link_append(&lk, 60);
	link_insert(&lk, 40);

	//link_remove_head(&lk);
	//link_remove_tail(&lk);
	link_remove(&lk, 30);
	size = link_size(&lk);
	for(i = 0; i < size; ++i){
		ret = link_get(&lk, i, &val);
		printf("%d ", val);
	}
	printf("\n");

	link_delete(&lk);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值