Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab exercise 2.1 To complete the basic operations on the sequential list ( the data element type is char), and based on the basic operations

Lab exercise 2.1

To complete the basic operations on the sequential list ( the data element type is char), and based on the basic operations to achieve the following functions:

Initiate a sequential list L;

Insert a,b,c,d,e to the list tail;

Output the list L;

Output the length of the list L;

To judge if the list is empty;

Output the 3rd element of the List;

Output the position of the element b.

//filename:algo2-1.cpp*/

#include

#include

#define MaxSize 50

typedef char ElemType;

typedef struct

{

ElemType data[MaxSize];

int length;

} SqList;

void InitList(SqList *&L) //initiate the list

{

L=(SqList *)malloc(sizeof(SqList));

L->length=0;

}

void DestroyList(SqList *L) //destroy the list

{ free(L);}

bool ListEmpty(SqList *L)

{ return(L->length==0);}

int ListLength(SqList *L)

{ return(L->length);}

void DispList(SqList *L)

{

int i;

if (ListEmpty(L)) return;

for (i=0;ilength;i++)

printf("%c ",L->data[i]);

printf(" ");

}

bool GetElem(SqList *L,int i,ElemType &e)

{

if (i<1 || i>L->length)

return false;

e=L->data[i-1]; //Get value

return true;

}

int LocateElem(SqList *L, ElemType e)

{

int i=0;

while (ilength && L->data[i]!=e)

i++;

if (i>=L->length)

return 0;

else

return i+1;

}

bool ListInsert(SqList *&L,int i,ElemType e)

{

int j;

if (i<1 || i>L->length+1)

return false;

i--;

for (j=L->length;j>i;j--)

L->data[j]=L->data[j-1];

L->data[i]=e;

L->length++;

return true;

}

bool ListDelete(SqList *&L,int i,ElemType &e)

{

int j;

if (i<1 || i>L->length)

return false;

i--;

e=L->data[i];

for (j=i;jlength-1;j++)

L->data[j]=L->data[j+1];

L->length--; return true;

}

//filename:exp2-1.cpp

#include

#include

#define MaxSize 50

typedef char ElemType;

typedef struct

{

ElemType data[MaxSize];

int length;

} SqList;

extern void InitList(SqList *&L);

extern void DestroyList(SqList *L);

extern bool ListEmpty(SqList *L);

extern int ListLength(SqList *L);

extern void DispList(SqList *L);

extern bool GetElem(SqList *L,int i,ElemType &e);

extern int LocateElem(SqList *L, ElemType e);

extern bool ListInsert(SqList *&L,int i,ElemType e);

extern bool ListDelete(SqList *&L,int i,ElemType &e);

void main()

{

SqList *L;

ElemType e;

printf("Basic operations for sequential list: ");

printf(" (1)initiate list L ");

InitList(L);

printf(" (2)insert a,b,c,d,e ");

ListInsert(L,1,'a');

ListInsert(L,2,'b');

ListInsert(L,3,'c');

ListInsert(L,4,'d');

ListInsert(L,5,'e');

printf(" (3)output list L:");

DispList(L);

printf(" (4)the length =%d ",ListLength(L));

printf(" (5) the list L is %s ",(ListEmpty(L)?" empty":"not empty"));

GetElem(L,3,e);

printf(" (6)the 3rd element is =%c ",e);

printf(" (7)The position of a =%d ", LocateElem(L,'a'));

DestroyList(L);}}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions