#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
#define MAXSIZE 100 //链表可能达到的最大长度
typedef struct LNode
{
ElemType data; //结点的数据域
struct LNode *next; //结点的指针域
} LNode, *linkList; //linkList为指向结构体LNode的指针类型
Status InitList(linkList L) //算法2.6 单链表的初始化
{
//构造一个空的单链表L
L = (LNode*)malloc(sizeof(LNode)); //生成新结点作为头结点,用头指针L指向头结点
L->next = NULL; //头结点的指针域置空
return OK;
}
Status DestroyList(linkList L)
{
/* 初始条件:线性表L已存在。操作结果:销毁线性表L */
linkList q;
while(L)
{
q = L->next;
free(L);
L = q;
}
return OK;
}
int ListLength(linkList L)
{
L->data=0;
linkList q=L->next;
while(q)
{
q = q->next;
L->data++;
}
return L->data;
}
int ListEmpty(linkList L)
{
/****在此下面完成代码***************/
L->data=ListLength(L);
if(L->data==0)
return OK;
return ERROR;
/***********************************/
}
Status GetElem(linkList L, int i, ElemType e) //算法2.7 单链表的取值
{
/****在此下面完成代码***************/
linkList q=L->next;
int j=1;
if(i>L->data||i<1)
return ERROR;
else
{
while(j<i)
{
q=q->next;
j++;
}
e=q->data;
return OK;
}
/***********************************/
} //GetElem
int LocateElem(linkList L, int e) //略有改动 算法2.8 按值查找
{
/****在此下面完成代码***************/
int j=1;
linkList q=L->next;
while(q)
{
if(q->data==e)
return j;
q=q->next;
j++;
}
return ERROR;
/*************************************/
} //LocateElem
Status ListInsert(linkList L, int i, ElemType e) //算法2.9 单链表的插入
{
/****在此下面完成代码***************/
linkList q;
if(i>(L->data+1)||i<1)
return ERROR;
else
{
q=L;
int j=0;
while(j<i-1)
{
q=q->next;
j++;
}
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=q->next;
q->next=s;
L->data++;
return OK;
}
/***********************************/
} //ListInsert
Status ListDelete(linkList L, int i) //算法2.9 单链表的删除
{
/****在此下面完成代码***************/
linkList q=L,p;
int j=1;
if(i<1||i>L->data)
return ERROR;
else
{
while(j<i)
{
q=q->next;
j++;
}
p=q->next;
q->next=q->next->next;
free(p);
return OK;
/***********************************/
}
} //ListDelete
void ListPrint(linkList L)
{
LNode *p;
for(p = L->next; p; p = p->next)
{
printf("%d",p->data);
if(p->next)
{
printf(" ");
}
else
printf("\n");
}
}
int main()
{
int i;
ElemType e;
linkList L;
char op[30];
InitList(L);
while(scanf("%s",op)!=EOF)
{
if(strcmp(op,"Empty")==0)
{
if(ListEmpty(L))
printf("Empty\n");
else
printf("Not empty\n");
}
else if(strcmp(op,"Insert")==0)
{
scanf("%d%d",&i,&e);
//cin >> i >> e;
if(ListInsert(L, i, e) == ERROR)
//cout << "Insert failed" << endl;
printf("Insert failed\n");
else
ListPrint(L);
}
else if(strcmp(op,"Length")==0)
{
//cout << "List length is " << ListLength(L) << endl;
printf("List length is %d\n",ListLength(L));
}
else if(strcmp(op,"GetElem")==0)
{
//cin >> i;
scanf("%d",&i);
if(GetElem(L, i, e) == ERROR)
//cout << "Out of index" << endl;
printf("Out of index\n");
else
printf("The elem at position %d is %d\n",i,e);
//cout << "The elem at position " << i << " is " << e << endl;
}
else if(strcmp(op,"LocateElem")==0)
{
//cin >> e;
scanf("%d",&e);
i = LocateElem(L, e);
if(i == 0)
printf("%d is not found in list\n",e);
//cout << e << " is not found in list" << endl;
else
printf("%d is found at the position %d\n",e,i);
//cout << e << " is found at the position " << i << endl;
}
else if(strcmp(op,"Delete")==0)
{
scanf("%d",&i);
if(ListDelete(L, i) == ERROR)
printf("Delete failed\n");
else
ListPrint(L);
}
}
DestroyList(L);
return 0;
}
为什么无法实现呢?