N—E—E 2021-09-29 12:31 采纳率: 59.5%
浏览 71
已结题

这个链表实现哪里有误

终端没有任何输出也没有输出完成显示

#include <stdbool.h>
struct Node;
typedef struct Node *List;
typedef struct Node *Position;


bool IsEmpty(List L);
bool IsLast(Position P);
char GetKey(Position P);
int GetValue(Position P);
void SetKey(Position P,char);
void SetValue(Position P,int);
void Advance(Position P1,Position P2);
Position Find(char key,List L);
void Delete(char name,List L);
Position FindPrevious(char name,List L);
void Insert(char name,int value,List L,Position P);  //将新节点\
                                             插入p之后
void show(List L);
        
    


struct Node
{
    char key;
    int value;
    Position next;

};

#include "Linklist.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

bool IsEmpty(List L){
    return L->next == NULL;
}
bool IsLast(Position P){
    return P->next == NULL;
}
void Advance(Position P1,Position P2){
    P1->next=P2;
       
}
char GetKey(Position P){
    return P->key;
}
int GetValue(Position P){
    return P->value;
}
void SetKey(Position P,char name){
    P->key=name;
}
void SetValue(Position P,int value){
    P->value=value;
}
Position Find(char key,List L){
    Position P;
    P=L->next;
    while (P != NULL && P->key!=key)    
    {
        if (!IsLast(P))
            P=P->next;
    }
    if (IsLast(P))    //为什么要加括号?
        return NULL;
    else
        return P;
    
}
void Delete(char name,List L){
    Position P,temp;
    P=FindPrevious(name,L);
    if (P != NULL)
    {
        temp=P->next;
        P->next=temp->next;
        free(temp);
    }
    else
    {
        printf("Node doesn't exist!\n");
    }

}
Position FindPrevious(char name,List L){
    Position P;
    P=L;
    while (P->next != NULL && P->next->key!=name)
    {
        P=P->next;
    }
    if (IsLast(P))    //为什么要加括号?
        return NULL;
    else
        return P;


}
void Insert(char name,int value,List L,Position P){
    Position new;
    new=malloc(sizeof(struct Node));
    if (new==NULL)
        printf("Out of space!!!");   //不知道error咋用。。
    new->key=name;
    new->next=P->next;
    P->next=new;
}
void show(List L){
    Position P;
    printf("%-5s%-5s","key","value");
    if (IsEmpty(L))
        printf("please set some nodes.\n");
    else
        P=L->next;
        while (!IsLast(P))
        {
            printf("%-5s%-5d\n",P->key,P->value);
            P=P->next;
        }
}


int main()
{


    struct Node *p0,*p1,*p2;
    SetKey(p0,'a');
    SetKey(p1,'b');
    SetKey(p2,'c');
    SetValue(p0,15);
    SetValue(p1,29);
    SetValue(p0,37);
    

    Advance(p0,p1);
    Advance(p1,p2);
    Advance(p2,NULL);
    show(p0);
    return 0;
}

  • 写回答

4条回答 默认 最新

  • qzjhjxj 2021-09-29 13:50
    关注

    修改如下,供参考:

    #include <stdbool.h>
    struct Node;
    typedef struct Node* PtrToNode;
    typedef PtrToNode List;
    typedef PtrToNode Position;
    
    bool IsEmpty(List L);
    bool IsLast(Position P);
    char GetKey(Position P);
    int  GetValue(Position P);
    void SetKey(Position P, char);
    void SetValue(Position P, int);
    void Advance(Position P1, Position P2);
    Position Find(char key, List L);
    void Delete(char name, List L);
    Position FindPrevious(char name, List L);
    void Insert(char name, int value, List L, Position P);  //将新节点 ,插入p之后
    void show(List L);
    
    
    struct Node
    {
        char key;
        int  value;
        Position next;
    };
    #include "Linklist.h"
    #include <stddef.h>
    #include <stdlib.h>
    #include <stdio.h>
    bool IsEmpty(List L) {
        return L->next == NULL;
    }
    bool IsLast(Position P) {
        return P->next == NULL;
    }
    void Advance(Position P1, Position P2) {
        P1->next = P2;
    }
    char GetKey(Position P) {
        return P->key;
    }
    int GetValue(Position P) {
        return P->value;
    }
    void SetKey(Position P, char name) {
        P->key = name;
    }
    void SetValue(Position P, int value) {
        P->value = value;
    }
    Position Find(char key, List L) {
        Position P;
        P = L->next;
        while (P != NULL && P->key != key)
        {
            if (!IsLast(P))
                P = P->next;
        }
        if (IsLast(P))    //为什么要加括号? IsLast()是函数
            return NULL;
        else
            return P;
    }
    void Delete(char name, List L) {
        Position P, temp;
        P = FindPrevious(name, L);
        if (P != NULL)
        {
            temp = P->next;
            P->next = temp->next;
            free(temp);
        }
        else
        {
            printf("Node doesn't exist!\n");
        }
    }
    Position FindPrevious(char name, List L) {
        Position P;
        P = L;
        while (P->next != NULL && P->next->key != name)
        {
            P = P->next;
        }
        if (IsLast(P))    //为什么要加括号?IsLast()是函数
            return NULL;
        else
            return P;
    
    }
    void Insert(char name, int value, List L, Position P) {
        Position new_m;  //new 是关键字,改为 new_m
        new_m = (Position)malloc(sizeof(struct Node));
        if (new_m == NULL)
            printf("Out of space!!!");   //不知道error咋用。。申请空间不成功时,提示信息。
        new_m->key = name;
        new_m->next = P->next;
        P->next = new_m;
    }
    void show(List L) {
        Position P;
        printf("%-5s%-5s\n", "key", "value");
        //printf("%-5s%-5s", "key", "value");
        if (IsEmpty(L))
            printf("please set some nodes.\n");
        else
            P = L;//P = L->next;
        while (P){  //!IsLast(P)
            printf("%-5c%-5d\n", P->key, P->value);
            //printf("%-5s%-5d\n", P->key, P->value);
            P = P->next;
        }
    }
    
    int main()
    {
    
        struct Node* p0 = nullptr, * p1 = nullptr, * p2 = nullptr;
        p0 = (Position)malloc(sizeof(struct Node));
        p1 = (Position)malloc(sizeof(struct Node));
        p2 = (Position)malloc(sizeof(struct Node));
        SetKey(p0, 'a');
        SetKey(p1, 'b');
        SetKey(p2, 'c');
        SetValue(p0, 15);
        SetValue(p1, 29);
        SetValue(p2, 37);
    
        Advance(p0, p1);
        Advance(p1, p2);
        Advance(p2, NULL);
        show(p0);
        return 0;
    }
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月7日
  • 已采纳回答 9月29日
  • 提问应符合社区要求 9月29日
  • 创建了问题 9月29日

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM