| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1077 人关注过本帖
标题:我的26个字符的插入来给修改修改把````
只看楼主 加入收藏
guitarliukai
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2004-12-5
收藏
得分:0 

我录的歌 http://1680279./ 我现在打不开了????
2005-04-10 18:15
时空之蕊
Rank: 2
等 级:新手上路
威 望:3
帖 子:691
专家分:0
注 册:2004-10-31
收藏
得分:0 

#include <stdio.h> #define len sizeof(struct a) #include <malloc.h>

struct a { char lk; struct a *next ; };

main() { struct a *head,*p,*q,*s; int i; char ch; head=p=(struct a *) malloc(len); for(i=1;i<=26;i++) { (p->lk)=('a'+i-1); ///**000**change start //为什么这么改实际上第26个的next应该不存在了几应该为NULL if ( i != 26 ) { p->next=(struct a *)malloc(len); p=p->next; } else { p->next = NULL; } /////**000**change end } //p->next=NULL; 00000 delete *********** p=head; while ( p != NULL ) //**111*** changed while (p->next != NULL) { printf("%c ",p->lk); p=p->next; } printf("\n"); q=p=head; printf("\ninput deleted letter:"); scanf("%c",&ch); fflush( stdin ); // **000** add while(ch != '0') { q=p=head; //首先应该先判断p是否为空 //防止p-〉lk的非法访问在VC中空指针好像是注意是好像啊0cccch有可能 //p-〉lk==ch从而出现错误 while ( p != NULL && p->lk != ch )//**222** changed from while(p->lk != ch && p->next != NULL) { q=p; p=p->next; } /* 因为p为空也会推出上面的循环,如果全部都被删除了那么q=p=head都是NULL实际上 //head=head->next是错误的 下面有我写的是自认为正确 这里是原来的去掉 if(p==head) { head=head->next;free(p); } else { if(p->lk == ch) { q->next=p->next; free(p) ; } else printf("can't find it"); } */ /***************************************** 自认为正确的部分*/ if ( p == NULL )//因为p为空也会推出上面的循环 { printf( "Can't find it" ); } else { if ( q == p ) { //就是p==q==head 的情况 head = head->next; free( p ); q = NULL; } else { q->next = p->next; free( p ); } } /***********************************/ q=p=head; printf("\n"); while ( p != NULL)//更改理由和前面的输出打印一样while( p->next != NULL) { printf("%c ",p->lk); p=p->next; } // printf("\n"); q=p=head; printf("\n2:input deleted letter"); fflush(stdin); scanf("%c",&ch); fflush(stdin); } p=head; while( p != NULL)//更改理由和前面的输出打印一样while( p->next != NULL) { printf("%c",p->lk); p=p->next; } q=p=head; printf("\ninput insert letter"); //insert 就看这个插入的```````````````````插入的判断条件大家看看错没(蓝字是) fflush(stdin); scanf("%c",&ch); fflush(stdin); while(ch != '0') { if ( ch < 'a' || ch > 'z' )//add 2 判断输入是否合法 { printf( "\a\n" ); fflush( stdin ); } else { q=p=head; while( p != NULL && p->lk < ch )//更改理由同上面那个while一样的 { q=p; p=p->next; } /*********************************************************/ //更改理由同上 //本人添加的 if ( p == NULL ) { if ( p == q ) { //就是p=q=head=NULL的情况 head = (struct a *) malloc(len); head->lk = ch; head->next = NULL; } else { s=(struct a *) malloc(len); s->lk=ch; s->next = NULL; q->next=s; } } else { if ( p == q ) { //head需要插入 s = (struct a *) malloc(len); s->lk = ch; s->next = head; head = s; } else { s=(struct a *) malloc(len); s->lk=ch; s->next = p; q->next=s; } } /*****************************************************/ /************************************ 下面是原来的去掉 if(p==head) { s=(struct a *) malloc(len); s->lk=ch; s->next=head; p=s; } else { if (p->lk == ch) { printf("inserted is exist"); } else { s=(struct a *) malloc(len); s->lk=ch; s->next=p; q->next=s; p=head; } } printf("\ntest %c\n",p->next->lk); *********************/ printf("\ntest %c\n", ch ); p = head; //add 3************* while( p != NULL) { printf("%c",p->lk); p=p->next; } } //p=head; 去掉 printf("\n2insert a letter"); fflush(stdin); scanf("%c",&ch); } }


我渴望掌控时空的核心——用最先进的技术,打造无比美丽的世界!
2005-04-11 02:07
激情依旧
Rank: 1
等 级:新手上路
威 望:2
帖 子:524
专家分:0
注 册:2005-4-4
收藏
得分:0 

以下代码由guitarliukai 和我 写的。。。。。我们2人合作才写的比较好(在原来的基础上)。。。但是都不够完美。请高手们指点 坚强依然!永不言苦!永不言败!睇透数据结构!编程编程再编程!------激情依旧

#include<stdio.h>

#include<malloc.h>

#include<stdlib.h>

typedef struct Node{

char data;

struct Node *next;

}SLNode,*Linklist;

void output(SLNode *head){

SLNode *p;

p=head;

while(p->next!=NULL){

printf("%c ",p->next->data);

p=p->next;

}

printf("\n");

}

Linklist Deletechar(SLNode *head)

{ SLNode *p,*q;

char ch;

p=head;

printf("please input delete char(The zero is over ! ):\n");

scanf("%c",&ch);

while(ch!='0')

{ p=head;

while(p->next!=NULL&&p->next->data<ch)

p=p->next;

if(p->next->data!=ch)

printf("No fine the delete char\n");

else

{

q=p->next;

p->next=q->next;

free(q);

q=NULL;

}

printf("deleted:\n");

output(head);

printf("plese input delete char:\n");

fflush(stdin);//在此加上清流函数

scanf("%c",&ch);

}

return head;

}

Linklist insert(SLNode *head)

{ SLNode *p,*q;

char ch;

printf("please input your char(The zero is over ! ):\n");

scanf("%c",&ch);

while(ch!='0')

{ p=head;

while(p->next!=NULL&&p->next->data<ch)

p=p->next;

if(p->next->data==ch)

printf("list have a char\n");

else

{

if((q=(SLNode *)malloc(sizeof(SLNode)))==NULL)exit(1);

q->data=ch;

q->next=p->next;

p->next=q;

}

printf("inserted:\n");

output(head);

printf("plese input insert char:\n");

fflush(stdin);//在此加上清流函数

scanf("%c",&ch);

}

return head;

}

main()

{SLNode *head,*p,*q;

int i;

if((head=(SLNode *)malloc(sizeof(SLNode)))==NULL) exit(1);

head->next=NULL;

p=head;

for(i=0;i<26;i++)

{

if((q=(SLNode *)malloc(sizeof(SLNode)))==NULL)exit(1);

(q->data)=('a'+i);

q->next=NULL;

p->next=q;

p=q;

q=NULL;

}

p=head;

printf("26 char:\n");

output(head);

printf("\n");

Deletechar(head);

insert(head);

}


生是编程人!!!!死是编程鬼!!!!颠峰人生!!!焚尽编程!!! 爱已严重死机!情必须重新启动!情人已和服务器断开连接!网恋也需要重新拨号!-----激情依旧
2005-04-11 07:46
激情依旧
Rank: 1
等 级:新手上路
威 望:2
帖 子:524
专家分:0
注 册:2005-4-4
收藏
得分:0 
以下代码由guitarliukai 同学所写的。我认为他写的非常好(程序的健壮好)。。。我未经他本人同意就帮他发表的。希望他勿怪。。。他是用数组做的。大家可以看看他的

#include <stdio.h>

#include <string.h>

main()

{ char str[27]="abcdefghijklmnopqrstuvwxyz";

char ch[2];

int i,j;

printf("\n%s",str);

printf("\ndelete(if exit ,enter 0):");

scanf("%s",ch);

while(ch[0]!='0')

{ j=-1;

for(i=0;str[i]!='\0';i++)

if (ch[0]==str[i])

{j=i;break;}

if (j!=-1)

{ while(str[i+1]!='\0')

{ str[i]=str[i+1];

i++;

}

str[i]='\0';

printf("\n%s\n",str);

}

else

printf("\nno find %c",ch[0]);

printf("\ndelete(if exit,enter 0):");

scanf("%s",ch);

}

printf("\ninsert(if exit,enter 0):");

scanf("%s",ch);

while(ch[0]!='0')

{ if ((ch[0]<'a')||(ch[0]>'z')) printf("\nIt is not letter");

else

{

for(i=0;str[i]!='\0';i++)

{ if(ch[0]>str[i]) continue;

else

{ if(ch[0]==str[i]) {printf("have exist"); break;}

else

{ j=strlen(str);

str[j+1]='\0';

for(;j>i;j--)

str[j]=str[j-1];

str[j]=ch[0];

printf("\n%s",str);

break;

}

}

}

}

printf("\ninsert(if exit,enter 0):");

scanf("%s",ch);

}

}


生是编程人!!!!死是编程鬼!!!!颠峰人生!!!焚尽编程!!! 爱已严重死机!情必须重新启动!情人已和服务器断开连接!网恋也需要重新拨号!-----激情依旧
2005-04-11 07:49
guitarliukai
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2004-12-5
收藏
得分:0 
q=p=head; printf("\ninput insert letter"); //insert 就看这个插入的```````````````````插入的判断条件大家看看错没(蓝字是) fflush(stdin); scanf("%c",&ch); fflush(stdin); while(ch != '0') { if ( ch < 'a' || ch > 'z' )//add 2 判断输入是否合法 { printf( "\a\n" ); fflush( stdin ); } else { q=p=head; while( p != NULL && p->lk < ch )//更改理由同上面那个while一样的 { q=p; p=p->next; } /*********************************************************/ //更改理由同上 //本人添加的 if ( p == NULL ) //我感觉还得加点儿,判断是否为 存在的字符的判断`````` { if ( p == q ) { //就是p=q=head=NULL的情况 head = (struct a *) malloc(len); head->lk = ch; head->next = NULL; } else { s=(struct a *) malloc(len); s->lk=ch; s->next = NULL; q->next=s; } } else { if ( p == q ) // 我感觉还得加点儿,判断是否为 存在的字符`````` { //head需要插入 s = (struct a *) malloc(len); s->lk = ch; s->next = head; head = s; } else { s=(struct a *) malloc(len); s->lk=ch; s->next = p; q->next=s; } }

我录的歌 http://1680279./ 我现在打不开了????
2005-04-11 20:36
时空之蕊
Rank: 2
等 级:新手上路
威 望:3
帖 子:691
专家分:0
注 册:2004-10-31
收藏
得分:0 
呵呵!你没有说明哦!没关系如果需要就判断一次啊!
if ( p-lk != ch )
才插入
else
不插入
 

我渴望掌控时空的核心——用最先进的技术,打造无比美丽的世界!
2005-04-12 01:26
快速回复:我的26个字符的插入来给修改修改把````
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015849 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved