一个数据链表的建立

     背景: 最近由于决定要学习一下C 语言的基础,所以决定一切从头开始。所以学习了一下链表的建立,由于基础不是太好,所以写的东西难免有些不合理的地方。所以还望阅读者见谅。

     环境: 操作系统:Debian 5 (Linux debian 2.6.26-2-686)

            编译器  :gcc version 4.4.5 (Debian 4.4.5-8)

     下面就是实验的代码:

 

/*
** This file is aimed to create a list,then display it,if you
** want you can delete a note from the list,after that display
** the list again.
**
** Copyrigth <C> <2011-03-19> <by CSDN_copyleft>
**
** License : CPL V3 <for more infomation,also see : 
**			 http://www.gnu.org/copyleft/gpl.html>
**
** Version : Debug 0.01
** Finish time : 2011-03-19
** Author  : CSDN_copyleft < yyphtwx@163.com >
*/

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

/* the list struct */
struct node
{
	char data;
	struct node *next;
};

/*
** this function aimed to creat a list.When input 'Q' to finish
** to create the list,and the return value is a pointer pinting
** to the list head if successful,otherwise return NULL.
*/
struct node *p_create(void)
{
	char		 ch = '0';
	struct node *head = NULL;
	struct node *p = NULL;	/* to locate the last node */
	struct node *s = NULL;	/* to get a new node */

	head = (struct node *)malloc(sizeof(struct node));
	if (head == NULL)
	{	/* malloc failed, return NULL */
		printf("malloc failed!\n");
		perror("malloc");	/* to give the failed reason */
		return NULL;
	}
	/* initialization the head pointer */
	head->data = '0';
	head->next = NULL;
	p = head;

	/* input the data */
	scanf("%c", &ch);

	/*
	** try to creat the list.if successful return the head of 
	** the list,otherwise return NULL.When input the 'Q',finish
	** creation the list and return the list head.
	*/
	while(ch != 'Q' && ch != 'q')
	{
		s = (struct node *)malloc(sizeof(struct node));
		if (s == NULL)
		{	/* malloc failed,return NULL,and release the resource */
			printf("malloc for while failed!\n");
			perror("malloc => while failed by:");

			free(head);
			return NULL;
		}

		/* set the s as a node of the list */
		s->data = ch;
		s->next = NULL;

		/* the first time to creat a node */
	//	if (head->next == NULL)
	//		head->next = s;

		/* to remember the last node */
		p->next = s;
		p = s;

		scanf("%c", &ch);
	}

	return head;
}

/*
** this function is to print the list.The input arg is the
** head of the list.If the list is empty,print a message
** the return value is the list members.
*/
int print_list(const struct node **head)
{
	int count = 0;	/* to count the members of the list */
	struct node *tmp = (*head)->next;

	if (head == NULL || (*head)->next == NULL)
	{	/* maybe the list is a empty list */
		printf("the list is empty!\n");
		return 0;
	}

	/* display the list */
	while(tmp!= NULL)
	{
		printf("the %d member is : %c\n", count, tmp->data);

		++count;	/* increase the count */

		tmp = tmp->next;
	}

	printf("the list count is : %d\n", count);

	return count;
}

/*
** this function delete one of the list member you give
** return 0 with success,failed return -1
*/
int delete_member(struct node **head, int which_one)
{
	int			 count = 0;	/* to count the members of the list */
	int			 i = 0;		/* to count the deleted member */
	struct node *tmp = (*head)->next;
	struct node *p	 = NULL;

	/* we cann't delete the empty list,so ... */
	if (head == NULL || (*head)->next == NULL)
	{
		printf("cann't delete the empty list!\n");
		errno = EINVAL;
		return -1;
	}

	/* if which_one less than 0,don't delete the node */
	if (which_one < 0)
	{
		printf("we don't delete any node from the list!\n");
		return 0;
	}

	while(tmp!= NULL)
	{
		++count;

		/* make tmp point to the next node */
		tmp = tmp->next;
	}

	/* make the tmp point to the head of the list */
	tmp = *head;

	while(tmp->next != NULL)
	{
		if (i == which_one)
		/* find the deleted member's place */
			break;

		++i;
		tmp = tmp->next;
	}

	/* maybe the list members is less than you input arg */
	if (i < which_one)
	{	/* the list's members is less than input arg */
		printf("the list's members is less than you input!\n");
		errno = EINVAL;
		return -1;
	}

	/* the tmp->next node will be deleted */
	p = tmp->next;
	tmp->next = p->next;
	free(p);

	return 0;
}

/*
** this function try to delete the list,and release the resouce
** successful return 0,otherwise return -1.the input arg is the
** list head pointer
*/
int delete_list(struct node **head)
{
	struct node *tmp = (*head)->next;	/* the node will be freed */
	struct node *position = (*head)->next;	/* store the current position */

	if (head == NULL || *head == NULL)
	{
		printf("we cann't delete the empty list,so ...\n");
		errno = EINVAL;
		return -1;
	}

	free(*head);
	memset(*head, 0, sizeof(struct node));

	while(position != NULL)
	{	/* try to release the resource */
		position = position->next;
		free(tmp);
		memset(tmp, 0, sizeof(struct node));
		tmp = position;
	}

	return 0;
}


int main(void)
{
	int			mycount = 0;
	int			which_one = -1;
	int			tmpval = -1;
	struct node *head = NULL;

	/* to create a list with your input characters */
	printf("please input the data<'Q' or 'q' to finish>:\n");
	head = p_create();

	printf("your input list is : \n");
	mycount = print_list(&head);
	printf("the list has %d members!\n", mycount);

	/* to delete a node from your list */
	printf("we count the member form 0,not 1!\n");
	printf("which one do you like to delete the member from the list?\n");
	scanf("%d", &which_one);

	tmpval = delete_member(&head, which_one);
	if (tmpval < 0)
	{	/* delete member failed,so we release the resource */
		printf("delete_member failed!\n");
		perror("delete_member failed by:");
		goto err_exit;
	//	return -1;
	}

	/* display the new list after delete the member */
	printf("after delete the member's list :\n");
	mycount = print_list(&head);
	printf("the list has %d members!\n", mycount);

/*
** failed of delete_member,we also try to delete_list to
** return the resource to the system,because we borrowed it.
** so ... you know... to be a good guy!
*/
err_exit:
	/* we must return the resource to the system so... */
	tmpval = delete_list(&head);
	if (tmpval < 0)
	{
		printf("delete_list failed!\n");
		perror("delete_list failed by:");
		return -1;
	}
	printf("delete_list done!\n");

	/* try to display the list after release the list */
	printf("trt to display the list after release the list!\n");
	tmpval = print_list(&head);
	if (tmpval < 0)
	{
		perror("printf_list aafter release failed:");
		return -1;
	}

	return 0;
}


 

    对于运行的结果在此我就不用多说了,结果也是很明显的。里面如果有什么没有考虑到或者写的不对的地方,还望读者能够指出。希望读者能够指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值