首页 > 其他 > 详细

数据结构 -- 单链表

时间:2014-01-21 09:58:46      阅读:372      评论:0      收藏:0      [点我收藏+]

      快要放假了,实在是待不住了,论文也看不下去,也没啥其他的事做,就写写常用的数据结构吧,正好下个学期就要找工作了,也是方便以后自己使用这些数据结构,好吧,这里实现的数据结构没有太多的错误控制,需要自己去判断什么不应该为空,第一篇文章贴出最经典的单链表的实现:

头文件:

/*
 * dlut_list.h
 *
 *  Created on: 2014年1月13日
 *      Author: DLUTBruceZhang
 */

#ifndef DLUT_LIST_H_
#define DLUT_LIST_H_

typedef 		int			need;

typedef struct _list
{
	need data;
	struct _list *next;
}list;

list * 	dlut_list_create(void);

void		dlut_list_insert_to_head(list *, need);
void		dlut_list_insert_to_tail(list *, need);

void		dlut_list_delete_the_head(list *);
void		dlut_list_delete_the_tail(list *);
void		dlut_list_delete_the_data(list *, need);

need		dlut_list_get_the_head(list *);
need		dlut_list_get_the_tail(list *);

void 		dlut_list_print_the_list(list *);

void		dlut_list_delete_the_list(list *);

#endif /* DLUT_LIST_H_ */


C文件:

/*
 * dlut_list.c
 *
 *  Created on: 2014年1月13日
 *      Author: DLUTBruceZhang
 */


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

list *dlut_list_create()
{
	list *head;

	head = (list *)malloc(sizeof(list));

	if (!head)
		return NULL;

	head -> data = 0;
	head -> next = NULL;

	return head;
}

void dlut_list_insert_to_head(list *head, need data)
{
	list *new_node;

	new_node = (list *)malloc(sizeof(list));

	if (!new_node)
		return;

	new_node -> next = head -> next;
	new_node -> data = data;

	head -> next = new_node;
	head -> data += 1;

	return;
}

void dlut_list_insert_to_tail(list *head, need data)
{
	list *_head = head;

	list *new_node;

	new_node = (list *)malloc(sizeof(list));

	if (!new_node)
		return;

	while (_head -> next != NULL)
		_head = _head -> next;

	new_node -> data = data;
	new_node -> next = NULL;

	_head -> next = new_node;
	head -> data += 1;

	return;
}

void dlut_list_delete_the_head(list *head)
{
	if (head -> next == NULL)
		return;

	list *old_node = head -> next;

	head -> next = head -> next -> next;

	head -> data -= 1;

	free(old_node);

	return;
}


void dlut_list_delete_the_tail(list *head)
{
	if (head -> next == NULL)
		return;

	list *_head = head;

	while (_head -> next -> next != NULL)
		_head = _head -> next;

	list *old_node = _head -> next;

	_head -> next = NULL;

	head -> data -= 1;

	free(old_node);

	return;
}

void dlut_list_delete_the_data(list *head, need data)
{
	if (head -> next == NULL)
		return;

	list *_head = head;

	while (_head -> next != NULL && _head -> next -> data != data)
		_head = _head -> next;

	if (!_head -> next)
		return;

	list *old_node = _head -> next;

	_head -> next = _head -> next -> next;

	head -> data -= 1;

	free(old_node);

	return;
}

need dlut_list_get_the_head(list *head)
{
	return head -> next ? head -> next -> data : -1;
}

need dlut_list_get_the_tail(list *head)
{
	if (head -> next == NULL)
		return -1;

	while (head -> next != NULL)
		head = head -> next;

	return head -> data;
}

void dlut_list_print_the_list(list *head)
{
	while (head -> next != NULL)
	{
		printf("%d  ", head -> next -> data);

		head = head -> next;
	}

	printf("\n");

	return;
}

void dlut_list_delete_the_list(list *head)
{
	while (head -> next != NULL)
	{
		dlut_list_delete_the_head(head);
	}

	free(head);

	return;
}





















数据结构 -- 单链表

原文:http://blog.csdn.net/dlutbrucezhang/article/details/18225473

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!