带头结点单链表。分三个文件,分别为头文件,测试文件,源代码文件。
给出了单链表的部分操作,每个操作都有对应的注释说明该函数功能。
test.c 文件中,对输入数据是否合法进行了检测,但是并未实现输入数据不合法时应该采取的措施。
测试文件 通过一个菜单来测试单链表功能。
list.h:
/* 单链表类型的头文件 */
#ifndef LIST_H_
#define LIST_H_
struct Node; //先声明,就可以在下面使用,而不必因定义在后面而产生冲突
/* 一般类型定义 */
typedef int ElementType; //抽象数据类型
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
/* 特定于程序的声明 */
struct Node {
ElementType element;
Position next;
};
/* 函数原型 */
List MakeEmpty ( List l );
int IsEmpty ( List L );
int IsLast ( Position p, List l );
Position Find ( ElementType x, List l );
void Delete ( ElementType x, List l );
Position FindPrevious ( ElementType x, List l );
void Insert ( ElementType x, List l, Position p );
void DeleteList ( List l );
Position Header ( List l );
Position First ( List l );
Position Last ( List l );
Position Advance ( Position p );
ElementType Retrieve ( Position p );
void PushFront ( ElementType x, List l );
void PopFront ( List l );
void PushBack ( ElementType x, List l );
void PopBack ( List l );
#endif
test.c:
/* 单链表 */
#include <stdio.h>
#include <windows.h>
#include "list.h"
struct Node;
void Menu ( void ); //菜单
void PrintList ( List l ); //打印单链表
int main ( void )
{
List l;
int choice;
ElementType x;
l = NULL;
choice = 0;
x = 0;
Menu ( );
scanf ( "%d", &choice );
while ( 0 != choice )
{
switch ( choice )
{
case 1: if ( NULL == ( l = MakeEmpty ( l ) ) ) //如若不接受返回值,则外面 l 未改变。因为 MakeEmpty 中形参 不影响 实参值,开辟的空间反而找不到了!重点!!
{
printf ( "初始化失败!\n" );
}
else
{
printf (