📄 新建 文本文档 (2).txt
字号:
一、实验目的
1、掌握用上机调试线性表的基本方法;
2、掌握线性表的基本操作,插入、删除、查找,以及线性表合并等运算在顺序
存储结构和链接存储结构上的运算。
二、实验内容
线性表基本操作的实现
要在线性表的顺序结构上的第i个位置上插入一个元素时,必先将线性表的第i个元素之后的所有元素依次后移一个位置,以便腾空一个位置,再把新元素插入到该位置。若要删除第i个元素时,也必须把第i个元素之后的
所有元素前移一个位置。
三、实验要求
1、 认真阅读和掌握本实验的程序。
2、 上机运行本程序。
3、 保存和打印出程序的运行结果,并结合程序进行分析。
4、 按照你对线性表的操作需要,重新改写主程序并运行,打印出文件清
单和运行结果
程序呈现
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef enum {sucess,error,underflow} status;
int list[MAXSIZE]={0},count=0;
status CreatList()
{ int a;
printf("\nplease input data to creat list and input -1 to end!\n\n");
scanf("%d",&a);
while(a!=-1)
{
if(count<MAXSIZE)
{
list[count++]=a;
//printf("\n\nplease input a data:");
scanf("%d",&a);
}
else
{wzc455133
printf("\nthe list full!");
return underflow;
}
}
return sucess;
}
status DelList(int pos,int *t)
{
int i;
if((pos>=1)&&(pos<=MAXSIZE))
if(count!=0)
{
*t=list[pos-1];
for(i=pos;i<MAXSIZE;i++)
list[i-1]=list[i];
count--;
return sucess;
}
else
{
printf("\nthe list is empty!");
return error;
}
else
{
printf("the position inlegal!");
return error;
}
}
status InsList(int pos,int t)
{
int i;
if(pos<1||pos>MAXSIZE+1)
{
printf("\nthe position inlegal!");
return error;
}
if(count==MAXSIZE)
{
printf("\nthe list full");
return underflow;
}
for(i=count-1;i>=pos-1;i--)
list[i+1]=list[i];
list[pos-1]=t;
count++;
return sucess;
}
void PrintList()
{
int i=0;
while(i<count)
{
printf("%d ",list[i]);
i++;
}
}
void main()
{
int x,y,position;
status s;
clrscr();
s=CreatList();
printf("\n\nthe list is:" );
PrintList();
printf("\n\nplease input a dat to insert:");
scanf("%d",&x);
printf("\nplease input the position:");
scanf("%d",&position);
s=InsList(position,x);
if(s!=sucess) return;
printf("\n\nthe list is:");
PrintList();
printf("\n\nplease input the del position:");
scanf("%d",&position);
s=DelList(position,&y);
if(s!=sucess)return;
printf("\n\nthe deleted number is:%d",y);
printf("\n\nthe list is:");
PrintList();
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -