池啾 2023-03-18 15:06 采纳率: 76.3%
浏览 40
已结题

实现单链表的基本运算


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct LinkNode
{
    char data;
    struct LinkNode *next;
}LinkNode;
void InitList(LinkNode *&L)
{
    L=(LinkNode*)malloc(sizeof(LinkNode));
    L->next=NULL;
}
void CreatList(LinkNode *&L,char a[],int n)
{
    LinkNode *s,*r;
    L=(LinkNode*)malloc(sizeof(LinkNode));
    r=L;
    for(int i=0;i<n;i++)
    {
        s=(LinkNode*)malloc(sizeof(LinkNode));
        s->data=a[i];
        r->next=s;
        r=s;
    }
    r->next=NULL;
}
void EmptyList(LinkNode *L)
{
    if(L->next==NULL)
    {
        printf("empty\n");
    }
    else
    {
        printf("not empty\n");
    }
}
void DisList(LinkNode *L)
{
    LinkNode *p=L->next;
    while(p!=NULL)
    {
        printf("%c",p->data);
        p=p->next;
    }
    printf("\n");
}
void LengthList(LinkNode *L)
{
    int n;
    LinkNode *p=L;
    while(p->next!=NULL)
    {
        n++;
        p=p->next;
    }
    printf("Length:%d\n",n);
}
void GetElem(LinkNode *L,int n)
{
    int j=0;
    LinkNode *p=L;
    if(n<=0)
    {
        printf("ERROR input\n");
    }
    else
    {
        while(j<n&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
    {
        printf("ERROR input\n");
    }
    else
    {
            printf("%c\n",p->data);
    }
}
}
void LocateList(LinkNode *L,char e)
{
    int i=1;
    LinkNode *p=L->next;
    while(p!=NULL&&p->data!=e)
    {
        p=p->next;
        i++;
    }
    if(p==NULL)
    {
        printf("ERROR input\n");
    }
    else
    {
        printf("%d\n",i);
    }
}
void ListDelete(LinkNode *&L,int n,char &e)
{
    int j=0;
    LinkNode *p=L;
    LinkNode *q;
    if(n<=0)
    {
        printf("ERROR input\n");
    }
    while(p!=NULL&&j<n-1)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
    {
        printf("ERROR input\n");
    }
    else
    {
        q=p->next;
        if(q==NULL)
        {
            printf("ERROR input\n");
        }
        e=q->data;
        p->next=q->next;
        free(q);
    }
}
void ListInsert(LinkNode *&L,int n,char &e)
{
    int j=0;
    LinkNode *p=L,*q;
    if(n<=0)
    {
        printf("ERROR input\n");
    }
    while(j<n-1&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
    {
        printf("ERROR input\n");
    }
    else
    {
        q=(LinkNode*)malloc(sizeof(LinkNode));
        q->data=e;
        q->next=p->next;
        p->next=q;
    }
}
int main()
{
    char s[100];
    char ch,sh;
    char e;
    int n,i,j,flag;
    LinkNode *L;
    int count;
    scanf("%s",s);
    count=strlen(s);
    scanf("%d\n",&n);
    scanf("%c\n",&ch);
    scanf("%d\n",&i);
    scanf("%c\n",&sh);
    scanf("%d",&j);
    InitList(L);
    EmptyList(L);
    CreatList(L,s,count);
    DisList(L);
    LengthList(L);
    EmptyList(L);
    GetElem(L,n);
    LocateList(L,ch);
    ListInsert(L,i,sh);
    DisList(L);
    ListDelete(L,j,e);
    DisList(L);
}

img

img


不知道为什么有一组数据一直通过不了5555会变成下面的结果:

img


还请各路神仙帮帮我,想好久不知道为什么55

  • 写回答

4条回答 默认 最新

  • qzjhjxj 2023-03-18 17:56
    关注

    修改如下,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct LinkNode
    {
        char   data;
        struct LinkNode *next;
    }LinkNode;
    void InitList(LinkNode *&L)
    {
        L=(LinkNode*)malloc(sizeof(LinkNode));
        L->next=NULL;
    }
    void CreatList(LinkNode *&L,char a[],int n)
    {
        LinkNode *s,*r;
        //L=(LinkNode*)malloc(sizeof(LinkNode));修改
        r = L;
        for(int i=0;i<n;i++)
        {
            s=(LinkNode*)malloc(sizeof(LinkNode));
            s->data=a[i];
            r->next=s;
            r=s;
        }
        r->next=NULL;
    }
    void EmptyList(LinkNode *L)
    {
        if(L->next==NULL)
        {
            printf("empty\n");
        }
        else
        {
            printf("not empty\n");
        }
    }
    void DisList(LinkNode *L)
    {
        LinkNode *p=L->next;
        while(p!=NULL)
        {
            printf("%c",p->data);
            p=p->next;
        }
        printf("\n");
    }
    void LengthList(LinkNode *L)
    {
        int n = 0;          //修改
        LinkNode *p=L->next;//修改
        while(p!=NULL)      //(p->next!=NULL) 修改
        {
            n++;
            p=p->next;
        }
        printf("Length:%d\n",n);
    }
    void GetElem(LinkNode *L,int n)
    {
        int j=0;
        LinkNode *p=L;
        if(n<=0)
        {
            printf("ERROR input\n");
        }
        else
        {
            while(p != NULL && j < n)
            {
                j++;
                p=p->next;
            }
            if(p==NULL)
            {
                printf("ERROR input\n");
            }
            else
            {
                printf("%c\n",p->data);
            }
        }
    }
    void LocateList(LinkNode *L,char e)
    {
        int i = 1;
        LinkNode *p = L->next;
        while(p!=NULL && p->data!=e)
        {
            p=p->next;
            i++;
        }
        if(p==NULL)
        {
            printf("ERROR input\n");
        }
        else
        {
            printf("%d\n",i);
        }
    }
    void ListDelete(LinkNode *&L,int n,char &e)
    {
        int j=0;
        LinkNode *p=L;
        LinkNode *q;
        if(n <= 0)
        {
            printf("ERROR input\n");
        }
        while(p != NULL && j<n-1)
        {
            j++;
            p=p->next;
        }
        if(p==NULL)
        {
            printf("ERROR input\n");
        }
        else
        {
            q=p->next;
            if(q==NULL)
            {
                printf("ERROR input\n");
            }
            else{
                e=q->data;
                p->next=q->next;
                free(q);
            }
        }
    }
    void ListInsert(LinkNode *&L,int n,char &e)
    {
        int j=0;
        LinkNode *p=L,*q;
        if(n <= 0)
        {
            printf("ERROR input\n");
        }
        while(p != NULL && j < n - 1)
        {
            j++;
            p=p->next;
        }
        if(p==NULL)
        {
            printf("ERROR input\n");
        }
        else
        {
            q=(LinkNode*)malloc(sizeof(LinkNode));
            q->data=e;
            q->next=p->next;
            p->next=q;
        }
    }
    void destory(LinkNode *L)
    {
        LinkNode *p = L;
        while (L)
        {
            p = L;
            L = L->next;
            free(p);
        }
    }
    
    int main()
    {
        char s[100];
        char ch,sh;
        char e;
        int n,i,j,flag;
        LinkNode *L;
        int count;
    
        scanf("%s",s);//(3)输入字符串 s
        scanf("%d",&n);//(7)输出单链表第 n 个元素
        scanf(" %c",&ch);//(8)输入一个字符ch ,查找该字符在链表中的位置
        scanf("%d %c",&i,&sh);//(9)在第 i 个位置插入元素 sh
        scanf("%d",&j);//(11)删除单链表的第 j 个元素
    
        InitList(L); //(1)初始化单链表
        EmptyList(L);//(2)判断单链表是否为空
    
        count=strlen(s);//(3)输入字符串 s
        CreatList(L,s,count);//插入链表
    
        DisList(L); //(4)输出单链表
        LengthList(L);//(5)输出单链表长度
        EmptyList(L);//(6)判断单链表是否为空
    
        GetElem(L,n);//(7)输出单链表第 n 个元素
    
        LocateList(L,ch);//(8)输入一个字符ch ,查找该字符在链表中的位置
    
        ListInsert(L,i,sh);//(9)在第 i 个位置插入元素 sh
        DisList(L);    //(10)输出单链表
    
        ListDelete(L,j,e);//(11)删除单链表的第 j 个元素
        DisList(L); //(12)输出单链表
    
        destory(L);//(13)释放单链表
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月1日
  • 已采纳回答 3月24日
  • 创建了问题 3月18日

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据