Sequential storage structure of linear table and implementation of operation

Requirements: use the sequential storage structure definition of the linear table in the book:

1) Write the following functions: (1) initialize a linear table; (2) On the basis of initialization, create a linear table containing 15 positive integer values not greater than 100 (15 values are randomly generated by the computer); (3) Insert a number x before the ith element (x and i are input when the program is running); (4) Delete the ith element (i is input when the program is running), and output the value of the deleted element after deletion; (5) Find out whether the given value x is in the linear table (x is input when the program is running). If yes, output the position where x appears for the first time in the linear table. If not, output the prompt that x is not in the table; (6) Outputs all elements in the linear table.

2) It is required to compile a menu, call various functions to execute according to the options, and output the contents of the linear table at each step of changing the linear table, so as to verify the correctness of your programming.

Remarks: (1) in the header file stdlib In H, srand() accepts the seeds of random numbers and rand () generates 0 ~ RAND_MAX is a function of a random integer. Use rand()% 100 + 1 to generate a positive integer value no greater than 100. (2) The program should be robust, that is, when inserting and deleting, the table should be empty, full, and whether the position is legal. When the input data is illegal, the program can also respond appropriately without inexplicable results.

Steps:

1. Initialize a linear table;

2. Use srand() to accept the seed of random number and rand() to randomly generate a linear table containing 15 positive integer values not greater than 100, and output the value of the linear table to enable the operator to observe whether this step is completed;

3. Pass the inserted number x and the insertion position i into the function, and judge whether the insertion position i is legal. If it is legal, move all the numbers after i back one bit, and empty the position to x, so that the number x is inserted into the unknown i. if it is not legal, an error will be reported;

4. Pass the deleted position i into the function, first judge whether i is legal, if it is legal, move all elements after position i forward one bit to achieve the purpose of deletion, and then assign the deleted number to an unknown number and output it. If it is not legal, an error will be reported;

5. Move backward one by one from the first number of the linear table and find out whether it is equal to the given value X. if it is equal, it will be in the linear table (x is input when the program is running) and output the position where x appears for the first time in the linear table. If it is not in the output, X is not in the table;

6. Use the for loop to output all elements in the linear table one by one.

7. Write a menu to facilitate the user to call each function according to the options

8. Use switch() to perform the user's operation.

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#define   TRUE         1
#define   FALSE        0
#define   OK           1
#define   ERROR        0
#define   OVERFLOW     -2
#define   MAXSIZE      100
typedef int ElemType;
typedef int Status;
typedef struct SQLIST{
    ElemType *elem;
    int length;
}SqList;

Status InitList(SqList &L)
{
    L.elem=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
    if(!L.elem) return ERROR;
    L.length=0;
    return OK;
}//Create initial linear table
Status ValueList(SqList &L)
{
    srand(time(0));
    for(int i=0;i<15;i++)
    {
        L.elem[i]=rand()%100+1;
        L.length+=1;
    }
    printf("The elements in the linear table are:");
    for(int i=0;i<L.length;i++)
    {
        printf("%4d",L.elem[i]);
    }
    printf("\n");
    return OK;
}//Assignment initial linear table
Status InsertList(SqList &L,int i,int x)
{
    if(i<1||i>(L.length+1))
    {
        printf("Unknown or wrong program you entered!");
        return ERROR;
    }
    for(int j=L.length;j>=i;j--)
    {
        L.elem[j]=L.elem[j-1];
    }
    L.elem[i-1]=x;
    L.length++;
    printf("The elements in the linear table are:");
    for(int i=0;i<L.length;i++)
    {
        printf("%4d",L.elem[i]);
    }
    printf("\n");
    return OK;
}//Insert a number x before the ith element (x and i are entered when the program is running)
Status DestroyElem(SqList &L,int i)
{
    int y,j;
    if(i<1||i>L.length)
    {
        printf("Unknown or wrong program you entered!");
        return ERROR;
    }
    y=L.elem[i-1];
    for(j=i;j<=L.length;j++)
    {
        L.elem[j-1]=L.elem[j];
    }
    L.length--;
    printf("The elements in the linear table are:");
    for(int i=0;i<L.length;i++)
    {
        printf("%4d",L.elem[i]);
    }
    printf("\n");
    printf("Deleted elements are numbers:%d\n",y);
    return OK;
}//Delete and output the i th element
Status FindElem(SqList &L,int x)
{
    int i;
    for(i=0;i<L.length;i++)
    {
        if(L.elem[i]==x)
        {
            printf("The element is in a linear table, and the element%d The location of the is%d\n",x,(i+1));
            break;
        }
    }
    if(i==L.length) printf("element%d Not in linear table\n",x);
    return OK;
}//Finds whether the given value x is in the linear table
Status PutList(SqList &L)
{
    for(int i=0;i<L.length;i++)
    {
        printf("The first%2d Elements:%4d\n",(i+1),L.elem[i]);
    }
}//Output all elements in linear table
int menu()
{
    printf("Menu:\n");
    printf("1.Initialize a linear table\n2.Assignment initial linear table\n3.Insert element\n4.Delete element\n5.Find element\n6.Output linear table\n7.End program\n");
    printf("Please enter the project you want to implement:");
}
int main()
{
    int option,location,element;
    SqList List;
    InitList(List);
    menu();
    scanf("%d",&option);
    while(option!=7){
    switch(option)
	{
    case 1:
        printf("1.Linear table initialization completed!\n");
        break;
	case 2:
		printf("2:Assignment initial linear table\n");
		ValueList(List);
		break;
	case 3:
		printf("3:Insert a number\n");
		printf("Insert element location:");
		scanf("%d",&location);
		printf("Insert element value:");
		scanf("%d",&element);
		InsertList(List,location,element);
		break;
	case 4:
		printf("4:Delete a number\n");
		printf("Delete element location:");
		scanf("%d",&location);
		DestroyElem(List,location);
		break;
	case 5:
		printf("5:Find element\n");
		scanf("%d",&element);
		FindElem(List,element);
		break;
	case 6:
	    printf("6.Output element:\n");
		PutList(List);
		break;
    default:
        printf("Input menu name error\n");
        break;
	}
	menu();
    scanf("%d",&option);
	};
	printf("The program has ended, bye!");
	return 0;
}

Keywords: data structure

Added by xudzh on Wed, 05 Jan 2022 18:49:36 +0200