什么叫真正理解一种数据结构,那就是在敲代码时,完全不看书,靠记忆力,纸和笔推导,一步步调试无错为止。
/*-------------------------------------------------------------------------------------
* Project: List.c
* Name: zwp
* Date: 2014/4
*--------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "List.h"
/*
** Initialize
*/
LIST Initialize(LIST H)
{
H = (LIST)malloc(sizeof(LIST));
if(H == NULL)
printf("Out of space....\n");
H->data = 0;
H->next = NULL;
return H;
}
/*
** Empty
*/
unsigned int Empty(LIST H)
{
return (H == NULL) ? 1 : 0;
}
/*
** Insert
*/
void Insert(ElementType num, LIST H)
{
List* N = (List*)malloc(sizeof(List));
if(Empty(H))
{
H->next = N;
}
else
{
N->data = num;
N->next = H->next;
H->next = N;
}
}
/*
** Delete
*/
void Delete(ElementType num, LIST H)
{
if(Empty(H))
printf("The List is Empty...\n");
LIST P = H;
LIST X = NULL;
while(P != NULL)
{
X = P;
P = P->next;
if(P->data == num)
break;
}
LIST T = P;
X->next = P->next;
free(T);
}
/*
** Travse
*/
void Travse(LIST H)
{
LIST P = H->next;
while(P != NULL)
{
printf("%d \n", P->data);
P = P->next;
}
}
/*-------------------------------------------------------------------------------
* Project: List.h
* Name: zwp
* Date: 2014.4
*-------------------------------------------------------------------------------*/
#ifndef LIST_H_
#define LIST_H_
typedef int ElementType;
typedef struct Node
{
ElementType data;
Node* next;
}List;
typedef List* LIST;
/*
** Initialize
*/
LIST Initialize(LIST H);
/*
** Insert
*/
void Insert(ElementType num, LIST H);
/*
** Delete
*/
void Delete(ElementType num, LIST H);
/*
** Travse
*/
void Travse(LIST H);
/*
** Empty
*/
unsigned int Empty(LIST H);
#endif