6-2 带头结点的链式表操作集 (20 分)
 

本题要求实现带头结点的链式表操作集。

函数接口定义:

List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );

其中List结构定义如下:

typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

各个操作函数的定义为:

List MakeEmpty():创建并返回一个空的线性表;

Position Find( List L, ElementType X ):返回线性表中X的位置。若找不到则返回ERROR;

bool Insert( List L, ElementType X, Position P ):将X插入在位置P指向的结点之前,返回true。如果参数P指向非法位置,则打印“Wrong Position for Insertion”,返回false;

bool Delete( List L, Position P ):将位置P的元素删除并返回true。若参数P指向非法位置,则打印“Wrong Position for Deletion”并返回false。

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h> #define ERROR NULL
typedef enum {false, true} bool;
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List; List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P ); int main()
{
List L;
ElementType X;
Position P;
int N;
bool flag; L = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
flag = Insert(L, X, L->Next);
if ( flag==false ) printf("Wrong Answer\n");
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.\n", X);
else {
flag = Delete(L, P);
printf("%d is found and deleted.\n", X);
if ( flag==false )
printf("Wrong Answer.\n");
}
}
flag = Insert(L, X, NULL);
if ( flag==false ) printf("Wrong Answer\n");
else
printf("%d is inserted as the last element.\n", X);
P = (Position)malloc(sizeof(struct LNode));
flag = Insert(L, X, P);
if ( flag==true ) printf("Wrong Answer\n");
flag = Delete(L, P);
if ( flag==true ) printf("Wrong Answer\n");
for ( P=L->Next; P; P = P->Next ) printf("%d ", P->Data);
return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

6
12 2 4 87 10 2
4
2 12 87 5

输出样例:

2 is found and deleted.
12 is found and deleted.
87 is found and deleted.
Finding Error: 5 is not in.
5 is inserted as the last element.
Wrong Position for Insertion
Wrong Position for Deletion
10 4 2 5

 1 List MakeEmpty(){
2 List l=(List)malloc(sizeof(struct LNode));
3 l->Data=0;
4 l->Next=NULL;
5 return l;
6 }
7
8 Position Find( List L, ElementType X ){
9 Position p=(Position)malloc(sizeof(Position));
10 while(L){
11 if(L->Data==X){
12 p=L;
13 return p;
14 }
15 L=L->Next;
16 }
17 return ERROR;
18 }//返回线性表中X的位置。若找不到则返回ERROR;
19
20 bool Insert( List L, ElementType X, Position P ){
21
22 Position n=(Position)malloc(sizeof(struct LNode));
23 n->Data=X;
24
25 while(L){
26 if(L->Next==P){
27 n->Next=P;
28 L->Next=n;
29 return true;
30 }
31 L=L->Next;
32 }
33 printf("Wrong Position for Insertion\n");
34 return false;
35 }//将X插入在位置P指向的结点之前,返回true。如果参数P指向非法位置,则打印“Wrong Position for Insertion”,返回false;
36
37 bool Delete( List L, Position P ){
38
39 while(L){
40 if(L->Next==P){
41 L->Next=P->Next;
42 return true;
43 }
44 L=L->Next;
45 }
46 printf("Wrong Position for Deletion\n");
47 return false;
48 }//将位置P的元素删除并返回true。若参数P指向非法位置,则打印“Wrong Position for Deletion”并返回false。

PTA 带头结点的链式表操作集的更多相关文章

  1. [PTA] 数据结构与算法题目集 6-4 链式表的按序号查找 & 6-5 链式表操作集 & 6-6 带头结点的链式表操作集

    带不带头结点的差别就是,在插入和删除操作中,不带头结点的链表需要考虑两种情况:1.插入(删除)在头结点.2.在其他位置. 6.4 //L是给定单链表,函数FindKth要返回链式表的第K个元素.如果该 ...

  2. PTA 求链式表的表长

    6-1 求链式表的表长 (10 分)   本题要求实现一个函数,求链式表的表长. 函数接口定义: int Length( List L ); 其中List结构定义如下: typedef struct ...

  3. C语言 线性表 链式表结构 实现

    一个单链式实现的线性表 mList (GCC编译). /** * @brief 线性表的链式实现 (单链表) * @author wid * @date 2013-10-21 * * @note 若代 ...

  4. jQuery 筛选器 链式编程操作

    $('#i1').next() 下一个标签$('#i1').nextAll() 兄弟标签中,所有下一个标签$('#i1').nextUntil('#ii1') 兄弟标签中,从下一个标签到id为ii1的 ...

  5. [PTA] 数据结构与算法题目集 6-3 求链式表的表长

    Length( List L ){ int res=0; while(L!=NULL){ res++; L=L->Next; } return res; }

  6. [PTA] 数据结构与算法题目集 6-2 顺序表操作集

    //创建并返回一个空的线性表: List MakeEmpty() { List L; L = (List)malloc(sizeof(struct LNode)); L->Last = -1; ...

  7. [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)

    优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...

  8. 线性表->链式存储->线形链表(单链表)

    文字描述: 为了表示前后两个数据元素的逻辑关系,对于每个数据元素,除了存储其本身的信息之外(数据域),还需存储一个指示其直接后继的信息(即直接后继的存储位置,指针域). 示意图: 算法分析: 在单链表 ...

  9. 数据结构----线性表顺序和链式结构的使用(c)

    PS:在学习数据结构之前,我相信很多博友也都学习过一些语言,比如说java,c语言,c++,web等,我们之前用的一些方法大都是封装好的,就java而言,里面使用了大量的封装好的方法,一些算法也大都写 ...

随机推荐

  1. java之 javassist简单使用

    0x01.javassist介绍 什么是javassist,这个词一听起来感觉就很懵,对吧~ public void DynGenerateClass() { ClassPool pool = Cla ...

  2. Docker in Action All In One

    Docker in Action All In One Docker https://www.docker.com/play-with-docker https://hub.docker.com/ $ ...

  3. DNS & HTTPS bug

    DNS & HTTPS bug SSL protocol version bug https://typescript-4.x-tutorials.xgqfrms.xyz/ errors Th ...

  4. PWA App All In One

    PWA App All In One PWA in Action PWA Weather App https://pwa.xgqfrms.xyz/pwa-app/public/index.html c ...

  5. O&#178; & O₂

    O² & O₂ special symbol O² & O₂ HTML HTML subscript and superscript Tags HTML 下标元素 HTML 上标元素 ...

  6. HTML5 QRCode Scaner

    HTML5 QRCode Scaner how to scan QR Code using the camera of the phone or website live demo https://c ...

  7. js in depth & prototype & __proto__

    js in depth & prototype & proto 实例的 proto 与 父类的 prototype,同时指向 父类的构造函数: https://hackernoon.c ...

  8. 国内源安装ROS2

    资料参考: https://www.mscto.com/op/428870.html 需要修正的一点是,使用如下的命令添加源: sudo sh -c 'echo "deb [arch=$(d ...

  9. 在.NET中使用Apache Kafka(一)

    ​曾经在你的应用程序中使用过异步处理吗?在处理不需要立即执行的任务时,异步代码似乎是不可避免的.Apache Kafka是最常用和最健壮的开源事件流平台之一.许多公司和开发者利用它的强大功能来创建高性 ...

  10. eclipse修改默认的代码注释

    在使用Eclipse编写Java代码时,自动生成的注释信息都是默认是使用的当前登录系统用户名,实际上是可以修改的. 选择Window → Preference → Java → Code Style  ...