Programming to realize the basic operation of linear table of data structure

This code mainly implements


1 linear table creation, insertion, deletion functions, etc.


2 The main function calls the above function


#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
#include<malloc.h>
struct LinearList /*Define the linear list structure*/
{
	int *list; /* store linear list elements */
	int size; /* store the length of the linear table */
	int MaxSize; /* Store the number of elements in the list array*/
};
typedef struct LinearList LIST;

void InitList(LIST *L, int ms) /* Initialize the linear list*/
{
	if ((L->list = (int*)malloc(ms * sizeof(int))) == NULL) {
		printf("Memory allocation error!\n");
		exit(1);
	}
	L->size = 0;//  L->size=0;              
	L->MaxSize = ms;
}

int InsertList(LIST *L, int item, int rc)
/* item: record value rc: insert position */
{
	int i;
	if (L->size == L->MaxSize) /* Linear table is full*/
		return -1;
	if (rc < 0) /* insert position is 0 --> L->size */
		rc = 0;
	if (rc>L->size)
		rc = L->size;
	for (i = L->size - 1; i >= rc; i--) /* Shift linear table elements backward*/
		L->list[i + 1] = L->list[i];
	L->list[rc] = item;
	L->size++;
	return 0;
}

void OutputList(LIST *L) /* Output linear list elements*/
{
	int i;
	for (i = 0; i<L->size; i++)
		printf("%d ", L->list[i]);
	printf("\n");
}


int FindList(LIST *L, int item)/* Return >=0 for element position -1 not found*/
{
	int i;
	for (i = 0; i < L->size; i++)
	if (item == L->list[i]) /* find the same element, return the position */
		return i;
	return -1; /* not found*/
}


int DeleteList1(LIST *L, int item)
/* Delete the linear table record of the specified element value, return >=0: delete successfully*/
{
	int i, n;
	for (i = 0; i < L->size; i++)
	if (item == L->list[i]) /* find the same element */
		break;
	if (i < L->size) {
		for (n = i; n < L->size - 1; n++)
			L->list[n] = L->list[n + 1];
		L->size--;
		return i;
	}
	return -1;
}



int DeleteList2(LIST L, int rc) /* delete the linear list record at the specified position*/
{
	int i, n;
	if (rc<0 || rc >= L.size)
		return -1;
	for (n = rc; n<L.size - 1; n++)
		L.list[n] = L.list[n + 1];
	L.list--;
	return 0;
}

intmain()
{
	LIST L1;
	int i, r;
	InitList(&L1, 100);
	printf("list addr=%p\tsize=%d\tMaxsize=%d\n");
	while (1)
	{
		printf("Please enter the element value, enter 0 to end the insertion operation: ");
		fflush(stdin);
		scanf_s("%d", &i);
		if (i == 0)
			break;
		printf("Please enter the caret position: ");
		scanf_s("%d", &r);
		InsertList(&L1, i, r - 1);
		printf("Linear table is: ");
		OutputList(&L1);
	}
	while (1)
	{
		printf("Please enter the search element value, enter 0 to end the search operation: ");
		fflush(stdin); /* flush the standard input buffer */
		scanf("%d", &i);
		if (i == 0)
			break;
		r = FindList(&L1, i);
		if (r < 0)
			printf("Not found\n");
		else
			printf("There is an element that meets the conditions, the position is: %d\n", r + 1);
	}
	while (1)
	{
		printf("Please enter the delete element value, enter 0 to end the search operation:");
		fflush(stdin); /* flush the standard input buffer */
		scanf("%d", &i);
		if (i == 0)
			break;
		r = DeleteList1(&L1, i);
		if (r < 0)
			printf("Not found\n");
		else {
			printf("There are qualified elements, the position is: %d\nThe linear table is: ", r + 1);
			OutputList(&L1);
		}
	}
	while (1)
	{
		printf("Please enter the delete element position, enter 0 to end the search operation: ");
		fflush(stdin); /* flush the standard input buffer */
		scanf("%d", &r);
		if (r == 0)
			break;
		i = r = DeleteList2(L1, r - 1);
		if (i < 0)
			printf("Position out of bounds\n");
		else {
			printf("Linear table is: ");
			OutputList(&L1);
		}
	}

	return 0;
}

//code on the book
//#include<stdio.h>
//#include<stdlib.h>
//#include<malloc.h>
//struct LinearList /*Define the linear list structure*/
//{
// int *list; /* store linear list elements */
// int size; /* store the length of the linear table */
// int MaxSize; /* Store the number of elements in the list array*/
//};
//typedef struct LinearList LIST;
//void InitList(LIST *L, int ms) /* Initialize the linear list*/
//{
//	if ((L->list = (int*)malloc(ms * sizeof(int))) == NULL) {
// printf("Memory allocation error!\n");
//		exit(1);
//	}
//	L->size = 0;
//	L->MaxSize = ms;
//}
//
//int InsertList(LIST *L, int item, int rc)
///* item: record value rc: insert position */
//{
//	int i;
// if (L->size = L->MaxSize) /* Linear table is full*/
//		return -1;
// if (rc < 0) /* insert position 0 --> L->size */
//		rc = 0;
//	if (rc>L->size)
//		rc = L->size;
// for (i = L->size - 1; i >= rc; i--) /* move the linear table elements backward */
//		L->list[i + 1] = L->list[i];
//	L->list[rc] = item;
//	L->size++;
//	return 0;
//}
//
//void OutputList(LIST *L) /* Output linear list elements*/
//{
//	int i;
//	for (i = 0; i<L->size; i++)
//		printf("%d ", L->list[i]);
//	printf("\n");
//}
//
//int FindList(LIST *L, int item) /* Return >=0 for element position -1 not found*/
//{
//	int i;
//	for (i = 0; i < L->size; i++)
// if (item == L->list[i]) /* find the same element, return the position */
//		return i;
// return -1; /* not found*/
//}
//
//int DeleteList1(LIST *L, int item)
///* Delete the linear table record of the specified element value, return >=0: delete successfully*/
//{
//	int i, n;
//	for (i = 0; i < L->size; i++)
// if (item == L->list[i]) /* find the same element */
//		break;
//	if (i < L->size) {
//		for (n = i; n < L->size - 1; n++)
//			L->list[n] = L->list[n + 1];
//		L->size--;
//		return i;
//	}
//	return -1;
//}
//
//int DeleteList2(LIST L, int rc) /* delete the linear list record at the specified position*/
//{
// /* Write a subroutine to delete the linear table record at the specified position */
//
//	int DeleteList2(LIST * L, int rc)
//	{
//		int i, n;
//		if (rc<0 || rc >= L->size)
//			return -1;
//		for (n = rc; n<L->size - 1; n++)
//			L->list[n] = L->list[n + 1];
//		L->list--;
//		return 0;
//	}
//}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325608380&siteId=291194637