Basic Operations of Line Sequence Table
Experiment content:
1. Write the basic operation function of the linear list:
(1) InitList(LIST L, int ms), initialize the linear list;
(2) InsertList(LIST L, int item, int rc): insert elements to the specified position of the linear list ;
(3)DeleteList1(LIST L,intitem):Delete the linear list record of the specified element value;
(4)DeleteList2(LIST L,intrc):Delete the linear list record of the specified position;
(5)FindList(LIST *L,int item): Find the elements in the linear list;
(6) OutputList(LIST *L): Output the elements of the linear list;
2. Call the above function to achieve the following operations, the operation steps are as follows:
(1) Initialize the linear list;
(2) Call insert The function builds a linear list;
(3) Find the specified element in the linear list;
(4) Delete the element with the specified value in the linear list;
(5) Delete the element at the specified position in the linear list;
(6) Traverse and output linear table.
Note: Every time a step is completed, the elements of the linear table must be output in time to facilitate observation of the results.
source code:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct LinearList /*定义线性表结构*/
{
int *list; /* 存线性表元素 */
int size; /* 存线性表长度 */
int MaxSize; /* 存list数组元素个数 */
};
typedef struct LinearList LIST;
void InitList( LIST *L, int ms ) /* 初始化线性表 */
{
if( (L->list = (int *) malloc( ms * sizeof(int) ) ) == NULL ) {
printf( "内存申请错误!\n" );
exit( 1 );
}
L->size = 0;
L->MaxSize = ms;
}
int InsertList( LIST *L, int item, int rc )
/* item:记录值 rc:插入位置 */
{
int i;
if( L->size >= L->MaxSize ) /* 线性表已满 */
return -1;
if( rc < 0 ) /* 插入位置为 0 */
rc = 0;
if( rc > L->size )
rc = L->size;
for( i = L->size - 1; i >= rc; i-- ) /* 将线性表元素后移 */
L->list[i+1] = L->list[i];
L->list[rc] = item;
L->size ++;
return 0;
}
void OutputList( LIST *L ) /* 输出线性表元素 */
{
int i;
for( i = 0; i < L->size; i++ )
printf( "%d ", L->list[i] );
printf( "\n" );
}
int FindList( LIST *L, int item ) /* 返回 >=0 为元素位置 ?1 没找到 */
{
int i;
for( i = 0; i < L->size; i++ )
if( item == L->list[i] ) /* 找到相同的元素,返回位置 */
return i;
return -1; /* 没找到 */
}
int DeleteList1( LIST *L, int item )
/* 删除指定元素值的线性表记录,返回>=0:删除成功 */
{
int i, n;
for( i = 0; i < L->size; i++ )
if( item == L->list[i] ) /* 找到相同的元素 */
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 ) /* 删除指定位置的线性表记录,返回0:删除成功 */
{
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->size --;
return 0;
}
void main()
{
LIST LL;
int i, r;
printf( "list addr=%p\tsize=%d\tMaxSize=%d\n", LL.list, LL.size, LL.MaxSize );
InitList( &LL, 100 );
printf( "list addr=%p\tsize=%d\tMaxSize=%d\n", LL.list, LL.size, LL.MaxSize );
while( 1 )
{
printf( "请输入元素值,输入0结束插入操作:" );
fflush( stdin ); /* 清空标准输入缓冲区 */
scanf( "%d", &i );
if( i == 0 )
break;
printf( "请输入插入位置:" );
scanf( "%d", &r );
InsertList( &LL, i, r-1 );
printf( "线性表为: " );
OutputList( &LL );
}
while( 1 )
{
printf( "请输入查找元素值,输入0结束查找操作:" );
fflush( stdin ); /* 清空标准输入缓冲区 */
scanf( "%d", &i );
if( i == 0 )
break;
r = r = FindList( &LL, i );
if( r < 0 )
printf( "没找到\n" );
else
printf( "有符合条件的元素,位置为:%d\n", r+1 );
}
while( 1 )
{
printf( "请输入删除元素值,输入0结束查找操作:" );
fflush( stdin ); /* 清空标准输入缓冲区 */
scanf( "%d", &i );
if( i == 0 )
break;
r = r = DeleteList1( &LL, i );
if( r < 0 )
printf( "没找到\n" );
else {
printf( "有符合条件的元素,位置为:%d\n线性表为:", r+1 );
OutputList( &LL );
}
}
while( 1 )
{
printf( "请输入删除元素位置,输入0结束查找操作:" );
fflush( stdin ); /* 清空标准输入缓冲区 */
scanf( "%d", &r );
if( r == 0 )
break;
i = i = DeleteList2( &LL, r-1 );
if( i < 0 )
printf( "位置越界\n" );
else {
printf( "线性表为:" );
OutputList( &LL );
}
}
}
operation result:
Source of question types: Tsinghua University Press Data Structure Fifth Edition (C language version), edited by Deng Wenhua