站点图标 陌路寒暄

链表的基本运算(线性表)

链表的基本运算(线性表)

时间: 1ms        内存:128M

描述:

编写一个程序,实现链表的各种基本运算(假设顺序表的元素类型为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;
}

输入:

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

输出:

示例输入:

1 2 3 4 5
6

示例输出:

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

提示:

参考答案(内存最优[0]):

/*烟台大学计算机学院 2016
作者: 马春澎
题目名称:OJ 2990 Problem  E 链表的基本运算(线性表)
完成日期:2017年3月24日 */

#include <iostream>
#include<stdio.h>
#include<malloc.h>
#define SizeMax 50
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,char x)
{
    SqList *s,*p;
    p=L;
    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)
    {
        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)
{
    if(L->next==NULL)
        return 0;
    else return 1;
}
bool PrintData(SqList *&L,int i)
{
    int j=0;
    SqList *p=L;
    while(j<i&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)

        return false;
    else
    {
        printf("%c\n",p->data);
        return true;
    }

}
bool Find(SqList *&L,char 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;
}
bool  Insertinto(SqList *&L,int i,ElemType f)
{
    int j=0;
    SqList *s,*p=L;
    i--;
    while(j<i&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
        return false;
    else
    {
        s=(SqList *)malloc(sizeof(SqList));
        s->data=f;
        s->next=p->next;
        p->next=s;
        return true;
    }
}
bool Delete(SqList *&L,int i)
{
    int j=0;
    SqList *q,*p=L;
    while(j<i&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
        return false;
    else
    {
        q=p->next;
        if(q==NULL)
            return false;
        p->next=q->next;
        free(q);
        return true;
    }
}
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]):


#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 n)
{
    SqList *p=L,*q;
    while(p->next!=NULL)
        p=p->next;
    q=p;
    p=(SqList*)malloc(sizeof(SqList));
    q->next=p;
    p->data=n;
    p->next=NULL;
}
void Print(SqList *L)
{
    SqList *p=L->next;
    while(p->next!=NULL)
    {
        printf("%c ",p->data);
        p=p->next;
    }printf("%c\n",p->data);
}
void PrintLength(SqList *L)
{
    int i=0;
    SqList *p=L->next;
    while(p!=NULL)
    {
        i++;
        p=p->next;
    }
    printf("%d\n",i);
}
bool SqNull(SqList *L)
{
    if(L->next!=NULL)return true;
    else return false;
}
void PrintData(SqList *L,int n)
{
    SqList *p=L->next;
    int i;
    for(i=0;i<n-1&&p->next!=NULL;i++)
        p=p->next;
    printf("%c\n",p->data);
}
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;
}
void Insertinto(SqList *&L,int n,ElemType f)
{
    SqList *p=L->next,*q;
    for(int i=1;p!=NULL;i++)
    {
        if(i==n-1)
        {
            q=(SqList*)malloc(sizeof(SqList));
            q->data=f;
            q->next=p->next;
            p->next=q;
        }
        p=p->next;
    }
}
void Delete(SqList *&L,int n)
{
    SqList *p=L->next,*q;

    for(int i=1;p!=NULL;i++)
    {
        if(i==n-1)
        {
            q=p->next;
            p->next=p->next->next;
            free(q);
        }
        p=p->next;
    }
}
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;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版