Ⅰ 用c语言设计线性顺序表,求前驱,后进的问题。
下面是我该完的程序。请运行一下,我改的地方都加了注释”//这里修改了“
#include <stdio.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int status;
typedef int elemtype;
typedef struct {
elemtype elem[MAXSIZE];
int length;
}sqlist;
/*初始化*/
status initlist(sqlist *l){
l->length=0;
return OK;
}
/*销毁*/
status destroylist(sqlist *l){
l->length=0;
return OK;
}
/*顺序表置空*/
status clearlist(sqlist *l){
l->length=0;
return OK;
}
/*求顺序表长度*/
int listlength(sqlist l){
return l.length;
}
/*顺序表空否*/
status listempty(sqlist l){
if( l.length) return FALSE;
else return TRUE;
}
/*查找*/
int locateelem(sqlist l,elemtype e){
int i=0;
while(i<l.length)
if (l.elem[i++]==e) return i;
return 0;
}
/*读第i个元素*/
status getelem(sqlist l,int i,elemtype *e){
if((i<1)||(i>l.length)) return ERROR;
*e=l.elem[i-1];
return OK;
}
/*求前驱*/
status priorelem(sqlist l,elemtype cur_e,elemtype *pre_e){
int i;
i=locateelem(l,cur_e);
if((i==0)||(i==1)) {*pre_e='no';printf(" 没有前驱 ");;return ERROR;} //这里修改了
else //这里修改了
{
*pre_e=l.elem[i-2];
return OK;
}
}
/*求后继*/
status nextelem(sqlist l,elemtype cur_e,elemtype *next_e){
int i;
i=locateelem(l,cur_e);
if((i==0)||(i==l.length)){ *next_e='no';printf(" 没有后继 ");return ERROR;} //这里修改了
else //这里修改了
{
*next_e=l.elem[i];
return OK;
}
}
/*在第i个位置插入元素e*/
status listinsert(sqlist *l,int i,elemtype e){
int j;
if((i<1)||(i>l->length+1)) return ERROR;
for(j=l->length;j>i-1;j--) l->elem[j]=l->elem[j-1];
l->elem[i-1]=e;
l->length++;
return OK;
}
/*删除第i个元素*/
status listdelete(sqlist *l,int i,elemtype *e){
int j;
if((i<1)||(i>l->length)) return ERROR;
*e=l->elem[i-1];
for(j= i;j< l->length;j++) l->elem[j-1]=l->elem[j];
l->length--;
return OK;
}
/*遍历*/
status listtraverse(sqlist l){
int i;
for(i=1;i<=l.length;i++)
printf("%d ",l.elem[i-1]);
printf("\n");
return OK;
}
/*建立顺序表*/
status listcreate(sqlist *l){
int i=0;
elemtype e;
printf("input data(end by -1):");
scanf("%d",&e);
while(e!=-1){
l->elem[i++]=e;
scanf("%d",&e);
}
l->length=i;
return OK;
}
/*主函数*/
main(){
sqlist la,lb,lc;
int i;
elemtype e,pre_e,next_e;
listcreate(&la);
listtraverse(la);
printf("the length of the list is %d\n",listlength(la));
printf("input i and e for insert:");
scanf("%d %d",&i,&e);
if(listinsert(&la,i,e)) listtraverse(la);
else printf("i is error!\n");
printf("input i :");
scanf("%d",&i);
getelem(la,i,&e);
printf("the element is %d ,",e);
priorelem(la,e,&pre_e);
nextelem(la,e,&next_e);
if(pre_e!='no')printf("the priore is %d\n",pre_e); //这里修改了
if(next_e!='no')printf("the next is %d\n",next_e); //这里修改了
listdelete(&la,i,&e);
listtraverse(la);
}
Ⅱ 输入一个字符,然后顺序输出该字符的前驱字符,该字符本身和后续字符 c语言
程序运行如下:
#include<stdio.h>
intmain()
{
printf("请输入一个字符:
");
charmiddle;
scanf("%c",&middle);
printf("前驱字符[%c],本身[%c],后续字符[%c]
",middle-1,middle,middle+1);
return0;
}
PS: 里面没有做异常判断,比如是否确实是可见字符,是否有溢出等。
有问题请追问,谢谢!
Ⅲ C语言:求前驱和后继字母。输入一个大写字母,求对应的小写字母及它的前驱和后继
楼主你好。
#include<stdio.h>
int main()
{
char ch,ch1,ch2;
scanf("%c",&ch);
if(ch>='B'&&ch<='Y'){
ch1=ch+31;
ch2=ch+33;
}else if(ch=='A'){
ch1='-';
ch2=ch+33;
}else {
ch1=ch+31;
ch2='-';
}
printf("%c %c\n",ch1,ch2);
return 0;
}
ch,ch1,ch2应该声明为char类型。
你原先定义成为int类型会导致条件判断的时候总是进入最后一个else中。所以只有前驱,没有后继。
Ⅳ 在c语言中,求一个字符的前驱字符和后驱字符。这个程序怎么编写,大一问题。求解答
定义一个字符串,然后输入一个字符,再判断出它在字符串中的位置i,输出a[i-1],a[i+1]就ok
Ⅳ 怎样用c语言输出前驱字符和后继字符
如楼上所说
常见于连续存储的数据结构
数组,链表,栈,队列
以字符数组为例
"abcde",其中b的前驱是a,后继是c
Ⅵ C语言:输入一个大写字母,求其对应的小写字母及其它的前驱与后继
#include <stdio.h>
int main()
{
int ch;
ch=getchar();
putchar(ch-'a'+'A') ;
putchar(ch-1);
putchar(ch+1);
return 0;
}
如果没有太多要求,这样应该是可以的。