数据结构 -- 单链表之删除节点

1. 单链表的创建

2.单链表的 节点的删除

以下代码在 vs2010 测试通过:

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

#define FALSE 0
#define TRUE 1
#define NOEXISTS 2


//typedef status int;
typedef struct NODE{
	int value;
	struct NODE *next;
}Node ;

int link_create(Node **list,int length);
int link_delete(Node **list, int value);
int main(){
	Node *list = NULL;
	Node *clist;
	Node *dlist;
	int length = 5;
	int value ;
	int cresult,dresult,result;
	cresult = link_create(&list,length);
	if(cresult == 0){
		return FALSE ;
	}
	printf("链表创建成功,他们的值为:\n");
	
	clist = list->next;
	while(clist != NULL){
		printf("%4d",clist->value);
		clist = clist->next;
	}
	printf("\n请输入要删除的数字:\n");
	dresult = scanf("%d",&value);
	if(dresult == 0){
		return FALSE;
	}

	result = link_delete(&list,value);
	if(result == 0){
		return FALSE ;
	}else if(result == 1){
		dlist = list->next;
		while(dlist != NULL){
			printf("%4d",dlist->value);
			dlist = dlist->next;
		}
	}else{
		printf("该节点的值不存在!");
		
	}
	printf("\n");
	system("pause");
	return TRUE;
	
}
int link_create(Node **list,int length){
	int i;
	Node *phead;
	Node *next;
	Node *new_node;

	*list = (Node *)malloc(sizeof(Node));
	(*list)->value = length;
	(*list)->next = NULL;
	
	for(i = 0 ; i < length ; i++){
		new_node = (Node *)malloc(sizeof(Node));
		if(new_node == NULL){
			return FALSE ;
		}
		new_node->value = i+4 ; //新建链表的值

		new_node->next = (*list)->next ;
		(*list)->next = new_node;
	}
	
	return TRUE ;
}
int link_delete(Node **list, int value){

	Node *previous;
	Node *next;

	next = *list;
	if(next->next == NULL){
		return FALSE;
	}
	previous = next;
	next = next->next;
	while(next != NULL){
		if(next->value != value){
			previous = next;
			next = next->next;
		}else{
			previous->next = next->next;
			free(next);
			return TRUE;
		}		
	}
	return NOEXISTS;

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值