数据结构第2章上机实验题2.1

来源:互联网 发布:唐青枫捏脸数据 编辑:程序博客网 时间:2024/06/05 00:22

问题描述:实现顺序表的基本运算(1-12具体要求见课本P62),作为对已经学过的顺序表的小revision~

源代码:

list.h:

#include <stdio.h>#include <malloc.h>#define max 100typedef char Elemtype;typedef struct list{    char data[max];    int length;} Sqlist;void Initlist(Sqlist *&l);void Createlist(Sqlist *&l,Elemtype a[],int n);void Displist(Sqlist *l);int Listlength(Sqlist *l);bool Listempty(Sqlist *l);void Dispelement(Sqlist *l,int loc);void Disploc(Sqlist *l,Elemtype e);bool Insertelement(Sqlist *&l,int loc,Elemtype e);bool Deleteelement(Sqlist *&l,int loc,Elemtype &e);void Destroylist(Sqlist *&l);

fun.cpp:

#include <stdio.h>#include "list.h"void Initlist(Sqlist *&l){    l=(Sqlist *)malloc(sizeof(Sqlist));    l->length=0;}void Createlist(Sqlist *&l,Elemtype a[],int n){    int i;    l=(Sqlist *)malloc(sizeof(Sqlist));    for(i=n-1;i>=0;i--)        l->data[i]=a[i];    l->length=n;}void Displist(Sqlist *l){    int i;    for(i=0;i<l->length-1;i++)        printf("%c ",l->data[i]);    printf("%c\n",l->data[l->length-1]);}void Dispelement(Sqlist *l,int loc){    int i;    for(i=0;i<l->length;i++)    {        if(i==loc-1)            printf("%c\n",l->data[i]);    }}void Disploc(Sqlist *l,Elemtype e){    int i;    int flag=0;                  //判断是否找到,flag=1找到,flag=0未找到    for(i=0;i<l->length;i++)    {        if(l->data[i]==e)        {            flag++;            printf("%d\n",i+1);        }    }    if(flag==0)        printf("未找到该元素\n");}int Listlength(Sqlist *l){    return (l->length);}bool Listempty(Sqlist *l){    if(l->length!=0)        return true;    else        return false;}bool Insertelement(Sqlist *&l,int loc,Elemtype e){    int i;    if(loc<1 || loc>l->length+1 || l->length==max)        return false;    loc--;    for(i=l->length;i>loc;i--)        l->data[i]=l->data[i-1];    l->data[loc]=e;    l->length++;    return true;}bool Deleteelement(Sqlist *&l,int loc,Elemtype &e){    int i;    if(loc<1 || loc>l->length+1)        return false;    loc--;    e=l->data[loc];    for(i=loc;i<l->length-1;i++)        l->data[i]=l->data[i+1];    l->length--;    return true;}void Destroylist(Sqlist *&l){    free(l);}

main.cpp:

#include <stdio.h>#include "list.h"int main(){    Sqlist *l;    Elemtype a[5]={'a','b','c','d','e'};       //5个元素a,b,c,d,e    Elemtype e;    Initlist(l);                               //初始化顺序表L    printf("顺序表已被初始化!\n");    if(Listempty(l))                           //判断顺序表L是否为空        printf("此顺序表不为空\n");    else        printf("此顺序表为空\n");    Createlist(l,a,5);                         //尾插法依次插入a,b,c,d,e    printf("尾插法插入元素后输出的顺序表为:\n");    Displist(l);                               //输出顺序表L    printf("此顺序表长度为:");    printf("%d\n",Listlength(l));              //输出顺序表L长度    if(Listempty(l))                           //判断顺序表L是否为空        printf("此顺序表不为空\n");    else        printf("此顺序表为空\n");    printf("顺序表L的第3个元素为:");    Dispelement(l,3);    printf("元素a的位置为:");    Disploc(l,'a');    Insertelement(l,4,'f');                    //在第4个元素位置上插入元素f    printf("插入元素后的顺序表为:\n");    Displist(l);                               //输出顺序表L    Deleteelement(l,3,e);                      //删除L的第3个元素    printf("删除元素后的顺序表为:\n");    Displist(l);                               //输出顺序表L    Destroylist(l);                            //释放顺序表L    printf("顺序表已被销毁!\n");    return 0;}

运行结果:


0 0
原创粉丝点击