鱼C论坛

 找回密码
 立即注册
查看: 664|回复: 1

[已解决]链式表

[复制链接]
发表于 2022-12-12 10:50:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#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;
}

/* 你的代码将被嵌在这里 */
Position Find( List L, ElementType X )
{
        while(L!=NULL)       //循环遍历链表查找目标值
        {
                if(L->Data==X)
                {
                        return L;
                }
                L=L->Next;      //链表向后查找
        }
        return ERROR;      
}
List Insert( List L, ElementType X, Position P )
{
        List head=L;          //保存表头
        List temp = (List)malloc(sizeof(struct LNode)); //创建新的节点
        temp->Data=X;     //将值插入该节点
        temp->Next =NULL;
       
        if(L==P)         //特殊情况,若P为表头时
        {
                temp->Next = L;  //将新节点直接插入表头前
                return temp;
        }
       
        while(L!=NULL)   
        {
                if(L->Next==P)    //一般情况,新节点要插在P和P前一个节点之间,所以判断条件要找到L和L->Next
                {
                        temp->Next = L->Next;  //将P位置的节点变为新节点的后继节点
                        L->Next = temp;        //将新节点变为原P位置前一个节点的后继节点,也就是把新节点插入在原P前一个节点和P节点之间
                        return head;
                }
                L=L->Next;       //若不满足条件,继续循环遍历
        }
        printf("Wrong Position for Insertion\n");    //若上面条件没有找到正确位置则表示指向了非法位置
        return ERROR;
}

List Delete( List L, Position P )
{
        List head=L,temp;
       
        if(L==P)        //特殊情况,删除位置为表头时
        {
                L = L->Next;
                return L;
        }
        while(L->Next!=NULL)
        {       
                if(L->Next==P)
                {
                        temp = L->Next;      
                        L->Next = temp->Next; //将temp后继节点变为L的后继节点,表示将temp删除
                        return head;
                }
                L=L->Next;
        }
        printf("Wrong Position for Deletion\n");
        return ERROR;
}







救命,这个main函数该怎么写啊。。。
最佳答案
2023-6-1 21:28:44
下面是一个可以运行上面提供的代码的 `main` 函数:


  1. int main()
  2. {
  3.     List L = NULL;
  4.     ElementType X;
  5.     Position P, tmp;
  6.     int N;

  7.     // 插入元素并检查是否出错
  8.     scanf("%d", &N);
  9.     for (int i = 0; i < N; i++) {
  10.         scanf("%d", &X);
  11.         L = Insert(L, X, L);
  12.         if (L == ERROR) {
  13.             printf("Wrong Answer\n");
  14.             return 0;
  15.         }
  16.     }

  17.     // 删除指定元素并打印结果
  18.     scanf("%d", &N);
  19.     for (int i = 0; i < N; i++) {
  20.         scanf("%d", &X);
  21.         P = Find(L, X);
  22.         if (P == ERROR) {
  23.             printf("Finding Error: %d is not in.\n", X);
  24.         } else {
  25.             L = Delete(L, P);
  26.             printf("%d is found and deleted.\n", X);
  27.             if (L == ERROR) {
  28.                 printf("Wrong Answer or Empty List.\n");
  29.                 return 0;
  30.             }
  31.         }
  32.     }

  33.     // 在表尾插入元素并检查是否出错
  34.     scanf("%d", &X);
  35.     L = Insert(L, X, NULL);
  36.     if (L == ERROR) {
  37.         printf("Wrong Answer\n");
  38.         return 0;
  39.     } else {
  40.         printf("%d is inserted as the last element.\n", X);
  41.     }

  42.     // 尝试在错误的位置插入和删除元素
  43.     P = (Position)malloc(sizeof(struct LNode));
  44.     tmp = Insert(L, X, P);
  45.     if (tmp != ERROR) {
  46.         printf("Wrong Answer\n");
  47.         return 0;
  48.     }
  49.     tmp = Delete(L, P);
  50.     if (tmp != ERROR) {
  51.         printf("Wrong Answer\n");
  52.         return 0;
  53.     }

  54.     // 打印最终结果
  55.     for (P = L; P != NULL; P = P->Next) {
  56.         printf("%d ", P->Data);
  57.     }
  58.     printf("\n");

  59.     return 0;
  60. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-1 21:28:44 | 显示全部楼层    本楼为最佳答案   
下面是一个可以运行上面提供的代码的 `main` 函数:


  1. int main()
  2. {
  3.     List L = NULL;
  4.     ElementType X;
  5.     Position P, tmp;
  6.     int N;

  7.     // 插入元素并检查是否出错
  8.     scanf("%d", &N);
  9.     for (int i = 0; i < N; i++) {
  10.         scanf("%d", &X);
  11.         L = Insert(L, X, L);
  12.         if (L == ERROR) {
  13.             printf("Wrong Answer\n");
  14.             return 0;
  15.         }
  16.     }

  17.     // 删除指定元素并打印结果
  18.     scanf("%d", &N);
  19.     for (int i = 0; i < N; i++) {
  20.         scanf("%d", &X);
  21.         P = Find(L, X);
  22.         if (P == ERROR) {
  23.             printf("Finding Error: %d is not in.\n", X);
  24.         } else {
  25.             L = Delete(L, P);
  26.             printf("%d is found and deleted.\n", X);
  27.             if (L == ERROR) {
  28.                 printf("Wrong Answer or Empty List.\n");
  29.                 return 0;
  30.             }
  31.         }
  32.     }

  33.     // 在表尾插入元素并检查是否出错
  34.     scanf("%d", &X);
  35.     L = Insert(L, X, NULL);
  36.     if (L == ERROR) {
  37.         printf("Wrong Answer\n");
  38.         return 0;
  39.     } else {
  40.         printf("%d is inserted as the last element.\n", X);
  41.     }

  42.     // 尝试在错误的位置插入和删除元素
  43.     P = (Position)malloc(sizeof(struct LNode));
  44.     tmp = Insert(L, X, P);
  45.     if (tmp != ERROR) {
  46.         printf("Wrong Answer\n");
  47.         return 0;
  48.     }
  49.     tmp = Delete(L, P);
  50.     if (tmp != ERROR) {
  51.         printf("Wrong Answer\n");
  52.         return 0;
  53.     }

  54.     // 打印最终结果
  55.     for (P = L; P != NULL; P = P->Next) {
  56.         printf("%d ", P->Data);
  57.     }
  58.     printf("\n");

  59.     return 0;
  60. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 02:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表