请问大家:急!!!!!!!!真的很急,谢谢了

ambition2005 2003-01-10 01:50:10
我们要做一个串的,下面是我的程序,可不可以运行,哪位可以帮我看看,太谢谢了//该程序在串头指针传替时出现错误,具体怎么该???????
//还没有弄清楚,具体的在做标记处



#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#define NULL 0

typedef struct node {
int num;
struct node *link;
}NODE;


NODE *create(NODE *h){
//创建一个字符串,返回头结点
char c;
NODE *p,*q;
h=(NODE *)malloc(sizeof(NODE));
h->num=0;
h->link=NULL;
q=h;
printf("input character\n");
while((c=getchar())!='\n'){
++h->num;
p=(NODE *)malloc(sizeof(NODE));
p->link=NULL;
q->link=p;
p->num=c;
q=p;
}
return h;
}




NODE *subs(NODE *h,int pos,int n){
//取出串中指定位置开始的若干字符
NODE *p,*q,*h1,*m;
int i=0;
if((pos+n-1)>h->num){
printf("error\n");//至此,程序正确
return NULL;//返回是否错误?????????
}//取出的字符数多与字符串中的字符数
else{
while(i<pos){
q=p;
p=p->link;
++i;
}
m=p;
i=0;
h1=(NODE *)malloc(sizeof(NODE));
h1->num=n;
h1->link=NULL;
q=h1;//取出字符串的头结点
while(i<n){
++i;
p=(NODE *)malloc(sizeof(NODE));
p->num=m->num;
p->link=NULL;
q->link=p;
q=p;
m=m->link;
}
return h1;
}

}


NODE *insert(NODE *h,NODE *h1,int pos){
int i=0;
NODE *p,*q;
p=h;
while(i<(pos-1)){
++i;
p=p->link;
}
q=p->link;
p->link=h1->link;
h->num=h1->num+h->num;
while(h1->link!=NULL)
h1=h1->link;
h1->link=q;
free(h1);
return h;
}


int ddelete(NODE *h,int pos,int n){
//成功删除,返回0;否则返回-1
int i=0;
NODE *p,*q,*m;
if((pos+n-1)>h->num)
return -1;
if(n==0)
return 0;
while(i<pos-1){
++i;
p=p->link;
}
q=p;
i=1;
p=p->link;
while(i<n){
++i;
m=p;
p=p->link;
free(m);
}
q->link=p->link;
free(p);
h->num=h->num-n;
return 0;
}


int position(NODE *h,NODE *h1){//该函数在找不到指定串时正常运行,如果串
//能找到,运行时会发生非法操作
//串匹配,如不存在,返回-1;存在,返回所在位置POS
NODE *p,*q,*m;
int pos=1;
p=h->link;
q=h1->link;
while(p->num!=q->num){
p=p->link;
++pos;
}
m=p;
while(p!=NULL&&((pos+h1->num-1)<h->num)){
while(p->num==q->num&&p!=NULL&&q!=NULL){//该出语句出错
p=p->link;
q=q->link;
}
if(p==NULL&&q!=NULL)
return -1;
else if(q==NULL)
return pos;
++pos;
p=m->link;
m=m->link;
q=h1->link;
}
return -1;
}



int main(){
long int w=1,pos,n;
char a,c;
NODE *h,*h1,*p,*q;
while(w){
printf("create a string\nplease input Y/N\n");
a=getchar();
if(a=='y'||a=='Y'){
getchar();
h=create(h);
w=0;
}
if(a=='n'||a=='N')
exit(1);
}
while(a!='5'){
printf("Get part of the string? If you want to do so,please input 1;\nFind part of the string? If you want to do so,please input 2;\nInsert another string? If you want to do so,please input 3;\nDelete part of the string? If you want to do so,please input 4;\nExit? If you want to do so,please input 5.\n");
a=getchar();
getchar();//用来消除ENTER键
switch(a){
case '1':{
printf("input the position of the string you want to get\n");
scanf("%d",&pos);
printf("input the number of the string you want to get\n");
scanf("%d",&n);
p=subs(h,pos,n);
if(p=NULL){
printf("heliangliang\n");// 当返回 NULL时,该语句没有被执行,该语句没作用,仅来检测是否被执行
break;
}
else{
printf("%d",p->num);
printf("the following is the string you want to get:\n");
p=p->link;
while(p!=NULL){
p=p->link;
printf("%c",p->num);
}
printf("\n");
break;
}
}
case '2':{
printf("Please input the string you want to find\n");
h1=(NODE *)malloc(sizeof(NODE));
h1->num=0;
h1->link=NULL;
q=h1;
while((c=getchar())!='\n'){
++h1->num;
p=(NODE *)malloc(sizeof(NODE));
p->link=NULL;
q->link=p;
p->num=c;
q=p;
}
pos=position(h,h1);
if(pos==-1)
printf("The string cannot be found !\n");
else
printf("The string's position is %d.\n",pos);
break;
}
case '3':{
printf("Please input the string you want to insert\n");
h1=(NODE *)malloc(sizeof(NODE));
h1->num=0;
h1->link=NULL;
q=h1;
while((c=getchar())!='\n'){
++h1->num;
p=(NODE *)malloc(sizeof(NODE));
p->link=NULL;
q->link=p;
p->num=c;
q=p;
}//至此,程序正确
printf("input the position of the string where you want to insert\n");
scanf("%d",&pos);
h=insert(h,h1,pos);
p=h->link;
printf("The new string which you have inserted another string is as follows:\n");
while(p!=NULL){
printf("%c",p->num);
p=p->link;
}
printf ("\n");
break;
}
case '4':{
printf("Please input the position where you want yo delete\n");
scanf("%d",&pos);
printf("Please input the number of chars of the string you want to delete\n");
scanf("%d",&n);
if(ddelete(h,pos,n))
printf("There is something error that cannot delete the string you offer successfully\n");
else{
printf("The new string is as follows: \n");
p=h->link;
while(p!=NULL){
printf("%c",p->num);
p=p->link;
}
printf("\n");
}
break;
}
case '5': exit(1);
}
}
return 0;
}







...全文
28 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liao2001 2003-03-06
  • 打赏
  • 举报
回复
急?尿出来不就爽了吗?
andyfr1210 2003-03-06
  • 打赏
  • 举报
回复
祝贺你喽,顺便接分.
riverboat 2003-03-06
  • 打赏
  • 举报
回复
就是啦,这么长的程序,哪些地方有问题还是自己找得快呀,要充分使用集成环境的调试功能呀!
programmer200x 2003-03-06
  • 打赏
  • 举报
回复
呵呵,我来捧场,给我加分吧?
也祝贺你自己查出来了。
ambition2005 2003-01-14
  • 打赏
  • 举报
回复
哎,我自己查出来了。
感觉爽死了 。
还是自己做出来好
ambition2005 2003-01-10
  • 打赏
  • 举报
回复
有谁乐意帮帮我吗?谢谢了
ambition2005 2003-01-10
  • 打赏
  • 举报
回复
谁能帮帮我吗?

谢谢了

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