








我录的歌 http://1680279./ 我现在打不开了????
#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); } }
以下代码由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);
}
#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);
}
}