时间:2021-05-19
复制代码 代码如下:
#include<stdio.h>
#include<malloc.h>
#define maxsize 1024
typedef char datatype;
typedef struct
{
datatype data[maxsize];
int last;
}sequenlist;
int insert(sequenlist *L,datatype x,int i)
{
int j;
if(L->last==maxsize-1)
{
printf("overflow");
return 0;
}
else if((i<0)||(i>L->last))
{
printf("error,please input the right 'i'");
return 0;
}
else
{
for(j=L->last;j>=i;j--)
{
L->data[j+1]=L->data[j];
L->data[i]=x;
L->last=L->last +1;
}
}
return(1);
}
int dellist(sequenlist *L,int i)
{
if((i<0)||(i>L->last))
{printf("error,please input the right 'i'");
return 0;}
else
{
for(;i<L->last ;i++)
L->data[i]=L->data[i+1];
L->last =L->last-1;
return(1);
}
}
void createlist(sequenlist *L)
{
int n,i;
char tmp;
printf("请输入元素个数:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("data[%d]=",i);
fflush(stdin);
scanf("%c",&tmp);
L->data[i] =tmp;
}
L->last=n-1;
printf("/n");
}
void printflist(sequenlist *L)
{
int i;
for(i=0;i<L->last ;i++)
{
printf("data[%d]=",i);
printf("%c\n",L->data [i]);
}
}
main()
{
sequenlist *L;
char cmd,x;
int i;
L=(sequenlist *)malloc(sizeof(sequenlist));
createlist(L);
printflist(L);
do
{
printf("i,I...插入\n");
printf("d,D...删除\n");
printf("q,Q...退出\n");
do
{
fflush(stdin);
scanf("%c",&cmd);
}while((cmd!='d')&&(cmd!='D')&&(cmd!='q')&&(cmd!='Q')&&(cmd!='i')&&(cmd!='I'));
switch(cmd)
{
case 'i':
case 'I':
printf("请输入你要插入的数据:");
fflush(stdin);
scanf("%c",&x);
printf("请输入你要插入的位置:");
scanf("%d",&i);
insert(L,x,i);
printflist(L);
break;
case 'd':
case 'D':
printf("请输入你要删除的元素的位置:");
fflush(stdin);
scanf("%d",&i);
dellist(L,i);
printflist(L);
break;
}
}while((cmd!='q')&&(cmd!='Q'));
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
数据结构顺序表操作复制代码代码如下:#include#include#include#defineLIST_INIT_SIZE100#defineLISINCR
前言首先说下线性表,线性表是一种最基本,最简单的数据结构,通俗点讲就是一维的存储数据的结构。线性表分为顺序表和链接表:顺序表示指的是用一组地址连续的存储单元依次
我们先看一下相关数据结构的知识。在学习线性表的时候,曾有这样一个例题。已知一个存储整数的顺序表La,试构造顺序表Lb,要求顺序表Lb中只包含顺序表La中所有值不
本文实例讲述了php数据结构之顺序链表与链式线性表。分享给大家供大家参考,具体如下:链表操作1、InitList(L):初始化链表2、DestroyList(L
用Python仿照C语言来实现线性表的顺序存储结构,供大家参考,具体内容如下本文所采用的数据结构模板为《数据结构教程》C语言版,李春葆、尹为民等著。该篇所涉及到