数据结构—Problem E: 链表的基本运算(线性表)

来源:互联网 发布:淘宝库存监控软件 编辑:程序博客网 时间:2024/06/06 19:30

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

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 120  Solved: 60
[Submit][Status][Web Board]

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 5

1 2 6 4 5

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