输出一个线性表,并对他进行插入、取数、删除的操作。

RT!!!!!!
使用c++数据库结构来解决。写出程序过程。要求简单明了易理解。后面有步骤解释的且可以执行的加分。

作者:12f3210

#include <stdlib.h>
#include <stdio.h>

#define MAXSIZE 100
typedef struct{
char elem[MAXSIZE]; //用于储存线性表中的元素,元素类型为char;
int len; //线性表的当前表长,即elem数组中已经储存多少个char.
}SqList;

int insert_Sq(SqList *L, int i, char c) //参数i:插入的位置(数组elem中的位置),参数c:要插入的值
{
if (i<1 || i>L->len+1) {
printf("\n插入位置不合理!");
return 0; //判断i的值,若小于1或者大于表长加1,则位置不合理
}
if (L->len == MAXSIZE-1) return -1; //若当前表长等于elem的最大储存量减1,则无法插入

for(int j = L->len; j >= i; --j)
{
L->elem[j+1] = L->elem[j]; //插入位置之后的元素依次后移,且应该先移动最后面的元素
}
L->elem[i] = c;
++L->len; //插入新元素后,当前表长应该加1
printf("您成功插入了:%c\n", c);
return 1;
}
int Delete_Sq(SqList *L, int i) //删除elem中位置为i的元素,若能够删除,则后面元素依次左移
{
if (i<1 || i>L->len) return 0; //不合理的位置,无法进行删除
if (L->len == 0) return -1; //线性表为空,无法进行删除
for (int j = i; j <= L->len-1; j++)
{
L->elem[j] = L->elem[j+1]; //从最前面一个元素开始依次左移,直到最后一位元素结束
}
--L->len; //被删除一个元素后,当前表长减1
return 1;
}

int PrintOut(SqList *L) //打印线性表中的元素
{
if (L->len == 0) return 0; //当前表长为0,所以线性表为空,不能做打印操作
printf("线性表中元素为:");
for (int j = 1; j < L->len; j ++) //j为循环变量,遍历数组0位置到L->Len-1这个位置,依次打印
{
printf("%c", L->elem[j]);
}
}

