数据结构基础(1)-->双向链表

linklist.h
#ifndef	LINK_LIST_H
#define	LINK_LIST_H		1
#include <stdio.h>
#include <string.h>
#include <malloc.h>

struct stu
{
	char		name[32];
	char		stu_num[32];
	char		sex[16];
	int		age;
	struct	stu	*next;
	struct	stu	*pre;
};

struct stu *initlist(struct stu **head);
void destroylist(struct stu *head);
void clearlist(struct stu *head);
int listempty(const struct stu *head);
int listlength(const struct stu *head);
struct stu *getelem(const struct stu *head, int i);
void listinsert(struct stu *head, struct stu *new);
void listdelete(struct stu *node);

#endif /* LINK_LIST_H */

linklist.c
#include <linklist.h>

struct stu *initlist(struct stu **head)
{
	*head = (struct stu *)malloc(sizeof(struct stu));
	if(!*head) {
		return NULL;
	}
	memset(*head, 0, sizeof(struct stu));
	(*head)->next = *head;
	(*head)->pre = *head;
	return *head;
}

void destroylist(struct stu *head)
{
	clearlist(head);
	free(head);
	return;
}

void clearlist(struct stu *head)
{
	struct stu *n;
	for(n=head->next; head->next!=head; n=head->next) {
		head->next = n->next;
		n->next->pre = head;
		free(n);
	}
	return;
}

int listempty(const struct stu *head)
{
	return head->next == head;
}
int listlength(const struct stu *head)
{
	int i = 0;
	struct stu *n = head->next;
	while(n != head) {
		n = n->next;
		++i;
	}
	return i;
}
struct stu *getelem(const struct stu *head, int i)
{
	struct stu *n = (struct stu *)head;
	for(; i--&&((n=n->next)!=head);)/* nothing*/;
	return n==head ? NULL:n;
}
void listinsert(struct stu *head, struct stu *new)
{
	new->next = head;
	new->pre = head->pre;
	head->pre->next = new;
	head->pre = new;
	return;
}
void listdelete(struct stu *node)
{
	node->pre->next = node->next;
	node->next->pre = node->pre;
	free(node);
	return;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值