此链表采用最基本的实现方法。
ADT-list.h:
/**********************************
*author:Felix
*last update:Tue Jan 1 04:33:03 EST 2008
*description:
*
*
*
*/
#ifndef ___LIST___
#define ___LIST___
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrNode;
typedef PtrNode List;
typedef PtrNode Position;
typedef int Element;
List MakeEmpty();
int IsEmpty(List l);
int IsLast(Position p);
Position Find(Element e,List l);
void Delete(Element e,List l);
Position FindPrevious(Element e,List l);
Position Insert(Element e,List l,Position p);
void DeleteList(List l);
Position Header(List l);
Position First(List l);
Position Advance(Position p);
Element Retrieve(Position p);
void Update(Position p,Element e);
#endif
实现-list.c
/**********************************
*author:Felix
*last update:Tue Jan 1 04:33:03 EST 2008
*description:
*
*
*
*/
#include "list.h"
struct Node
{
Element e;
Position next;
};
List MakeEmpty()
{
List l;
l=(List)malloc(sizeof(struct Node));
if(!l)return NULL;
l->next=NULL;
return l;
}
int IsEmpty(List l)
{
return NULL==l->next;
}
int IsLast(Position p)
{
return NULL==p->next;
}
Position Find(Element e,List l)
{
Position p=l->next;
while(p&&p->e!=e)p=Advance(p);
return p;
}
void Delete(Element e,List l)
{
Position p=FindPrevious(e,l);
Position tmp=Advance(p);
p->next=Advance(tmp);
free(tmp);
}
Position FindPrevious(Element e,List l)
{
Position p=l;
while(p->next&&p->next->e!=e)p=Advance(p);
return p->next?p:NULL;
}
Position Insert(Element e,List l,Position p)
{
Position tmp=(Position)malloc(sizeof(struct Node));
if (!tmp) return NULL;
tmp->e=e;
tmp->next=p->next;
p->next=tmp;
return tmp;
}
void DeleteList(List l)
{
Position p,tmp;
p=tmp=First(l);
l->next=NULL;
while(p)
{
p=Advance(p);
free(tmp);
tmp=p;
}
}
Position Header(List l)
{
return l;
}
Position First(List l)
{
return l->next;
}
Position Advance(Position p)
{
return p?p->next:NULL;
}
Element Retrieve(Position p)
{
return p->e;
}
void Update(Position p,Element e)
{
p->e=e;
}
注意:为了实现数据封装,struct Node的定义不应放进头文件中
测试部分:
menu_c.h与menu_c.c用脚本makeTCtmp(见前面文章)生成,testList.c也是在原来自动生成文件的基础上加入内容实现的
/*menu_c.h:*/
/*///
*author:Felix
*create date:Tue Jan 1 05:13:11 EST 2008
*last update:Tue Jan 1 05:13:11 EST 2008
*description:
*
*
*/
#include <stdio.h>
#ifndef __MENU____
#define __MENU____
#define SELECT() ((___sel=___select())!='0')
#define SELECTION ___sel
char ___sel;
char ___select();
/*
define the menu:
*/
#endif
/*menu_c.c*/
/*///
*author:Felix
*create date:Tue Jan 1 05:13:11 EST 2008
*last update:Tue Jan 1 05:13:11 EST 2008
*description:
*
*
*/
#include "menu_c.h"
char *___menu[]={
"1.Print list.h",
"2.Initialize the list with random integer.",
"3.Insert a number to the list.",
"4.Delete a number from the list.",
"5.Update a number in the list.",
"6.Delete the whole list.",
"7.Print the list",
"0.EXIT",
NULL
};
void ___showMenu()
{
printf("please select:/n");
char **p=___menu;
while(*p)printf("%s/n",*p++);
printf(":>");
}
char ___select()
{
char c;
___showMenu();
while((c=getchar())=='/n');
return c;
}
/******************testList.c******************/
/*///
*author:Felix
*create date:Tue Jan 1 05:13:11 EST 2008
*last update:Tue Jan 1 05:13:11 EST 2008
*description:
*
*
*//
#include "menu_c.h"
#include<stdio.h>
#include<stdlib.h>
#include"list.h"
#define RAN_COUNT 30
#define DEF_MAX 9999
int main()
{
List l;
Position p;
int n,n2;
int count;
l=MakeEmpty();
while(SELECT())
{
switch (SELECTION)
{
/*Print list.h*/
case '1':
system("less ../list.h");
break;
/*Initialize the list with random integer.*/
case '2':
count=RAN_COUNT;
while(count-->0)Insert(random_f(DEF_MAX),l,Header(l));
break;
/*Insert a number to the list.*/
case '3':
printf("number to insert:>");
if(scanf("%d",&n)>0)
Insert(n,l,First(l));
else printf("illegal input");
break;
/*Delete a number from the list.*/
case '4':
printf("number to delete:>");
if(scanf("%d",&n)>0)
{
if (Find(n,l))Delete(n,l);
else printf("no match found/n");
}else printf("illegal input/n ");
break;
/*Update a number in the list.*/
case '5':
printf("integer to update:>");
if(scanf("%d",&n)>0)
{
if (p=Find(n,l))
{
printf("update with:>");
if(scanf("%d",&n2)>0)Update(p,n2);
else printf("illegal input");
}
else printf("no match found/n");
}else printf("illegal input/n ");
break;
/*Delete the whole list.*/
case '6':
DeleteList(l);
break;
/*print the list*/
case '7':
p=First(l);
while(p)
{
printf("%d ",Retrieve(p));
p=Advance(p);
}
default:break;
}
}
free(l);
return 0;
}
ADT-list.h:
/**********************************
*author:Felix
*last update:Tue Jan 1 04:33:03 EST 2008
*description:
*
*
*
*/
#ifndef ___LIST___
#define ___LIST___
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrNode;
typedef PtrNode List;
typedef PtrNode Position;
typedef int Element;
List MakeEmpty();
int IsEmpty(List l);
int IsLast(Position p);
Position Find(Element e,List l);
void Delete(Element e,List l);
Position FindPrevious(Element e,List l);
Position Insert(Element e,List l,Position p);
void DeleteList(List l);
Position Header(List l);
Position First(List l);
Position Advance(Position p);
Element Retrieve(Position p);
void Update(Position p,Element e);
#endif
实现-list.c
/**********************************
*author:Felix
*last update:Tue Jan 1 04:33:03 EST 2008
*description:
*
*
*
*/
#include "list.h"
struct Node
{
Element e;
Position next;
};
List MakeEmpty()
{
List l;
l=(List)malloc(sizeof(struct Node));
if(!l)return NULL;
l->next=NULL;
return l;
}
int IsEmpty(List l)
{
return NULL==l->next;
}
int IsLast(Position p)
{
return NULL==p->next;
}
Position Find(Element e,List l)
{
Position p=l->next;
while(p&&p->e!=e)p=Advance(p);
return p;
}
void Delete(Element e,List l)
{
Position p=FindPrevious(e,l);
Position tmp=Advance(p);
p->next=Advance(tmp);
free(tmp);
}
Position FindPrevious(Element e,List l)
{
Position p=l;
while(p->next&&p->next->e!=e)p=Advance(p);
return p->next?p:NULL;
}
Position Insert(Element e,List l,Position p)
{
Position tmp=(Position)malloc(sizeof(struct Node));
if (!tmp) return NULL;
tmp->e=e;
tmp->next=p->next;
p->next=tmp;
return tmp;
}
void DeleteList(List l)
{
Position p,tmp;
p=tmp=First(l);
l->next=NULL;
while(p)
{
p=Advance(p);
free(tmp);
tmp=p;
}
}
Position Header(List l)
{
return l;
}
Position First(List l)
{
return l->next;
}
Position Advance(Position p)
{
return p?p->next:NULL;
}
Element Retrieve(Position p)
{
return p->e;
}
void Update(Position p,Element e)
{
p->e=e;
}
注意:为了实现数据封装,struct Node的定义不应放进头文件中
测试部分:
menu_c.h与menu_c.c用脚本makeTCtmp(见前面文章)生成,testList.c也是在原来自动生成文件的基础上加入内容实现的
/*menu_c.h:*/
/*///
*author:Felix
*create date:Tue Jan 1 05:13:11 EST 2008
*last update:Tue Jan 1 05:13:11 EST 2008
*description:
*
*
*/
#include <stdio.h>
#ifndef __MENU____
#define __MENU____
#define SELECT() ((___sel=___select())!='0')
#define SELECTION ___sel
char ___sel;
char ___select();
/*
define the menu:
*/
#endif
/*menu_c.c*/
/*///
*author:Felix
*create date:Tue Jan 1 05:13:11 EST 2008
*last update:Tue Jan 1 05:13:11 EST 2008
*description:
*
*
*/
#include "menu_c.h"
char *___menu[]={
"1.Print list.h",
"2.Initialize the list with random integer.",
"3.Insert a number to the list.",
"4.Delete a number from the list.",
"5.Update a number in the list.",
"6.Delete the whole list.",
"7.Print the list",
"0.EXIT",
NULL
};
void ___showMenu()
{
printf("please select:/n");
char **p=___menu;
while(*p)printf("%s/n",*p++);
printf(":>");
}
char ___select()
{
char c;
___showMenu();
while((c=getchar())=='/n');
return c;
}
/******************testList.c******************/
/*///
*author:Felix
*create date:Tue Jan 1 05:13:11 EST 2008
*last update:Tue Jan 1 05:13:11 EST 2008
*description:
*
*
*//
#include "menu_c.h"
#include<stdio.h>
#include<stdlib.h>
#include"list.h"
#define RAN_COUNT 30
#define DEF_MAX 9999
int main()
{
List l;
Position p;
int n,n2;
int count;
l=MakeEmpty();
while(SELECT())
{
switch (SELECTION)
{
/*Print list.h*/
case '1':
system("less ../list.h");
break;
/*Initialize the list with random integer.*/
case '2':
count=RAN_COUNT;
while(count-->0)Insert(random_f(DEF_MAX),l,Header(l));
break;
/*Insert a number to the list.*/
case '3':
printf("number to insert:>");
if(scanf("%d",&n)>0)
Insert(n,l,First(l));
else printf("illegal input");
break;
/*Delete a number from the list.*/
case '4':
printf("number to delete:>");
if(scanf("%d",&n)>0)
{
if (Find(n,l))Delete(n,l);
else printf("no match found/n");
}else printf("illegal input/n ");
break;
/*Update a number in the list.*/
case '5':
printf("integer to update:>");
if(scanf("%d",&n)>0)
{
if (p=Find(n,l))
{
printf("update with:>");
if(scanf("%d",&n2)>0)Update(p,n2);
else printf("illegal input");
}
else printf("no match found/n");
}else printf("illegal input/n ");
break;
/*Delete the whole list.*/
case '6':
DeleteList(l);
break;
/*print the list*/
case '7':
p=First(l);
while(p)
{
printf("%d ",Retrieve(p));
p=Advance(p);
}
default:break;
}
}
free(l);
return 0;
}