int main()
{
SqList *s = (SqList *)malloc(sizeof(SqList));
s->len = 0;
char love[] = " I love you!";
for(int i = 1; i < sizeof(love); i ++)
{
insert_Sq(s, i, love[i]);
}
PrintOut(s);
Delete_Sq(s, 8);
Delete_Sq(s, 8);
Delete_Sq(s, 8);
insert_Sq(s, 8, 'u');
PrintOut(s);
insert_Sq(s, 14, 'u');
free(s);
getchar();
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
/*栈操作主要根据top来进行,事实上规定top所指向的位置是没有值的*/
typedef struct
{
char elem[MAXSIZE]; //栈的最大容量为MAXSIZE
int top; //栈顶的值,即为elem数组中最后面的一个元素的位置
}SqStack;

void InitStack(SqStack *s)
{
s->top = 0; //当前top标记elem数组中0位置,说明栈为空
}
int Empty_Sq(SqStack *s) //判断栈是否为空
{
return (s->top == 0); //top为0,则返回true
}
int Push_Sq(SqStack *s, char c)
{
if (s->top == MAXSIZE)
{
printf("栈已满,不能做插入操作!");
return 0; //首先判断栈是否已满(当top标记为elem最后一个元素时)
}
s->elem[s->top] = c; //若栈没满,则将c放入当前top所指位置
s->top++; //然后top下移
printf("%c 成功进栈\n", c);
return 1;
}
int Pop_Sq(SqStack *s, char *y) //出栈操作,将top所指向的元素的下一位置的值保存在*y里
{
if(s->top == 0)
{
printf("栈为空,不能做出栈操作!");
return 0;
}
--s->top; //先让top指向它当前位置的前一个元素的位置
printf("当前元素 %d 出栈,它的值为 %c\n", s->top, s->elem[s->top]);
*y = s->elem[s->top]; //取出top现在指向的那个位置的元素,放入*y;即为出栈操作.

}
int main()
{
SqStack *s = (SqStack *)malloc(sizeof(SqStack));
InitStack(s); //初始化栈,让top为0;
char ch = 'c';
Push_Sq(s, 'a'); //连续三次进栈操作
Push_Sq(s, 'a');
Push_Sq(s, 'c');

Pop_Sq(s, &ch); //做一次出栈操作,数据保存在ch中
printf("%c", ch); //打印

Pop_Sq(s, &ch); //第二次出栈
printf("%c", ch);

Pop_Sq(s, &ch); //第三次出栈
printf("%c", ch);

Pop_Sq(s, &ch); //第四次出栈(当然这次会失败)
printf("%c", ch);
getchar();
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-04-14
#include <iostream.h>
#include <conio.h>
#include <alloc.h>
typedef int Elemtype;
//线性表的基本操作
void Initiallist(Elemtype *L);
int Isempty(Elemtype *L);
void ListTraverse(Elemtype *L);
int NextElem(Elemtype *L);
int PriorElem(Elemtype *L);
int LocateElem(Elemtype *L,Elemtype &e);
void GetElem(Elemtype *L);
void ListInsert(Elemtype *L);
void ListDelete(Elemtype *L);
void ClearList(Elemtype *L);
const int N=10;
Elemtype temp;//全局变量!
void Initiallist(Elemtype *L)
{
for(int i=0;i<N;i++)
*(L+i)='#';
}
int Isempty(Elemtype *L)
{
if(*L=='#')
return 1;
else
return 0;
}
void ListTraverse(Elemtype *L)
{
static int k;
if(*(L)==35)
cout<<"The list is NULL!\n";
else
{
cout<<"The records of the list are:\n";
for(int i=0;i<N+k;i++)
{
if((*(L+i)>32768))
break;
else
cout<<*(L+i)<<" ";
}
k++;
}
}
int NextElem(Elemtype *L)
{
int index;
Elemtype e;
cout<<"Input the records for searching it's next Elem!\n";
cin>>e;
index=LocateElem(L,e);
if(*(L+index+1)>32768)
cout<<"It has no next Elem!\n";
else
cout<<e<<"的后继是:"<<*(L+index+1)<<endl;
}

int PriorElem(Elemtype *L)
{
int index;
Elemtype e;
cout<<"Input the records for searching it's prior Elem!\n";
cin>>e;
index=LocateElem(L,e);
if(index>N)
cout<<"It has no next Elem!\n";
else if(index==-1||index==0)
{
cout<<"It has no prior Elem!\n";
return 0;
}
else
cout<<e<<"的前驱是:"<<*(L+index-1)<<endl;
}
int LocateElem(Elemtype *L,Elemtype &e)
{
int i;
for(i=0;i<N+1&&(*(L+i)!='#');i++)
{
if(*(L+i)==e)
return i;
else
continue;
}
if(i<N||i>=N)
return -1;
return i;
}
void GetElem(Elemtype *L)
{
int index;
cout<<"Input the value of i:\n";
cin>>index;
if(*(L+index-1)>32768)
cout<<"The records NULL!\n";
else
cout<<"The records which you want to search are: "<<*(L+index-1)<<endl;
}
void ListInsert(Elemtype *L)
{
Elemtype e;
int index,i;
cout<<"Please input only one inserted number and it's location!\n";
cin>>e;
cin>>index;
if(index>N)
*(L+index-1)=e;
else
{
for(i=N;i>=index;i--)
*(L+i)=*(L+i-1);
*(L+index-1)=e;
}
ListTraverse(L);
}
void ListDelete(Elemtype *L)
{
Elemtype e;
int index,i;
cout<<"Input the number which you want to deleted!\n";
cin>>e;
index=LocateElem(L,e);
for(i=index;i<N+1&&(*(L+i)!='#');i++)
*(L+i-1)=*(L+i);
cout<<"Deleted "<<e<<endl;
ListTraverse(L);
}
void ClearList(Elemtype *L)
{
for(int i=0;i<N+1;i++)
*(L+i)='#';
}
int main()
{
int choice,flag=0;
char ch;
Elemtype *p,e;
p=(Elemtype *)malloc((N+1)*sizeof(Elemtype));
if(!p)
{
cout<<"No more memory can be obtained!\n";
goto loop;
}
loop: p=(Elemtype *)realloc(p,(N+1)*sizeof(Elemtype));
if(!p)
{
cout<<"overflow!\n";
exit(0);
}
Initiallist(p);
cout<<"Input "<<N<<" records!\n";//数据互不相同!
for(int i=0;i<N;i++)
cin>>*(p+i);
if(Isempty(p))
cout<<"The list is NULL!\n";
cout<<" Menu Fuction \n"
<<" 1: 遍历线性表的元素!( 只允许调用一次!)\n"
<<" 2: 求某一元素的前驱和后继!\n"
<<" 3: 获取线性表L中的第i个数据元素内容!\n"
<<" 4: 在线性表中插入一个元素!\n"
<<" 5: 删除线性表中值为e的元素!\n"
<<" 6: 检索值为e的数据元素!\n"
<<" 7: 清空线性表!"<<endl;
do
{
cout<<"Please input your choice!\n";
cin>>choice;
switch(choice)
{
case 1: ListTraverse(p); break;
case 2: NextElem(p);
PriorElem(p);
break;
case 3: GetElem(p); break;
case 4: ListInsert(p); break;
case 5: ListDelete(p); break;
case 6:
cout<<"Input the records !\n" ;
cin>>e;
cout<<"It's location is "<<LocateElem(p,e)+1;
break;
case 7: ClearList(p);
cout<<"Now ";
ListTraverse(p);
break;
}

cout<<"\nDid you want to continue the operation?(Y/N)\n";
cin>>ch;
if(ch=='Y'||ch=='y')
flag=1;
else
flag=0;
}while(flag);
free(p);
getch();
return 0;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网