qq_53680336 2021-03-24 13:38 采纳率: 0%
浏览 42

数据结构(链表实现)

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

为什么无法实现呢?

  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-07 16:51
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 Unity 2022.3.34版本安卓打包apk失败,gradle配置问题,用的是mono2x
  • ¥15 R语言中安装bibliometrix 后运行biblioshiny出现问题
  • ¥20 关于#android#的问题:用开发助手发现找不到控件(autojs)
  • ¥15 dir815漏洞反弹shell失败
  • ¥15 支付宝小程序云函数登录获取user_id失败
  • ¥50 python for 循环速度慢
  • ¥15 CubeMX生成的代码用keil编译有报错
  • ¥15 Stata链式中介效应代码修改
  • ¥15 pip安装PyAV报错
  • ¥15 latex投稿显示click download