当前位置:网站首页>Basic operation of sequence table

Basic operation of sequence table

2022-06-24 20:27:00 Learning The Monk

Catalog

One 、 The experimental requirements

Two 、 Code implementation

3、 ... and 、 Running results


One 、 The experimental requirements


    1、 Confirmatory experiments : Realize the basic operation of sequence table
Experimental content : Write a program sqlist.cpp ( or .c),
        Realize various basic operations of the sequence table and the overall table building algorithm ( Suppose the content type of the sequence table ElemType by char),
        On this basis, a program is designed exp1.cpp ( or .c) Complete the function .
(1)   Initialization sequence table L.
(2)   Put the element a、b、c、d、e Insert sequence table in sequence L in .
(3)   Output sequence table L.
(4)   Output sequence table L The length of .
(5)   Judgment sequence table L Is it empty .
(6)   Output sequence table L Of the 3 Elements .
(7)   Output elements a The location of .
(8)   In the 4 Insert elements... At element locations f.
(9)   Output sequence table L.
(10) Delete order table L Of the 3 Elements .
(11) Output sequence table L.
(12) Destruction sequence table L.


 

Two 、 Code implementation

/*
    1、 Confirmatory experiments : Realize the basic operation of sequence table 
 Experimental content : Write a program sqlist.cpp ( or .c),
         Realize various basic operations of the sequence table and the overall table building algorithm ( Suppose the content type of the sequence table ElemType by char),
         On this basis, a program is designed exp1.cpp ( or .c) Complete the function .
(1)   Initialization sequence table L.
(2)   Put the element a、b、c、d、e Insert sequence table in sequence L in .
(3)   Output sequence table L.
(4)   Output sequence table L The length of .
(5)   Judgment sequence table L Is it empty .
(6)   Output sequence table L Of the 3 Elements .
(7)   Output elements a The location of .
(8)   In the 4 Insert elements... At element locations f.
(9)   Output sequence table L.
(10)  Delete order table L Of the 3 Elements .
(11)  Output sequence table L.
(12)  Destruction sequence table L.
*/

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100 		// Maximum allocated capacity of linear table 
#define LISTINCREMENT 10 		// Linear table expansion capacity 
typedef char ElemType;  // The data element is of character type 

typedef struct 
{
    /* data */
    ElemType *data;	
    int length;
    int listsize;
}sqlist;

// Initialization sequence table 
int InitList(sqlist *L)
{
	// Dynamic allocation :malloc Function to apply for a continuous piece of storage space 
    L->data=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L->data)
	{
		return ERROR;
	}
    L->length=0;			// The initial table length is 0
    L->listsize=LIST_INIT_SIZE;
	return OK;
}

// Put the element e Insert into the sequence table L Of the k A place .
int List_Insert(sqlist *L,int k,ElemType e)
{
    int i=0;
    if(!L->data)
    return ERROR;
    if (k>L->length+1|| k<1)// Determine the legitimacy of the insertion position 
    {
        return ERROR;
    }
  if (L->length >= L->listsize)
	{
		char *new_base;   // The space is insufficient , Redistribution 
		new_base = (char*)realloc(L->data, sizeof(char)*(L->listsize + LISTINCREMENT));
		L->data = new_base;
		if (!L->data)
		{
			return ERROR;
		}
		L->data = new_base;
		L->listsize += LISTINCREMENT; 
	}
	// Insert elements e
	if(k<L->length)
	{
		for (i = L->length - 1 ; i>= k - 1; i--)
			L->data[i + 1] = L->data[i];
	}
	L->data[k - 1] = e;
	L->length++;                // One more element length plus 1
	return OK;
}
// Output sequence table L.
int print_List_data(sqlist *L)
{
    if(!L->data)
    return ERROR;
    for (int i = 0; i < L->length; i++)
    {
        /* code */
        printf("%c  ",L->data[i]);
    }
	printf("\n");
    return OK;
}

// Output sequence table L The length of .
int print_List_Length(sqlist *L)
{
    if(!L->data)
        return ERROR;
    printf("%d\n",L->length);
    return OK;
}
// Judgment sequence table L Is it empty .
int ListEmpty(sqlist *L)
{
    if(!L->data||L->length!=0)
        return ERROR;
    else
        return OK;
}
// Output sequence table L Of the i Elements .( This element e It needs to be returned to the user )
int GetElem(sqlist *L, int i, ElemType *e)
{
	if (!L->data)
	{
		return ERROR;
	}
	if (i < 1 || i >= L->length-1)
	{
		return ERROR;     //i Illegal value 
	}
	*e =L->data[i-1];
	return OK;
}

// Output elements a The location of .( This element e It does not need to be returned to the user )
int LocateElem(sqlist *L, char e)
{
	if (!L->data)
	{
		return ERROR;
	}
    for (int i = 0; i < L->length; i++)
    {
       if (L->data[i]==e)
        {
            return i+1;     // Returns the location of the element 
        }
    }
	return OK;
}
//  Delete order table L Of the i Elements .( This element e It does not need to be returned to the user )
int ListDelete(sqlist *L, int i, char e)
{
	int k;
	if (!L->data)
	{
		return ERROR;
	}
	if (i<1 || i>L->length)
	{
		return ERROR;  //i Illegal value 
	}
	e = L->data[i - 1];
	for (k = i; k <= L->length ;k++)  
		L->data[k - 1] = L->data[k];
		L->length--;        
	return OK;
}
//  Destruction sequence table L.
int DestroyList(sqlist *L)
{
	if (!L->data)
	{
		return ERROR;  // Table does not exist 
	}
	else
	free(L->data);      // Free memory   Destroy the linear table 
	printf("	 Linear table destroyed successfully !");
	return 0;
}


int main()
{
	sqlist L;
	ElemType e;
	printf("1: Initialization sequence table L\n");
	InitList(&L);
	printf("2: Insert... By tail interpolation in turn a , b , c , d , e Elements \n");
	List_Insert(&L, 1, 'a');
	List_Insert(&L, 2, 'b');
	List_Insert(&L, 3, 'c');
	List_Insert(&L, 4, 'd');
	List_Insert(&L, 5, 'e');
	printf("3: The order sheet L:");
	print_List_data(&L);
	printf("4: The linear table L The watch is :");
	print_List_Length(&L);
	printf("5: The order sheet L by :%s\n", (ListEmpty(&L) ? " empty " : " Non empty "));
	GetElem(&L, 3, &e);
	printf("6: The first in the sequence 3 The element is :%c\n", e);
	printf("7: The linear table L Medium element a The location of the for : %d\n", LocateElem(&L, 'a'));
	printf("8: In the 4 Insert at element locations f Elements \n");
	List_Insert(&L, 4, 'f');
	printf("	 The order sheet L by :");
	print_List_data(&L);
	printf("9: Delete L Of the 3 Elements \n");
	ListDelete(&L, 3, e);
	printf("	 The order sheet L by :");
	print_List_data(&L);
	printf("10: Destruction sequence table L\n");
	DestroyList(&L);
	return 0;
}

3、 ... and 、 Running results

 

 

原网站

版权声明
本文为[Learning The Monk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241412033852.html