YOJ2990-链表的基本运算

题目描述

编写一个程序,实现链表的各种基本运算(假设顺序表的元素类型为 char),主函数已给出,请补充每一种方法。

  1. 初始化单链表 LLL;
  2. 采用尾插法插入一个元素;
  3. 输出单链表 LLL;
  4. 输出单链表 LLL 的长度;
  5. 判断单链表 LLL 是否为空;
  6. 输出单链表 LLL 的第三个元素;
  7. 输出元素 aaa 的位置;
  8. 在第四个元素位置插入元素 fff;
  9. 输出单链表 LLL;
  10. 删除L的第三个元素;
  11. 输出单链表 LLL;
  12. 释放单链表 LLL。

数据元素类型定义为

typedef char ElemType;

顺序表的定义为

typedef struct Node
{
    ElemType data;
    struct Node *next;
} SqList;

主函数:

int main()
{
    SqList *L;
    InitList(L);                            //初始化单链表
    ElemType a, b, c, d, e;
    scanf("%c %c %c %c %c%*c", &a, &b, &c, &d, &e);
    Insert(L,a);
    Insert(L, b);
    Insert(L, c);
    Insert(L, d);
    Insert(L, e);                           // 使用尾插法插入元素 a, b, c, d, e
    Print(L);                               // 输出单链表
    PrintLength(L);                         // 输出单链表长度
    if (SqNull(L))
        printf("单链表不为空\n");
    else printf("单链表为空\n");              // 判断单链表是否为空
    PrintData(L, 3);                        // 输出第三个元素
    printf("元素a的位置:%d\n", Find(L, a));  // 输出元素 a 的位置
    ElemType f;
    scanf("%c", &f);
    Insertinto(L, 4, f);                    // 将 f 插入到第四个位置
    Print(L);                               // 输出单链表
    Delete(L, 3);                           // 删除第三个元素
    Print(L);                               // 输出单链表
    free(L);                                // 释放内存
    return 0;
}

输入

第一行输入五个元素 a,b,c,d,ea, b, c, d, ea,b,c,d,e;接下来输入元素 fff;请根据题目编写算法。

输出

按题目要求输出。

输入输出样例

样例输入 #1

1 2 3 4 5
6

样例输出 #1

1 2 3 4 5
5
单链表不为空
3
元素a的位置:1
1 2 3 6 4 5
1 2 6 4 5

【代码】

#include
typedef char ElemType;
typedef struct Node
{
    ElemType data;
    struct Node *next;
} SqList;
void InitList(SqList *&L)
{
    L=(SqList*)malloc(sizeof(SqList));
    L->next=NULL;
}
void Insert(SqList *&L,ElemType a)
{        
    SqList *p=L,*t;
    while(p->next!=NULL)
    {
        p=p->next;
    }
    t=(SqList*)malloc(sizeof(SqList));
    p->next=t;
    t->data=a;
    t->next=NULL;

void Print(SqList *l)
{
    SqList *p;
    p=l->next;
    while(p->next!=NULL)
    {
        printf("%c ",p->data);
        p=p->next;
    }
    printf("%c\n",p->data);
}  
void PrintLength(SqList *l)
{
    SqList *p;
    p=l->next;
    int sum=0;
    while(p!=NULL)
    {
        sum++;
        p=p->next;
    }
    printf("%d\n",sum);
    return;
}
void PrintData(SqList *l,int n)
{
    SqList *p;
    p=l->next;
    int sum=0;
    while(p!=NULL)
    {
        sum++;
        if(sum==n)
        {
            printf("%c\n",p->data);
            return;
        }
        p=p->next;
    }
    return;
}
void Insertinto(SqList *&L,int n,ElemType m)
{
    SqList *pre1=L->next,*pre2,*p;
    p=(SqList*)malloc(sizeof(SqList));
    p->data=m;
    int sum=0;
    while(pre1!=NULL)
    {
        sum++;
        if(sum         {
            pre2=pre1;
            pre1=pre1->next;
        }
        if(sum==n)
        {
            p->next=pre2->next;
            pre2->next=p;
            break;
        }
    }
}
void Delete(SqList *&L,int n)
{
    SqList *p,*pre;
    p=L->next;
    int sum=0;
    while(p!=NULL)
    {
        sum++;
        if(sum         {
            pre=p;
            p=p->next;
        }
        if(sum==n)
        {
            pre->next=p->next;
            free(p);
            break;
        }
    }
}
bool SqNull(SqList *L)
{
    if(L->next!=NULL)return true;
    else return false;
}
int Find(SqList *L,ElemType a)
{
    SqList *p=L->next;
    for(int i=0; p!=NULL; i++)
        if(p->data==a)return i+1;
    return 0;
}
int main()
{
    SqList *L;
    InitList(L);                            //初始化单链表
    ElemType a, b, c, d, e;
    scanf("%c %c %c %c %c%*c", &a, &b, &c, &d, &e);
    Insert(L,a);
    Insert(L, b);
    Insert(L, c);
    Insert(L, d);
    Insert(L, e);                           // 使用尾插法插入元素 a, b, c, d, e
    Print(L);                               // 输出单链表
    PrintLength(L);                         // 输出单链表长度
    if (SqNull(L))
        printf("单链表不为空\n");
    else printf("单链表为空\n");              // 判断单链表是否为空
    PrintData(L, 3);                        // 输出第三个元素
    printf("元素a的位置:%d\n", Find(L, a));  // 输出元素 a 的位置
    ElemType f;
    scanf("%c", &f);
    Insertinto(L, 4, f);                    // 将 f 插入到第四个位置
    Print(L);                               // 输出单链表
    Delete(L, 3);                           // 删除第三个元素
    Print(L);                               // 输出单链表
    free(L);                                // 释放内存
    return 0;
}

你可能感兴趣的:(c++,c#)