oj Problem E: 链表的基本运算(线性表)

来源:互联网 发布:震惊国外网络购物视频 编辑:程序博客网 时间:2024/06/05 10:57

问题:

Description

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

 

1、初始化单链表L;

2、采用尾插法插入一个元素;

3、输出单链表L;

4、输出单链表L的长度;

5、判断单链表L是否为空;

6、输出单链表L的第三个元素;

7、输出元素a的位置;

8、在第四个元素位置插入元素f;

9、输出单链表L;

10、删除L的第三个元素;

11、输出单链表L;

12、释放单链表L;

    

数据元素类型定义为
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;
}

Input

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

Output

Sample Input

1 2 3 4 56

Sample Output

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

HINT

请使用C++编译并提交

代码:

#include <iostream>#include <stdio.h>#include <stdlib.h>#define SizeMax 80 using namespace std;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 x){    SqList *s=L,*r;    while(s->next!=NULL)        s=s->next;    r=s;    s=(SqList *)malloc(sizeof(SqList));    s->data=x;    r->next=s;    r=s;    r->next=NULL;}void Print(SqList *L){    SqList *p=L->next;    while(p!=NULL)    {        printf("%c ",p->data);        p=p->next;    }    printf("\n");}void PrintLength(SqList *L){    int n=0;    SqList *p=L;    while(p->next!=NULL)    {        n++;        p=p->next;    }    printf("%d\n",n);}bool SqNull(SqList *L){    return(L->next!=NULL);}void PrintData(SqList *L,int a){    int j=0;    SqList *p=L;    while(j<a&&p!=NULL)    {        j++;        p=p->next;    }    printf("%c\n",p->data);}int Find(SqList *L,ElemType x){    int i=1;    SqList *p=L->next;    while(p!=NULL&&p->data!=x)    {        p=p->next;        i++;    }    if(p==NULL)        return 0;    else        return i;}void Insertinto(SqList *&L,int a,ElemType x){    int j=0;    SqList *p=L,*s;    while(j<a-1&&p!=NULL)    {        j++;        p=p->next;    }    s=(SqList *)malloc(sizeof(SqList));    s->data=x;    s->next=p->next;    p->next=s;}void Delete(SqList *&L,int a){    int j=0;    SqList *p=L,*s;    while(j<a-1&&p!=NULL)    {        j++;        p=p->next;    }    s=p->next;    p->next=s->next;    free(s);}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;}

小结:

链表的基本操作。


0 0
原创粉丝点击