6-5 链式表操作集 (20分)

本题要求实现链式表的操作集。

函数接口定义:
Position Find( List L, ElementType X );
List Insert( List L, ElementType X, Position P );
List Delete( List L, Position P );

其中List结构定义如下:

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

各个操作函数的定义为:

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

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

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

裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>

#define ERROR NULL
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

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

int main()
{
List L;
ElementType X;
Position P, tmp;
int N;

L = NULL;
scanf("%d", &N);
while ( N-- ) {
    scanf("%d", &X);
    L = Insert(L, X, L);
    if ( L==ERROR ) 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 {
        L = Delete(L, P);
        printf("%d is found and deleted.\n", X);
        if ( L==ERROR )
            printf("Wrong Answer or Empty List.\n");
    }
}
L = Insert(L, X, NULL);
if ( L==ERROR ) printf("Wrong Answer\n");
else
    printf("%d is inserted as the last element.\n", X);
P = (Position)malloc(sizeof(struct LNode));
tmp = Insert(L, X, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
tmp = Delete(L, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
for ( P=L; 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

Position Find(List L, ElementType X)
{
	while(L){//省略写法 原本应该是L!=NULL但while()的判断机制中默认NULL为假
        if(L->Data==X)return L;
        L=L->Next;
    }return ERROR;
}
List Insert(List L, ElementType X, Position P)
{   /*观察主函数中Insert(L,X,L),再结合Insert函数的功能
得出该函数是头插法的应用*/
    List prev,head=L;//无论L怎样改变最终返回表头,用head来记录。
    //原函数中L并没有分配空间故在Insert函数体内需要进行malloc动态分配
    prev=(List)malloc(sizeof(List));
    prev->Next=NULL;prev->Data=X;
//情况1:如果是在头结点插入则需要特殊处理
    if(P==L){
        prev->Next=P;
        return prev;
    }
//情况2:如果正常插入中间部分
    while(L->Next){
        if(L->Next==P){
            prev->Next=P;
            L->Next=prev;//注意顺序
            //操作结束直接返回头指针
            return head;
        }L=L->Next;
    }
//情况3:如果P为NULL,即插入最后
    if(P==NULL){
        L->Next=prev;
        return head;
    }
//情况4:P指向非法位置
    printf("Wrong Position for Insertion\n");
    //观察输出样例需要打换行符不然会影响整体格式
    return ERROR;
}
List Delete(List L, Position P)
{
	List head=L,prev,temp;//因为要返回表头故用head标记
//情况1:P在表头
    if(P==head){
        //需释放空间
        temp=L;
        L=L->Next;
        free(temp);
        return L;
    }

    if(L->Next)prev=L->Next;
    while(L->Next){
//情况2:P在表中
        if(prev==P&&prev->Next)//且不为尾节点
        {
            temp=prev;
            prev=prev->Next;
            L->Next=prev;
            free(temp);
            return head;
        }
//情况3:P是尾节点
        if(prev==P&&prev->Next==0)
        {
            L->Next=NULL;
            free(prev);
            return head;
        }
        prev=prev->Next;
        L=L->Next;
    }
//情况4:非法情况
        printf("Wrong Position for Deletion\n");
        return ERROR;
}

该题的代码都是基本的单链表操作没有难度,稍微有点绕的就是主函数的写法和函数中的参数List L,需要观察出该题函数Insert采用的是头插法:
基本上平时常用的是尾插法,这里总结一下:
头插法:
头插法

尾插法
在这里插入图片描述
除此之外进行链表操作时需注意三段式:
插入时:在头结点之前,在表中,在尾节点之后;
删除时:删除头结点,删除表中,删除尾节点;
特殊情况特殊处理。

2020/3/10——By Suki

发布了29 篇原创文章 · 获赞 27 · 访问量 2952

猜你喜欢

转载自blog.csdn.net/Eumenides_Suki/article/details/104765702