链表的纯C实现

输入

为线性表的操作系列,每个操作一行,具体见样例。

输出

如果输入为"Empty", 则根据表是否为空输出"Empty"或 "Not empty"。

如果输入为"Length",则输出表长。

如果输入为"Insert i e",插入失败则输出"Insert failed",否则在i位置插入e后输出插入后表中的所有元素。

如果输入为"GetElem i ",参数i错误输出"Out of index",否则输出在i位置的元素。

如果输入为"LocateElem e",如果未发现输出"e  is not found in list",否则输出e在表中的位置。

如果输入为"Delete i",如果失败输出"Delete failed",否则删除i位置的元素后输出插入后表中的所有元素。

具体参见样例。

样例输入

Empty
Insert 1 7
Empty
Insert 2 3
Length
Insert 1 -100
Length
Insert 10 100
Length
Insert 2 10000
Empty
GetElem 2
GetElem 5
LocateElem 999
LocateElem 3
Delete 0
Delete 4
Delete 1
Length

样例输出

Empty
7
Not empty
7 3
List length is 2
-100 7 3
List length is 3
Insert failed
List length is 3
-100 10000 7 3
Not empty
The elem at position 2 is 10000
Out of index
999 is not found in list
3 is found at the position 4
Delete failed
-100 10000 7
10000 7
List length is 2
#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=(LinkList)malloc(sizeof(LNode));//L = new LNode; //生成新结点作为头结点,用头指针L指向头结点
    (*L)->next = NULL; //头结点的指针域置空
    return OK;
}
 
Status DestroyList(LinkList *L)
{
   
    /* 初始条件:线性表L已存在。操作结果:销毁线性表L */
    LinkList q;
    while (*L)
    
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值