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;
}